How to make a part move within a radius of a circle around another part

Can I see what modifications you made to my code? It will always do this at runtime with the current setup, but then won’t again. Is it really that critical to have it not jump at runtime when the server starts when the player likely won’t even see it?

When looping, the displacement between each angle is about 1 because if your 36 degrees, you go to 37 degrees. However, when you start, the displacement is like 180 because the initial angle isn’t at 0. Instead of setting it to 0 to begin with, you can find the real initial degree, but I gotta do some thinking to figure out that.


This is all I did.

When declaring degree for the first time you set it to 0, but thats not really the correct degree. You can find the correct initial degree by doing something like this I think:

local rise = partA.Position.Z - partB.Position.Z
local run = partB.Position.X - partA.Position.X
local degree = math.deg(math.atan2(rise, run))

This still throws the part to the same area, hm.

To clarify his post, this worked for me.

local originPart = CENTERPARTHERE -- change
local orbitingPart = MOVINGPARTHERE -- change
local rise = originPart.Position.Z - orbitingPart.Position.Z
local run = orbitingPart.Position.X - originPart.Position.X
local angle = math.atan2(rise,run)

local radius = (originPart.Position - orbitingPart.Position).Magnitude

while true do
	local newCFrame = originPart.CFrame + Vector3.new(radius * math.cos(angle),0,radius * math.sin(angle))
	orbitingPart.CFrame = newCFrame
	angle = angle + math.rad(1)
	task.wait()
end
3 Likes

It seems to throw it to the complete opposite side now rather than what it did before. How strange.
image

The one marked in black is the original position.

Hm, I tried mine and it didn’t flip. But when I rotated both parts, it did.

I’ll see if I can figure out how to fix this.

Try using a hingeconstraint with a motor instead of coding it in lua.

https://create.roblox.com/docs/tutorials/building/physics/building-a-hinged-door

Thank you for your help so far.

Thank you as well, MrNobodym8 and everyone else responding - it means a whole lot. Hopefully a solution can be found soon.

Yeah - I’ll have to think on it. Can’t think of a way to fix this right now. I feel like I’ve done this before! That’s one of the reasons it’s so infuriating for me.

Take your time, hopefully this works out.

Well, I think I got it working. Sorry for the late response, I’ve been busy and I haven’t had time until now.

local originPart = YOURCENTERPARTHERE -- change
local orbitingPart = YOURMOVINGPARTHERE -- change
local rise = -math.abs(orbitingPart.Position.Z - originPart.Position.Z)
local run = -math.abs(originPart.Position.X - orbitingPart.Position.X)

local angle = math.atan2(rise,run)

local radius = (originPart.Position - orbitingPart.Position).Magnitude

while true do
	local newCFrame = originPart.CFrame * CFrame.new(radius * math.cos(angle),0,radius * math.sin(angle))
	orbitingPart.CFrame = newCFrame
	angle = angle + math.rad(1)
	task.wait()
end

But it only works two ways, before it works with 1 rotation, now it works with two. This is quite annoying. :man_facepalming:

2 Likes

Works perfectly - thank you so much!

Oh, nevermind.

Unfortunately - it only works within that one area, what I’m intending to do is have it work from any position and still move around within a circle.

Yeah - I knew about that. Sorry for the late response. If I have time I’ll see if I can fix it soon…

Thank you, genuinely thank you so much for your help. Take as much time as you need - I don’t mind.

have you came up with the solution?

Works perfectly fine, but is there a chance I can change the speed of orbiting?

um…
just use a hinge constraint