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

I am trying to achieve a script where a part moves around another part (not within a loop, it’s a function that whenever called, slightly moves the part around the other part). For example, once it’s called, it’ll move slightly within a circle around the other part.

I’m, not sure exactly how to wrap it around the part. The issue is that I wanna make it move around the part in small steps, but I’m not quite sure where to go from here.

I’ve been looking around on DevForum for help and I’ve gotten no result.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local partA = workspace.PartA -- the part that moves
local partB = workspace.PartB -- the part that part a goes around

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

Any help would be much appreciated - I’ve been stuck on this for a while. Thank you for reading.

Edit: If there’s anything I’ve been obscure about - please let me know. I can detail further if needed.

1 Like

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.