Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How am I using lerp wrong?

Asked by 8 years ago

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
I'm aware I'm not using waitforchild, but that's not the problem, so don't answer or comment saying that :P

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.

1 answer

Log in to vote
6
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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:

  1. start:lerp(finish, 0)
  2. start:lerp(finish, 0.1)
  3. start:lerp(finish, 0.4)
  4. start:lerp(finish, 0.7)
  5. start:lerp(finish, 0.9)
  6. 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)
-- 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

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:

-- "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.

0
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? flufffybuns 89 — 8y
1
of course. A -- B -- C -- A is just A --> B, followed by B --> C, followed by C --> A. BlueTaslem 18071 — 8y
0
It's cool that people are still seeing this great answer! :3 OldPalHappy 1477 — 8y
Ad

Answer this question