- Home /
How the heck does Mathf.Lerp work?
What does it exactly do, the scripting reference doesn't quite give me the best explanation. I'm trying to get it to move a character from "point a" to "point b" over a set amount of time. However it doesn't seem to work like that... Anyways can someone sum up what exactly lerp is and how it works? Thank!
I like the title you gave to your question. The docs fail epically and it's frustrating.
Answer by Kleptomaniac · Apr 09, 2012 at 02:45 AM
Yes, you're right. The docs on Mathf.Lerp are horrible. I'm guessing you're using Time.time as your t parameter like in the docs? Bad idea.
Basically, lerp stands for linear interpolation. It means that the value will change from your from value to your to value over t. The t parameter is set within a range of 0 to 1, 0 being your from value, 1 being your to value. Therefore, 0.5 will be halfway between.
In this way, if you were to set it up like this:
var tParam : float = 0.5;
var valToBeLerped : float = 0;
valToBeLerped = Mathf.Lerp(0, 3, tParam);
valToBeLerped would equal 1.5. This means that you can have a changing t parameter to have a value which linearly interpolates between two values. So like this:
var tParam : float = 0;
var valToBeLerped : float = 0;
var speed : float = 0.3;
if (tParam < 1) {
tParam += Time.deltaTime * speed; //This will increment tParam based on Time.deltaTime multiplied by a speed multiplier
valToBeLerped = Mathf.Lerp(0, 3, tParam);
}
This will gradually fade the valToBeLerped variable between 0 and 3 proportional to t.
The problem with using Time.time, however (as it is used in the docs) is that, since the t parameter is based on a range between 0 and 1, and Time.time is the amount of seconds since the startof runtime, your Lerp will only ever work once in the very first second of the game. Therefore, it is best to stay away from Time.time in Lerp functions, unless of course you only want your Lerp to work in the first second of the game for some reason.
This can be helpful ... although it's not directly compatible with Unity's Mathf class, it will still maybe help you to understand how Lerp actually works better than the Script Ref can. Also have a look at the Wikipedia definition of linear interpolation to gain a better understanding of the graphical meaning of lerping.
I hope that helps, Klep
No worries! I had trouble understanding Lerp at first ... so I'm glad I could help someone out. :)
such a great explanation. Unity Docs makes it real difficult to understand. This has been bugging for me years. Thank you soooooooo much. Look at how many "O"'s I used. You know I mean it,
Hi, would it be possible to convert this into c# please. Thank you in advance :)
Answer by kolban · Apr 09, 2012 at 02:39 AM
Lerp interpolates from one value to another over a given range. A pretty good tutorial video on this topic can be found here:
I wish UT had time to add such awesome examples and explanations to their documentation :)
although i am running into an issue with this lerp works fine in Update, but not in a function call other than update as the function only gets called in 1 frame. lerp happens over many. if my understanding is incorrect, could someone please correct me, i find Lerp Doc's to be very messy although other documentation is rather very nice.
Lerp does not happen over many frames. It's a very basic math function, that returns a value immediately. If you want progression, you need to call it repeatedly while increasing (or decreasing) the value of t, the third parameter.
Hey, I read a similar answers from you back in teh days. I know this post is old but is still on the very top of google searches for Lerp. While the objective of lerp is to linearly interpolate between two known values using an increasing variable "t". There are other situations where t may have a given value and one of the values is the one that varies. I can´t find your answer now but you whent against a time.deltatime like value in t somewhere in Unity Answers. Its true that it defeats a bit the purpose of the fuction but there are situations where you whant to smoothen the movement (interpolate) of a thing where the positions you whant to interpolate are not known and t is used just as time.deltatime*speed. And to me that is perfectly ok. (this all running in Update). Funny enough the examples in unity video tutorials use the time.deltatime for t when interpolating the ball. It is pretty common to see the deltatime approach in camera smoothing scripts etc.
It's not O$$anonymous$$, because it makes the movement framerate-dependent. Unity tutorials aren't known for always being correct when it comes to coding.
Lerp(a, b, t)
is essentially shorthand for a*(1-t) + b*t
where t
is clamped between 0 and 1. It does only that, nothing else.
float c = Lerp(a, b, t);
float d = a*(1-t) + b*t;
// c == d, assu$$anonymous$$g t is between 0 and 1
Your answer
Follow this Question
Related Questions
how to use time on lerp 2 Answers
Vector3.Lerp completing before t = 1? 2 Answers
Vector3.Lerp moves in the wrong direction 1 Answer
Vector3.Lerp isn't working 3 Answers