I am using a modified version of egomoose’s gravity controller, which i found on the dev forums:
However, the gravity controller only affects players. I need to know how (if possible) to apply the same effect to unanchored parts.
How can I approach this?
Hi, i’m the person you mentioned that remixed Egomoose’s gravity code. I’ve encountered this problem myself and also have the solution for it.
What I did was to disable Workspace gravity, setting it to 0. Programmed a script so that every time a player joins, the program will create a LineForce located in the model. This also requires another attachment at the center of planet you want the object to be gravitated to. Create a folder named ‘Bodies’ in Workspace and put whatever objects you want the player to be gravitated to.
Now we also need to create LineForces on the said objects as well.
Script for LineForce
The essential script:
Store it in a part as a server script.
for index, object in pairs(game.Workspace.Bodies:GetChildren()) do
local LineForce = script.LineForce:Clone()
LineForce.InverseSquareLaw = false
LineForce.Visible = false
LineForce.Attachment0 = script.Parent.RootAttachment
LineForce.Name = object.Name
LineForce.Attachment1 = object.Attachment
LineForce.Parent = script.Parent
LineForce.MaxForce = math.huge
end
My original script:
This script accounts for decreasing the gravitational force when the player goes farther away or into the core of the planet, and the ability to latch on to multiple planets depending on which one is closest to the object / player.
local Mass = 0
for Index, Object in pairs(script.Parent.Parent:GetDescendants()) do
if Object.ClassName == "MeshPart" or Object.ClassName == "Part" or Object.ClassName == "UnionOperation" or Object.ClassName == "VehicleSeat" then
if Object.Massless == false then
Mass += Object.Mass
end
end
end
for index, object in pairs(game.Workspace.Bodies:GetChildren()) do
local LineForce = script.LineForce:Clone()
LineForce.InverseSquareLaw = false
LineForce.Visible = falsez
LineForce.Attachment0 = script.Parent.RootAttachment
LineForce.Name = object.Name
LineForce.Attachment1 = object.Attachment
LineForce.Parent = script.Parent
LineForce.MaxForce = math.huge
end
while wait(0.1) do
local closestPart, closestPartMagnitude
local tmpMagnitude
local CamPos = script.Parent.Position
for i, v in pairs(game.Workspace.Bodies:GetChildren()) do
if closestPart then -- we have a part
tmpMagnitude = (CamPos - v.Position).magnitude
-- check the next part
if tmpMagnitude < closestPartMagnitude then
closestPart = v
closestPartMagnitude = tmpMagnitude
end
else
-- our first part
closestPart = v
closestPartMagnitude = (CamPos - v.Position).magnitude
end
end
script.Parent.Parent.PlanetFocus.Value = closestPart
if closestPartMagnitude <= closestPart.GravityRadius.Value then
for index, object in pairs(script.Parent:GetChildren()) do
if object.ClassName == "LineForce" then
if object.Name == closestPart.Name then
object.Enabled = true
if closestPartMagnitude <= closestPart.PlanetSize.Value then
object.Magnitude = (Mass * closestPart.PlanetMass.Value * (game.Workspace.Settings.GConstant.Value * 2 - 1) * 10^5)/(2*closestPartMagnitude - (3*closestPart.PlanetSize.Value))^2
else
object.Magnitude = (Mass * closestPart.PlanetMass.Value * game.Workspace.Settings.GConstant.Value * 10^5)/closestPartMagnitude^2
end
else
object.Enabled = false
end
end
end
end
end
Now paste the script chosen as the child of the object, like a part. However, the part will need to be inside a Model class as well.
Hope this helps! Feel free to ask me more questions, I’ve also managed to integrate the gravity module for players to sit in seats as well.