# Object orbiting a part

I’m looking to figure out how to get a part to rotate around a moving position. I don’t mean rotating in place, I mean more like an orbit.

Something like this:

I used body movers to make it. Like this:

Any idea on how to do this? Maybe use some kind of CFrame operator?

I made a post similar to this awhile back (similar problem, I already had the orbiting part). I just needed to figure out how to alter the inclination angle.

Wow. The code is confusing to me; I’m not very good at math. What do `math.sin` and `math.cos` do/refer to? I haven’t learned trig yet, so I don’t really have a good understanding of the code.

Edit: some relevant background info on trig would’ve been helpful in understanding this lol. If you imagine walking around the Unit Circle, the distance to cover the entire circle is TAU (2*math.pi) or ~6.2832.

Just pi alone is half of a circle, pi/2 is a quarter of a circle, etc. This is why the loop is `for i = 0, tau, increment do` and the increment is divisible by TAU.

`math.cos` is used to get the X value and `math.sin` is used to get the Y value (both return a number between -1 and 1).

Edit: As for why `cos` and `sin` return a number between -1 and 1 is because of this:

If you look at the image above, the brown arrow points to where you start “walking” on the unit circle. Because `math.cos` is just giving you the X value, you’re at a distance of 1 unit away from the origin (the center). If you printed `math.cos(0)`, you’d get 1. If you printed `math.cos(math.pi)` you’d get -1. Because math.pi is half the distance of a full circle, so it’s at (-1, 0) in the picture above. `Cos` is just reading that X value of -1.

You can imagine if you do this:

``````local tau = 2*math.pi -- mathematical constant
local increment = tau/60 -- for the loop

for i = 0, tau, increment do -- start at 0, go the full length of a circle, at an increment of a 60th of a circle each iteration
print(math.cos(i), math.sin(i))
end
``````

`math.cos(i)` and `math.sin(i)` will give you the correct X and Y values to place the part to. To get a part to follow a circular path and point at the origin you could do:

``````local part = script.Parent
local tau = 2*math.pi -- mathematical constant
local increment = tau/360 -- for the loop
local origin = part.Position
local radius = 5 -- how far away the orbit is

while true do -- loop infinitely
for i = 0, tau, increment do -- main for loop like in the example
local pos = Vector3.new(math.cos(i), 0, math.sin(i)) * radius
part.CFrame = CFrame.new(origin + pos, origin)
end
end
``````

And you’ll get this result:
https://gyazo.com/193cbac40005fef2c012e056b0dfff31

Edit: I’ll try and get a visualization of this working so you can sort of see what I mean by `sin` and `cos` going from -1 to 0 to 1 and vice versa.

1 Like

If the part rotates at a constant offset to the center part you could simply maintain this offset in accordance to the position of the center part and rotate the part around the y-axis accordingly.

``````local centerPart = workspace.PartA
local rotatePart = workspace.PartB

centerPart.Position += Vector3.new(1, 0, 0) --shift 1 stud along the x-axis every frame
rotatePart.Position = centerPart.Position + Vector3.new(0, 0, 0) --this vector3 should represent the constant offset the rotating part has to the center part
rotatePart.Orientation += Vector3.new(0, 1, 0) --rotate 1 stud around the y-axis every frame
end
end)
``````

I’ve figured it out. I used some CFrame stuff, here it is!

Thanks for all the help and info!

1 Like

Using your model as an example, is there a way to make it slower?

You’d edit the wait time in the loop to make it slower.

1 Like