Can I control the speed of lerp?

Hey guys,

I have a question about lerping. When I use the lerp function, how do I control the speed of an object to its destination. It seems that when I try to move one character to another position, I have very limited control over the speed they can move from one area to the next. I am programming a navigation system from scratch just in case lerp doesn’t give me that functionality, but why recreate the wheel if it already exists.

Alexis

Lerp is a way of blending between two numbers or vectors. It’s picking a point along a linear graph. You use a number (often called alpha) to choose how far between the two values you want to be.

So if you are lerping a value x between 5.0 and 10.0. You have an alpha that varies from 0 to 1.

At alpha = 0.0 we’re at the “start” so x = 5.0
At alpha = 0.5 we’re in the middle so x = 7.5
At alpha = 1.0 we’re at the “end” so x = 10.0

It’s linear from 5.0 to 10.0.

So, to answer your question, to change the speed at which the lerp occurs, you change the speed at which you modify alpha.

e.g.

var LERP_SPEED = 2.0;

initialize: function () {
    this.alpha = 0;
    this.start = 5.0;
    this.end = 10.0;
},

update: function(dt) {
    this.alpha += LERP_SPEED*dt;
    var value = pc.math.lerp(this.start, this.end, this.alpha);
}
1 Like

Thanks, I do this all the time in Unity, but I have no idea why I could not wrap my head around the way Play Canvas did it. I think the 1 - 0 threw me off a bit. Thanks for the explanation.

Hi, @Gariantroll, I need help on lerp topic. I am currently trying to do a simple application in which an object will move to target after raycast on target. But I have no idea on how to do it. Based on the topic above, may you show me your code for reference? Thousand Thanks…

Hi @wang_hoon - it looks like @Gariantroll didn’t reply, but perhaps this thread will help you:

Let me know if this doesn’t help. :slight_smile:

I realize this thread is a bit old, but I have a small question about lerping I thought would be easier to find an answer to here rather than make a new one.

The question is: would this work the same way with lerpAngle?

from https://developer.playcanvas.com/en/api/pc.math.html#lerpAngle

Calculates the linear interpolation of two angles

So yes, it has the same behavior as math.lerp but ensuring that interpolation is correctly performed across the 360 to 0 degree boundary