Evenly Spaced Orbitting Parts

I have a script that makes a ball orbit the player when it’s touched. As more balls orbit the player, I want it to evenly space around the player. How would I do this?

local touched = false
script.Parent.Touched:Connect(function(char)
	if touched == false then
		touched = true
		wait(0.01)
		
		local center = char.Parent.HumanoidRootPart
		local moon = script.Parent
		script.Parent.Parent = workspace[char.Parent.Name]
		
		local ORBIT_TIME = 2
		local RADIUS = 5
		local ECLIPSE = 1
		local ROTATION = CFrame.Angles(0,0,0)


		local sin, cos = math.sin, math.cos
		local ROTSPEED = math.pi*2/ORBIT_TIME
		ECLIPSE = ECLIPSE * RADIUS
		local runservice = game:GetService('RunService')
		
		local rot = 0
		game:GetService('RunService').Stepped:connect(function(t, dt)
			rot = rot + dt * ROTSPEED
			moon.CFrame = ROTATION * CFrame.new(sin(rot)*ECLIPSE, 0, cos(rot)*RADIUS) + center.Position
		end)
	end
end)
1 Like

You need a general offset for each ball, which is inversely proportional to how many balls there are in the system.

You can find that by doing this…

local offset =  i / number_off_balls * math.pi * 2

Once you have found the balls’ general offset, you can do your normal rotation with it but accounting for the offset


ball.CFame = ROTATION * CFrame.new( sin(rot + offset) * ECLIPSE, 0, cos(rot + offset) * RADIUS ) + ...
1 Like

I made it so that it checked how much it needed to be offset and offset it when it started and it worked.
The problem now is it starts at that position without considering the other balls already orbiting, making it uneven.
image