Planetary Gravity

First off, no I do not want to use EgoMoose’s gravity controller script as it is very nauseating and I don’t want players to walk on buildings. My goal is to make a terrain planet with a part in the middle that can (hopefully) attract the player to the planet with line forces. I have several problems though. How does someone make it so that the line force is in the humanoid root part and is being attracted to every planet at once (each one with varying weakness to keep it balanced)? Secondly, how would the camera auto-rotate to look normal, finally how would someone stay on the planet? if they were on the side, they wont stand normally and instead flip on their side.

2 Likes

Creating a planetary gravity system that doesn’t rely on EgoMoose’s gravity controller and avoids the issues you’ve mentioned can be challenging. However, I can provide you with a basic outline of how you can achieve this using BodyForce, custom camera manipulation, and character orientation adjustments.

  1. Apply a BodyForce to the HumanoidRootPart:
    Create a BodyForce instance in the HumanoidRootPart of each player’s character. Update the force applied by this BodyForce based on the player’s position relative to the planet’s center. To apply different forces for different planets, you can adjust the force magnitude depending on the planet’s size and distance.
local function updateGravityForce(character, planetCenter, gravityStrength)
    local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
    if humanoidRootPart then
        local bodyForce = humanoidRootPart:FindFirstChild("GravityForce") or Instance.new("BodyForce", humanoidRootPart)
        bodyForce.Name = "GravityForce"
        local direction = (planetCenter - humanoidRootPart.Position).Unit
        bodyForce.Force = direction * gravityStrength * humanoidRootPart.Mass
    end
end
  1. Custom camera manipulation:
    To adjust the camera to always look “normal” relative to the planet’s surface, you can create a custom camera system. The following code demonstrates how to set up a basic custom camera for the player:
local function updateCamera(planetCenter)
    local camera = workspace.CurrentCamera
    local player = game.Players.LocalPlayer
    local character = player.Character
    local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")

    if humanoidRootPart then
        local cameraOffset = Vector3.new(0, 15, 15) -- Adjust this offset as needed
        local cameraDirection = (humanoidRootPart.Position - planetCenter).Unit
        camera.CFrame = CFrame.new(humanoidRootPart.Position + cameraDirection * cameraOffset, humanoidRootPart.Position)
    end
end
  1. Adjust the character’s orientation:
    To prevent the character from flipping on its side, you can use BodyGyro to control the character’s orientation relative to the planet surface:
local function updateCharacterOrientation(character, planetCenter)
    local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
    if humanoidRootPart then
        local bodyGyro = humanoidRootPart:FindFirstChild("OrientationGyro") or Instance.new("BodyGyro", humanoidRootPart)
        bodyGyro.Name = "OrientationGyro"
        local upDirection = (humanoidRootPart.Position - planetCenter).Unit
        bodyGyro.CFrame = CFrame.new(Vector3.new(), upDirection)
        bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
    end
end

Now, you can call these functions inside a loop or in response to specific events like entering a planet’s atmosphere:

while wait() do
    local player = game.Players.LocalPlayer
    local character = player.Character
    if character then
        local planetCenter = Vector3.new(0, 0, 0) -- Replace with your planet's center position
        local gravityStrength = 500 -- Replace with your desired gravity strength

        updateGravityForce(character, planetCenter, gravityStrength)
        updateCamera(planetCenter)
        updateCharacterOrientation(character, planetCenter)
    end
end

Please note that this is a basic implementation and might need further adjustments depending on your game’s requirements.

5 Likes

That seems like it will work, but I need to know where to place the scripts, like StarterCharacterScripts

1 Like

To implement the planetary gravity system I previously described, you can place the main script in a ServerScriptService script. This script will handle the gravity for all the players and the planets in the game.

For the camera rotation to work correctly, you’ll need to add a LocalScript inside the StarterPlayerScripts. This script will handle the rotation of the camera for each individual player as they move on the planets.

Here’s a basic outline of how you can organize the scripts:

  1. Create a script inside ServerScriptService. In this script, you’ll handle the gravity calculations and updating the humanoid root part’s position and orientation.
  2. Create a LocalScript inside StarterPlayerScripts. In this script, you’ll handle the camera rotation logic. You can use the workspace.CurrentCamera property to access the current player’s camera and manipulate its CameraType, CFrame, and other properties.

By placing the scripts in these locations, you’ll ensure that they run correctly for all players in your game.

2 Likes

Wouldnt the humanoid upright itself due to normal behavior, and also ragdoll after MaxSlopeAngle is reached? I tried this some days ago and it didnt work.

1 Like

Nothing really seems to be going on, and the camera keeps on giving me “infinite yield possible on” in the output log

I’ve mentioned already,

This basic implementation might need further adjustments to make it work.

This is just a theory on how Planetary Gravity could be made.

1 Like

I tried to build a Planet with Gravity System a few months ago, I had to stop due to other projects…

But yeah, the Default CharacterController will fight to keep the Character into the right orientation, no matter if you apply a force into it to rotate it, it will fight against the force to keep it in the correct orientation.

To me, seems mandatory to disable the default controller and build your own. I think that suggestion/step should be added into the approach being explained in this thread.

Is there anyway to fork the character controller to prevent this or is this due to internal humanoid code?

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