How to lerp to another part over a given set of time

I want to lerp PartA to PartB in 2 seconds.

This was my FAILED attempt:

Duration = 2
for i = 0, 2, 0.1 do
	wait(0.1)
	workspace.PartA.CFrame =  workspace.PartA.CFrame:Lerp(workspace.PartB.CFrame, i/Duration) 
end

if ur wondering why im not using for loop, is because if the delta time was 0 it would just skip the entire thing which is annoying

I’m not too much of an expert with coding but this video may be able to help you!

It doesnt show me how to lerp a part to another part in a given set of time unfortunatly

Sorry! I am still learning quite a bit about scripting. Good luck!

1 Like

You see, theres a problem. Lerp moves a CFrame to the end CFrame using alpha (which is like a percentage), since you dont use for loop and instead use repeat, the distance between the Original CFrame to the End CFrame (or X’s cframe) decrease overtime, and the alpha percentage value increase, so i suggest you use for loop or use a code that takes the original cframe and lerp it.

local Duration = 10

local StartTime = tick()
local EndTime = tick()
local Delta = 0

local i = 0
local originalCF = workspace.Part.CFrame
repeat 
	i += Delta
	workspace.Part.CFrame =  originalCF:Lerp(workspace.x.CFrame, i/(Duration))
	StartTime = tick()
	task.wait()
	EndTime = tick()
	Delta = EndTime-StartTime 
	print(i/Duration)
until i >= Duration
1 Like