Interpolation
lerp is short for "linear interpolation".
Interpolation is computing some middle value between a start and a finish value.
The amount to interpolate is usually between 0 and 1.
0 means at start
1 means at finish
0.5 means half way in between, etc.
Thus a sequence of :lerps can be used to make a smooth animation from a start to a finish:
start:lerp(finish, 0)
start:lerp(finish, 0.1)
start:lerp(finish, 0.4)
start:lerp(finish, 0.7)
start:lerp(finish, 0.9)
start:lerp(finish, 1)
What's wrong
In your script, you do not have a "start" and "finish"; part.CFrame is changing constantly, so it's not a good choice for :lerping in the simple case.
Normally, you'll fix your start and finish ahead of time, and then vary (in a loop) the amount to interpolate.
What to do
That means you'll have some kind of progress variable as the second parameter to the :lerp method, which is probably changing in a loop.
start, finish, and progress come together as start:lerp(finish, progress) to make a CFrame which is in-between.
You can just update part.CFrame with this in-between CFrame repeatedly, moving progress from 0 to 1 in order to move part.CFrame from start to finish:
local start = part.CFrame
local finish = part.CFrame * CFrame.new(10, 10, 10)
for progress = 0, 1, 0.03 do
part.CFrame = start:lerp(finish, progress)
wait()
end
Other Interpolation
Notice in my list at the top, I don't move "evenly" from 0 to 1? You don't have to do that.
You can move however you want, as long as you go continuously in the range 0 to 1.
For example, here's a way to go slow...fast...slow:
for time = 0, 1, 0.03 do
local progress = 1 - math.cos(time * math.pi)
part.CFrame = start:lerp(finish, progress)
wait()
end
Here's going fast and then slowing down:
for time = 0, 1, 0.03 do
local progress = 1 - math.exp(-time * 5)
part.CFrame = start:lerp(finish, progress)
wait()
end
This "exponential easing" is interesting, because there's another way to write this:
for time = 0, 1, 0.03 do
part.CFrame = part.CFrame:lerp(finish, 0.01)
end
Notice that the "start" value is constantly moving? This is really the same as the above; you're just changing the start instead of changing the progress. This is obviously much harder to understand/analyze but much easier to code.
3 comments
Sorry if I'm late, but is there a way to have more than 2 destinations? For example, start, middle, to finish and then loop back to start?
of course. A -- B -- C -- A is just A --> B, followed by B --> C, followed by C --> A.
It's cool that people are still seeing this great answer! :3