# Programming in C#: Lerp

Lerping is a common method of changing a value over a certain percentage. This can be used to move an object from one point to another over a certain time period, filling a health bar a certain amount when they lose health, etc. Lerp stands for linear interpolation, moving between set points over a specific range.

This is commonly included in many libraries to be used, the basic way you would use it would be `Lerp(start, end, percentage)`. It’s pretty simple to understand; the start is the value we are beginning at, end is our destination value, and the percentage is a value from zero to one that says how far along from the start to the end we are.

# So What Is Lerp… Really?

So let’s take a glance into the math side of lerp, because that’s all it is. It’s a simple math formula. Don’t worry, it’s not very complex, and I will explain each part of it. The formula for a lerp is as follows:

`start + percentage * (end - start)`

So let’s take a look at this step by step, in the order of operations. For the sake of explanation, we will reference these numbers for example. Our `start` will be 10, our `end` will be 20, and we want the halfway value, so our `percentage` will be 0.5; the `result` should be 15.

So the first section we would take a look at is the `(end - start)`, in our case this would evaluate to `10`. We do this because we want to get our difference value from zero, the difference between the start and the end is 10, and so that’s what we’ve gotten here.

Next we need to get the halfway mark, so we multiply by our percentage, which is 0.5. So we take `10 * 0.5` and we’re left with 5. This is half the difference between the end and the start.

But now we need to shift it back into place, since we’re looking at these values from zero. So we add start, `10 + 5`, where 10 is our start value, and 5 is the result from above, and this leaves us with 15. Which is smack dab in the middle of 10 and 20.

You can use this for almost any values that you want to get a percentage between. Try changing the percentage and you can see what values you get.

# Common Mistake

There is a very common mistake I see people do when using lerp. As you read above, lerp has a solid start, a solid end, and a percentage. That way you can get a percentage value between them. However I’ve seen it used to slowly move toward a value over time, without ever reaching that value.

`value = Lerp(value, end, 0.2f);`

So this will happen, and they will call it over and over again, to move the value towards the end value. But it will never actually reach that value is the problem. Using our values from above, if we wanted to increase our value 5 times by 0.2 we would do this

```float start = 10;
float end = 20;
float percentage = 0;
float changeAmount = 0.2f; //How much we increase by each time

float value = start;
for(int i = 0; i < 6; i++)
{
value = Lerp(start, end, percentage);
percentage += changeAmount;
Console.WriteLine(value);
}

Output:
10
12
14
16
18
20```

Here we increase our percentage each time, and so it linearly plots out our points. Now what if we did this with the incorrect method shown above?

```float start = 10;
float end = 20;
float changeAmount = 0.2f; //How much we increase by each time

float value = start;
for(int i = 0; i < 6; i++)
{
value = Lerp(value, end, changeAmount);
Console.WriteLine(value);
}

Output:
10
12
13.6
14.88
15.904
16.7232```

Our values don’t come out right. Since we’re just multiplying by 0.2 each time, it’s just slowly getting closer and closer to the value, but never actually reaching it. It’ll get really close if we keep doing it, but mathematically never reach the end value.

# What If I Want A Curved Lerp?

There are ways you can manipulate your percentage value to create different curves or effects. Without changing the percentage value, you can change the value you pass into the lerp function to adjust it’s values.

For example you can make a `Ease In Cubic`  ease by simply using `Math.Pow(percentage, 3)`. There are lots of useful libraries on the internet you can find that have made these functions and done the math for you.

## Support

Are you having trouble with understanding this tutorial? Please feel free to contact me via email at KoseckCory@gmail.com or message me on discord at 7ark#8194.

I would love to get feedback from people so I can add and improve these tutorials overtime. Letting me know what you’re confused about will let me update the tutorials to be more concise and helpful.

If you’re interested in supporting me personally, you can follow me on Twitter or Instagram. If you want to support me financially, I have a Patreon and a Ko-fi.