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);
}
}
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…
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.
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.
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);