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))
task.wait()
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)
task.wait()
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.