Move part between two other moving parts

I have a rotating cube and I want to have some moving parts on the cube that also rotate with the cube. Currently, to achieve this, I am using a for loop with lerp. The start and end parts are attached to the cube so they rotate with the cube.

local MovingPart = script.Parent
local Start = MovingPart.Parent.Start
local End = MovingPart.Parent.End
--parts are in a model

while true do
	for i = 0, 1, 0.005 do
		wait()
		MovingPart.CFrame = MovingPart.CFrame:Lerp(End.CFrame, i)
	end
	
	for x = 0, 1, 0.005 do
		wait()
		MovingPart.CFrame = MovingPart.CFrame:Lerp(Start.CFrame, x)
	end
	wait()
end

This does move the part between the two points and it rotates with the cube but it doesn’t move in a straight line, which is what I would like, when the cube rotates too quickly.

I’ve tried using Prismatic Constraints so the part only moves on the line but when I unanchor the moving part for the constraint to work, the moving part shakes uncontrollably.

I’ve also tried using BodyMovers and it didn’t work (probably because of my lack of knowledge on them so I am still open to trying them out).

I’m out of ideas so if you have any suggestions or any other method of doing this I’d like to hear them. :slightly_smiling_face:

I did this quickly, I hope it was useful.
(maybe the calculation of t is not correct. you should check it)
Baseplate.rbxl (21.0 KB) UPDATED

2 Likes

This is the problem. Lerp from Start.CFrame, not MovingPart.CFrame:

MovingPart.CFrame = Start.CFrame:Lerp(End.CFrame, i)

(and vice-versa for the second one).

Of course, that will make it linearly translate from Start to End, but you probably want it to be smoother.

I would personally do that by turning the linear function into a cosine interpolation like this:

local function CosineInterpolate(i)
    return (1 - math.cos(math.pi * i))/2
end

-- ...

        MovingPart.CFrame = Start.CFrame:Lerp(End.CFrame, CosineInterpolate(i))
2 Likes