Hello! I’m planning to make a very realistic space simulator, but I’m having difficulties with the physics part, how do I make code about gravity? I would like to use the equation F = G m1.m2/r2 in which depending on the mass of the object or model, the player would be attracted by gravity, the mass could be a variable, and in empty spaces the gravity would be zero, anyway, please help me, thanks
Make a script that calculates the gravitational force between the player (or object) and a celestial body based on their masses and distance, applying a force to the player to simulate gravity. Xx
Sure there are many ways to do this.. In a game I’m working on I have an area gravity is normal (a rectangle), other than that it’s space-like.
--LocalScript in StarterPlayerScripts
local rs=game:GetService("RunService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")
local velocity, grav = Vector3.zero, false
function setGrav(tog)
if tog then workspace.Gravity = 196
velocity = Vector3.zero
end
end
function check()
local localPos = workspace.Omega.GCube.CFrame:PointToObjectSpace(root.Position)
local size = workspace.Omega.GCube.Size / 2
grav = math.abs(localPos.X) <= size.X and
math.abs(localPos.Y) <= size.Y and
math.abs(localPos.Z) <= size.Z
setGrav(grav)
end
while rs.Stepped:Wait() do check()
if root.AssemblyLinearVelocity.Magnitude > 20 then
velocity = root.AssemblyLinearVelocity
end if not grav then workspace.Gravity = 0
root.AssemblyLinearVelocity = velocity
end
end
This is pretty old, has always worked well. I can see a few spots that could use some refining.