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

bumping since i urgently need help

I would try something like this:

local degree = 0
local function rotatePart()
	local center = centerPart.CFrame
	local angle = CFrame.Angles(0,math.rad(degree),0)
	local offset = CFrame.new(0,0,100) -- where the part is from the center
	local rotatedCFrame = center * angle * offset
	part.Position = rotatedCFrame.Position
	degree += 1
end

you’ve got the sin/cos part right you just need to store a variable outside the function, or have it take a parameter.

local t = 0
function circle()
	partA.CFrame += Vector3.new(math.sin(t), 0, math.cos(t))
	t += 0.1
end

currently trying this, what exactly do i apply to the part? i tried applying position to the position variable and it just gave me an error

Im dumb Its not the position but the CFrame Use the calculated CFrame and do CFrame.Position to apply to the part.

the part jumps super far away (being the one all the way in the back), plus it doesn’t really move anywhere else afterwards
image

is there something i forgot to do?


(the while true loop is to test it moving around so i can then control it after)

Here, I made one for you.

local originPart = YOURCENTERPART -- change
local orbitingPart = PARTTOMOVE -- change
local radius = 25
local angle = 0

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

Here is a video of the script in progress


Hope this helps!

8 Likes

I tested the same code I got the desired result, just made the offset closer so you can see.
robloxapp-20220926-1309487.wmv (185.1 KB)

I think the problem is that you are waiting 2 seconds between each iteration. It is rotating, but very slowly. I would suggest increasing the degree by more than 1 or calling the function more frequently.

It seems almost completely perfect, it’s just the only problem is that it jumps to the other side of the part it’s moving around, is there any way to change this?

Give my script a shot, just change the wait time, and it might work.

I did, it results in the same result where it jumps to the other side.

Can you send me a screenshot of the issue?

I dont think wait time is the problem anymore. It sounds like the axis is wrong. Just replace the math.rad(degree) into a different axis when creating the angle variable.

Look at the video I showed above, it works fine there. Something else has to be wrong with it.

This seems to be the main issue, where it just jumps toward the other side (I think depending on the z angle?)

Is there anything more I should show?

You need to change the radius variable.

Actually, here’s a new script that automatically updates the radius to the original distance between the two parts.

local originPart = YOURCENTERPARTHERE -- CHANGE
local orbitingPart = YOURMOVINGPARTHERE -- CHANGE
local angle = 0

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
2 Likes

Just tried now, similar result for some reason.

I’m asking this since I wanna set it up where no matter where the part’s position is, it’ll still loop around the circle without jumping anywhere.

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?