- Home /

# Explanation of Lerp()?

Ok if someone can please explain it, because I'm having trouble understanding it. I am not sure how my calculations aren't similar to the effect in Unity when i run the project. As i read the linear interpolation takes is based on formula (y-y0)/(x-x0) = (y1-y0)/(x1-x0) given we have two point A(x0,y0) and B(x1,y1). Also I have found that for singular values formula for Lerp is x0 = from, x1 = to, t = fraction so the formula is **x0 + (x1-x0)*t**. So basically I want to use Lerp between values 0-90. Based on Unity documentation on Lerp and also the tutorial example I've seen they use the Time.deltaTime as t usualy multiplied with some smooth factor. My question is next:

```
float startValue;
float resultValue;
float smooth = 4f;
void Update()
{
if(Input.GetKeyDown(KeyCode.A)){
resultValue = 0f;
toValue = 90f;
}
resultValue = Mathf.Lerp(resultValue, toValue, smooth*Time.deltaTime);
}
```

The Time.deltaTime is as I get it now around ~0.02 sec multiplied with 4 that's 0.08~0.09. Before trying to apply the formula, based on documentation and some posts I read the value of t is between 0-1. If it's 0.5 it returns half way of between values, so between 0 and 90 it should return 45. If t=0.08 is that 8% of the 90? If that so it should be 7.2 for first iteration of Update(). Instead I get value of 5.96?!? Next I tried to use formula it is the same as my fist thought. So how does it work? Also I've noticed there is a few scenarios of using the Lerp(), one like this that I'm using, the one where you have start time then calculate t based on maxValue/currentValue, and I think there was another one but i cant remember now.

Thank you in advance, cheers!!! :)

**Answer** by Peter G
·
Sep 09, 2013 at 03:18 PM

The general equation for linear interpolation is your second one.

```
Output = (1 - t) * Input1 + t * Input2
```

It can nicely be expanded to higher degrees (see Bezier curves) and vectorized. Just treat the vector as a collection of parallel interpolations.

As to why your value is slightly off, your `Time.deltaTime`

for the first frame was probably just a little faster than 20ms. Really I wound't focus that much on it unless you need exactly 8%.

If you need to calculate `t`

, then Unity has a built-in fuction for that:

```
t = Mathf.InverseLerp ( Input1 , Input2 , Output);
```

and the actual calculation for that looks something like:

```
t = (Output - Input1) / (Input2 - Input1)
```

**Answer** by Kavorka
·
Sep 09, 2013 at 03:43 PM

Time.deltaTime is not fixed to 0.02 seconds. It depends on the frame rate. Your Time.deltaTime was about 0.015 seconds and your frame rate was about 68 frames per second. Lerp works as you explain but not Time.deltaTime.

**Answer** by MDarkwing
·
Sep 09, 2013 at 04:37 PM

Thanks guys, @Kavorka yeah i got that, that was indeed my miscalculation after i checked it. :D Either way I read some more posts and explanations and now I totaly get it what happens in Lerp based methods.

@Peter G, thank you man for that additional info it's so damn useful, and for real formula also :)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How does the Unity Karma and Rank work? 1 Answer

how "Animation" work? 1 Answer

How to work out system requirements 1 Answer

How To Use Slerp 1 Answer

Moving player in an arc, from startPoint to endPoint 2 Answers