How to use Mathf.Lerp properly?

I feel so stupid asking this because i know about a hundred tutorials teaching about Mathf.Lerp, but i will be honest here; I still don’t understand. The “interpolation point” part is really hard to me.

I have a script to control the main player movement, and i need to slow the player down to a certain smaller value when he is holding down space. I would like to “lerp” my current movement speed to this smaller value over time.

I won’t post my whole script here, but i believe the part i will post is pretty self explanatory.

What happens is: When i call the function below, my two walkSpeeds variables slow down only a little bit. I know the error is on the interpolation part, and i really would like to know what i need to do to make this Lerp lasts for like 1 second (just an example).
Thanks and sorry for the stupid question.

 void startJump()
    {
        if (isGrounded)
        {
            //Works but it snaps to this value, and i would like to do it slowly.
            // walkSpeedHorizontal = walkSpeedHorizontalOrigin / 3;
            // walkSpeedVertical = walkSpeedVerticalOrigin / 3;

            walkSpeedHorizontal = Mathf.Lerp(walkSpeedHorizontal, 0.3f, Time.deltaTime * 5);
            walkSpeedVertical = Mathf.Lerp(walkSpeedVertical, 0.3f, Time.deltaTime/5);

        }
}

Lerp can be used for this but it’s not ideal.

Perhaps you are looking to this pattern to gradually reduce / increase your speed?

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

So your input would go from 0 to 100 instantly, but the actual motion of the player would slowly move to the desired speed.

1 Like

Yeah, I don’t understand why a lot of tutorials use Lerp this way. Even confused me when I was starting.

To put it simply, Lerp takes in a starting & ending value as the first two parameters, and the third parameter is the percentage between those two values within a range of 0.0 - 1.0:

//value1 = 50
var value1 = Mathf.Lerp(1, 100, 0.5f);

//value2 = 15
var value2 = Mathf.Lerp(1, 100, 0.15f);

//value3 = 80
var value3 = Mathf.Lerp(1, 100, 0.80f);

//value4 = 1
var value4 = Mathf.Lerp(1, 100, 0f);

//value5 = 100
var value5 = Mathf.Lerp(1, 100, 1f);

This is the same for all Lerp functions such as Vector3.Lerp, Quaternion.Lerp, etc…

4 Likes

I hate to be that guy but your math is ever so slightly off for value1, value2, and value3. Those are the correct results if the first parameter was 0.

3 Likes

:slight_smile: Right, though if you think of the floored value it would be correct.

// 0.15 --> 15.85
// 0.5  --> 50.5
// 0.8  --> 80.2

Of course it’s always less than 1 off since the lower bounds is “1 off” :slight_smile:

1 Like

Whoops. :stuck_out_tongue:

Thank you bro, your answer helped me a LOT. You always answers my questions lol, you have no idea of how much you have helped me. Thank you, i used Math.MoveTowards and now it is working properly.

1 Like

Yeah these tutorials are pretty confusing. The interpolation point is really hard, but the way tou explained it, i thik i got it. it’s like a a percentage,right? But still, what’s the point? The lerp will only lerp up to this percentage determined by the interpolation? I thought that this last parameter of the functions was about the speed in which the values would lerp, but i don’t think it is anymore.

Think about what would happen if you apply this Lerp operation once every frame over the course of many frames. Think about what value x would take on (assuming x starts at 0)
x = Mathf.Lerp(x, 5, 0.2f);