Why does my gravity code not work?

The title is kind of true but I really just want to know how to make(not ego moose) individual gravity (read below code block), I’ll give my code though why doesn’t it work? It wont apply the force.

--[variables]--
local GravityField = script.Parent
local Gravity = GravityField.Gravity

--(functions)--

local function ApplyGravity(otherPart) -- applys the gravity field's "gravity" to the instance that has entered the field
	print("apply")
	if otherPart.ClassName == "Model" then
		print("model")
		otherPart.PrimaryPart.AssemblyLinearVelocity = Vector3.new(0, -Gravity, 0)
	else
		print("part")
		otherPart.AssemblyLinearVelocity = Vector3.new(0, -Gravity, 0)
	end
end

local function RemoveGravity(otherPart) -- subtracts the gravity field's "gravity" when the instances leaves the field
	print("remove")
	if otherPart.ClassName == "Model" then
		print("model")
		local ModelVelocity = otherPart.PrimaryPart.AssemblyLinearVelocity
		ModelVelocity += Vector3.new(0, Gravity, 0)
	else
		print("part")
		local PartVelocity = otherPart.AssemblyLinearVelocity
		PartVelocity += Vector3.new(0, Gravity, 0)
	end
end

--{events}--
GravityField.Touched:Connect(ApplyGravity)
GravityField.TouchEnded:Connect(RemoveGravity)

It’s been bothering me, how do you make individual gravity for planets? Not gravity like ego moose but a baseplate that has a gravity of 196.2 while the workspace gravity is 0.
I have used a vector force relative to the world with a force of 196.2 to make a zero gravity part but I want to have gravity on a large scale.

Please get this out of my head! I honestly can’t think of how to go upon making individual gravity(not ego moose gravity)! (its probably really simple…)

dont give me a script just tell me the “idea” of what i need to do

1 Like

What you want to do here is apply a downward force on the root part, instead of a velocity. If you want to replicate the effect of workspace gravity, for example, you would apply a force in the -Y direction. Remember F=ma from physics class? This is how you work out how much force you need. You take your desired gravitational acceleration (e.g. 196.2) and multiply it by the character’s assembly mass.

The problem with using velocity is that you won’t have any acceleration, everything will just fall at constant speed. If that speed is too high, things might also vibrate against the ground as they try to penetrate and the overlap gets resolved.

1 Like

do i apply a vector force to every valid part or model that comes into the field?

Generally only to the root part of each assembly. For characters, the HumanoidRootPart. For other models, you should of course set a primary part, but also make sure the assembly root is the part you expect it to be by looking at the assembly root part property of the model in the properties panel. You can use part’s RootPriority to make them more likely to be the assembly root.

If your model has multiple assemblies, which is the case when things are attached in ways other than rigid Welds and Motor6Ds, you’ll need to experiment. If you use loose constraints like ropes, or prismatic with no servo or motor, I’m guessing you’re going to need to put a force on each sub assembly’s root part. can’t say I’ve ever done this, so you’ll have to just try it and find out!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.