How To Make Planet Gravity Affect Players And Parts

Print(“No Code For A Base I Need Help Figuring This Out”)

1 Like

image

gravity force = gravity constant x ((mass of planet) x (mass of part) / (square of the distance between them))

use lineForce and calculate gravity every frame.
you can ignore the force that the parts and player’s pull on the planet.

And you need to adjust the values ​​to the Roblox units

1 Like

would this all be done in a script/local script or multiple and I am bad at coding so would this be easy to implement

In my opinion, characters should be done in local for performance and server for Parts
(I’m not 100% sure),

It won’t be very easy, but you need to use LineForce instead of vectorForce,
So you can make it easier.

And the code could be like this:

local G = 6.67430 ^ -11  
G *= 64 --If 4 studs is equal to 1m

--client (StarterCharacterScripts)
local RunService = game:GetService("RunService")
local Collection = game:GetService("CollectionService")
local planetTag = "Planet" --or anything

local character = script.Parent
local humRootPart = character:WaitForChild("HumanoidRootPart")
local rootAttach = Instance.new("Attachment")
rootAttach.Parent = humRootPart

local GravityForces = {}

local function planetGravity(planetPart)
	--center of planet
	local planetAttach = Instance.new("Attachment")
	planetAttach.Parent = planetPart

	--add gravity lineforce
	local gravityForce = Instance.new("LineForce")
	gravityForce.Attachment0 = rootAttach
	gravityForce.Attachment1 = planetAttach
	gravityForce.ApplyAtCenterOfMass = true
	gravityForce.Parent = humRootPart
	table.insert(GravityForces, gravityForce)
end

for _, object in pairs(Collection:GetTagged(planetTag)) do
	planetGravity(object)
end

--if you want to
local maxAttitude = 500

--update
RunService.Heartbeat:Connect(function()
	for _, force: LineForce in GravityForces do
		local part0 = force.Attachment0.Parent
		local part1 = force.Attachment1.Parent		
		local distance = (force.Attachment0.WorldPosition - force.Attachment1.WorldPosition).Magnitude
		
		if distance > 500 then
			continue
		end
		
		--Calculate Force of gravity
		force.Magnitude = G * ((part0.Mass*10^7 * part1.Mass) / distance^2)
	end
end)
  • there will be multiple planets, so using CollectionService
  • you need to make planet part even if the planet is made of terrain.
    and add tag your planet part like “Planet”

I have no time but this is my best

okay thank you for your help ill try to make a script that looks like that

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