Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make this move at the same speed?

function lerp(startpoint,endpoint)
	local part = Instance.new('Part',workspace.clerptest)
	part.BrickColor = BrickColor.Random()
	part.Anchored = true
	part.CFrame = startpoint
	while true do
--		wait(1)
		for i = 0,1,.01 do
			wait()
			part.CFrame = part.CFrame:lerp(endpoint,i)
			print(part.CFrame)
		end
--		wait(1)
		for i = 0,1,.01 do
			wait()
			part.CFrame = part.CFrame:lerp(startpoint,i)
			print(part.CFrame)
		end
	end
end
lerp(workspace.clerptest.A.CFrame,workspace.clerptest.B.CFrame)

just learned what lerp was and sort of how to use it. It seems it can only move it like 'quad' when tweening guis and I want it to move at the same speed so it doesnt look choppy in the middle without using renderstepped:wait()

1 answer

In your code, at each point in time, you are interpolating between where it is and the target. This is not what you want.

Consider an example moving from 0 to 10 in 10 steps.

First, we're at 0. We move to 10% between 0 and 10, which is 1.

Now we're at 1. We move to 20% between 1 and 10, which is 2.8.

Now we're at 2.8. We move 30% between 2.8 and 10, which is 4.96.


Notice that this is not what you want. What you want is to always be looking at where you started and where you're going:


First, we're at 0. We want to move to 10% between 0 and 10, which is 1.

Now we're at 1. We move to 20% between 0 and 10, which is 2.

Now we're at 2. We move to 30% between 0 and 10, which is 3.


for i = 0, 1, 0.01 do
	wait()
	part.CFrame = startpoint:lerp(endpoint, i)
end
for i = 0, 1, 0.01 do
	wait()
	part.CFrame = endpoint:lerp(startpoint, i)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.