I've recently been trying to learn how the lerp
function in roblox works.
Here is my script,
local part = game.Workspace.SpawnLocation while wait(.5) do local x = part.CFrame:lerp(part.CFrame * CFrame.new(10,10,10), .5) x.CFrame = Vector3.new(x) end
At first I was trying it without the variable, but I read a little of the wiki and it said that the lerp function returns a value. So I tried making the variable and printing it. The output was this,
local part = game.Workspace.SpawnLocation local x = part.CFrame:lerp(part.CFrame * CFrame.new(10,10,10), .5) print(x) ---------------------------OUTPUT----- -44.2292328, 7.5, 49.8699265, 1, 0, 0, 0, 1, 0, 0, 0, 1
I also tried the loop because someone said you have to use a for loop or something, idk he's weird.
Any and all help would be awesome
Thank you.
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 :lerp
s 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)
In your script, you do not have a "start" and "finish"; part.CFrame
is changing constantly, so it's not a good choice for :lerp
ing 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.
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) -- start where we were; finish 10 studs up/right/back. -- over the course of ~1 second, vary `progress` from `0` to `1` for progress = 0, 1, 0.03 do -- consequently: -- over the course of 1s, vary `part.CFrame` from `start` to `finish` part.CFrame = start:lerp(finish, progress) wait() end
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:
-- "cosine easing" 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:
-- "exponential easing" 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.