How do i make a part that orbits/rotates around another part?

even if i set the world pivot thing it rotates vertically instead of horizontally, anyways i can change that?

Just change a different value in the CFrame to rotate it on another axis

e.g.

local function updatePosition(deltaTime)
    local angle = orbitSpeed * deltaTime
    local orbitPosition = centralPart.Position + Vector3.new(orbitRadius * math.cos(angle), orbitRadius * math.sin(angle), 0)
    orbitingPart.CFrame = CFrame.new(orbitPosition) * CFrame.Angles(0, 0, angle)
end
local function updatePosition(deltaTime)
    local angle = orbitSpeed * deltaTime
    local orbitPosition = centralPart.Position + Vector3.new(orbitRadius * math.cos(angle), orbitRadius * math.sin(angle), 0)
    orbitingPart.CFrame = CFrame.new(orbitPosition) * CFrame.Angles(0, angle, 0)
end
local function updatePosition(deltaTime)
    local angle = orbitSpeed * deltaTime
    local orbitPosition = centralPart.Position + Vector3.new(orbitRadius * math.cos(angle), orbitRadius * math.sin(angle), 0)
    orbitingPart.CFrame = CFrame.new(orbitPosition) * CFrame.Angles(angle, 0, 0)
end

tried that. changing it to another thing than z while just do the same thing but the part while also spin on itself

If you want it to orbit around with physics using gravity, consider looking into LineForces, with .InverseSquareLaw enabled.

It’s specifically designed for gravity.

If you want to do it using CFrames, this equation should help:

part.CFrame = rotation * position + point

point = the point orbiting around
rotation = the rotation around point
position = the relative position to the point

They are all CFrames. You can make it orbit by changing the rotation value, see code below.

local degreesPerSecond = 5

while true do
    rotation *= CFrame.Angles(0, math.rad(task.wait() * degreesPerSecond), 0)
end

I haven’t tested this system, tell how it works if you implement it.

Yes, you can make the planet look towards its direction of movement. You can achieve this by calculating the direction vector between the current position and the next position, and then using the CFrame.lookAt() method to orient the planet towards that direction

Here’s your modified script.

-- Get the parts for the sun and the planet
local sun = game.Workspace:WaitForChild("Sun")
local planet = game.Workspace:WaitForChild("Planet")

-- Define the orbit parameters
local orbitRadius = 10 -- Adjust the radius of the orbit
local orbitSpeed = 1 -- Adjust the speed of orbit

-- Function to update the planet's position and orientation in orbit
local function updateOrbit()
    -- Calculate the new position of the planet in orbit
    local angle = math.rad(orbitSpeed)
    local newX = sun.Position.X + orbitRadius * math.cos(angle)
    local newZ = sun.Position.Z + orbitRadius * math.sin(angle)
    local newPosition = Vector3.new(newX, planet.Position.Y, newZ)

    -- Calculate direction vector between current and new position
    local direction = (newPosition - planet.Position).unit

    -- Update the planet's position
    planet.Position = newPosition

    -- Orient the planet towards the direction of movement
    planet.CFrame = CFrame.new(planet.Position, planet.Position + direction)
end

-- Run the updateOrbit function in a loop
while true do
    updateOrbit()
    wait() -- Wait for the next frame
end
1 Like