# 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!

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

Lerp interpolates from one value to another over a given range. A pretty good tutorial video on this topic can be found here:

https://vimeo.com/channels/151501/17070181