How to make custom gravity?

I want to make a custom gravity for characters and objects to a planet and depending on the magnitude or depending how far a character and object is near the planet, It will fall to the planet because of gravity. Instead of using EgoMoose’s gravity controller, isn’t there another way? The reason I don’t prefer using his gravity controller is because It doesn’t use magnitude and since I’m new to using modules, I don’t know how to use module. But still, I still prefer to make my own… But, how? How can I specifically achieve that. To let you know, the workspace gravity is set to 0.

Please answer, I don’t really have time waiting.

Note: This is for asking help, and I’m not asking for a free script.

You basically create 2 reference vectors and have the player go there with the gravitational force. I made this before but you didnt want the script so :stuck_out_tongue:

EDIT: I dug through my place files and found it put in server script service.rbxm (1.8 KB)

EDIT 2: Scared of links? Here is the code (note multiply other dependencies are at play)

local function getMass(char)
	local mass = 0
	for _, v in pairs(char:GetDescendants()) do
		if v:IsA("BasePart") then
			mass += v:GetMass()
		end
	end
	return mass
end

local function getClosestFaux(char, fauxs)
	local closest = 0
	local faux = nil
	for i, v in pairs(fauxs:GetChildren()) do
		if v:IsA("BasePart") then
			local dif = (v.Position - char.HumanoidRootPart.Position).Magnitude
			if dif > closest then
				closest = dif
				faux = v
			end
		end
	end
	return closest, faux, faux:GetMass()
end

local function getScalar(char)
	local m1 = getMass(char)
	local difference, faux, m2 = getClosestFaux(char, workspace.Fauxs)
	return m1 * m2 / difference^2, faux, difference
end

local function getNormal(char, faux)
	return (faux.Position - char.HumanoidRootPart.Position).Unit
end

local function getRotationBetween(u, v, axis)
	local dot, uxv = u:Dot(v), u:Cross(v)
	if (dot < -0.99999) then return CFrame.fromAxisAngle(axis, math.pi) end
	return CFrame.new(0,0,0, uxv.x, uxv.y, uxv.z, 1 + dot)
end


game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
	char.Humanoid.PlatformStand = false
	local attractor = script.attractor:Clone()
	attractor.Parent = char.HumanoidRootPart
	game:GetService("RunService").Heartbeat:Connect(function()
		local scalar, faux, difference = getScalar(char)
			local normal = getNormal(char, faux)
			normal = char.HumanoidRootPart.CFrame:VectorToObjectSpace(normal)
		local force = scalar * normal
		attractor.Force = force
		local u = char.HumanoidRootPart.CFrame.UpVector
			local axis = u:Cross(normal)
			char.LowerTorso.Root.C0 = CFrame.new(0,-1,0,1,0,0,0,1,0,0,0,1) * getRotationBetween(u, -normal, axis)
		end)
	end)
end)
1 Like

What is fauxs and also how does this work?

You could use this:

Workspace.Gravity won’t affect client if it’s only changed on the client. has to be changed on server.

I don’t get how that might help.

I still need help guys :<<<<<<

Sorry for the long wait.
If it’s for gamers, use this in a LocalScript on StarterPlayerScripts:

workspace.Gravity = 5    -- Customize this.

If it’s for parts, use this in a Script inside the part:

local BodyForce = Instance.new("BodyForce",script.Parent)
local Mass_Gravity = script.Parent:GetMass() * workspace.Gravity
local Custom = 0.9    -- Customize this.
BodyForce.Force = Vector3.new(0, Mass_Gravity*Custom ,0)

I’m sorry I don’t think you get it, I mean it as custom gravity on the planet (and I want gravity to be in 0), and I want the gravity to focus by the main part of the planet (like the core).

Pls help, I really need help. :((((

1 Like