How to make a part orbit another part?

Its pretty simple what I want to do, I just want a part to circle another part, I’m not that good at CFrame and only know the basics.

How would I do this?

5 Likes

Do you want others to give you the script? Thats against the category rules!

No, didnt say that, just asking how I would do it

He never asked for the script, he just wants an idea on how to go about it

Oh, then you could maybe do a basic script that makes a hinge constraint that works as a servo change its goal, and then weld a part kinda far away to it, so it orbits

Orbits are a rather vague concept due to various methods of CFrame manipulation. To form a generic circular orbit, we need to define a couple of constants:

  • Origin; where does the orbit center around?
  • Radius; how far away is the object doing the orbiting?
  • Speed; how many radians does the object complete in one second?

If this takes place on the server, we should use RunService’s Heartbeat which runs every frame at 30Hz. We could use traditional infinite loops and waits, but server lag probably won’t keep the expected rotation consistent:

local origin; --a cframe representing the origin of the rotation
local r = 10; --radius is commonly represented as a lowercase r in geometrical calculation
local rps = math.pi; --abbreviation for radians per second. note that pi radians equals 180 degrees
local orbiter; --reference to object doing the rotation. should be anchored for decent results

local angle = 0; --beginning angle in radians because radians are awesome
game:GetService'RunService'.Heartbeat:Connect(function(dt) --note that dt is short for deltatime, a value representing the time it took since the last frame for this frame to begin
	angle = (angle + dt * rps) % (2 * math.pi); --add our rotation per second to our total angle after multiplying it by the deltatime to ensure the addition happens over the course of a second (if we didnt multiply this by the deltatime we would essentially be rotating by pi radians approximately every 30th of a second which is extremely fast). we should also ensure our angle value doesnt exceed 2pi radians (equivalent to 360 degrees) through modulus which isnt necessary but i believe to be good practice nonetheless
	orbiter.CFrame = origin * CFrame.new(math.cos(angle) * r, 0, math.sin(angle) * r); --set the cframe of the orbiter every frame. our trigonometrical results should be multiplied by our radius to extend the distance according to the value. the way i arranged the trig here causes it to rotate on the y-plane but this can be changed pretty easily.
end)

As @TDtheTV mentioned, you shouldn’t just half-heartedly use this script since I spent quite some time writing this post. Please use my comments to the fullest and try to understand what calculations I performed and what concepts I used.

28 Likes

Sorry if this is a stupid question, but how would I adjust the speed it rotates around? I tried changing the

the “2” number, but nothing happens…

Thanks for any help!

Edit:
I tried to change the rps to just 0.1, and it’s a lot slower now. However, it’s a little bit “jerky”, and “laggy”.

Sorry for the late reply. That portion of the code just constraints the angle cache to 2𝜋 (equal to 360 degrees) so that it remains at a smaller value.

What you did with the rps variable is correct, although I have no clue as to what causes the strange latency that you mentioned. I tested the script just now with that value and failed to notice any strange behavior whatsoever, so it may be an external factor. If you haven’t already, ensure that the orbiter is anchored and that there is no other script that tampers with the orbiter’s position.

1 Like

You could try my module. It is a simple and easy to use free module that contains circular orbits, eccentric orbits and elliptical orbits currently!

Get it here the module here nowm: Tomroblox54321’s Advanced Planetary Orbit Module. Hope you enjoy!

1 Like