This makes them easier to write, easier to manage and, once they’re finished, they stop themselves.
In this example, Lerp is being used to return a float, but it’s also possible to Lerp other values, such as colours and Vector3 values (more on that later). Here’s what it looks like in scripting: lerpValue = Mathf.Lerp(minValue, maxValue, interpolationPoint) Essentially, acting as a percentage between a and b.Īn interpolation point of 0 (0%) would return the minimum value (in this case also 0) while an interpolation point of 0.5 (50%) would return a value halfway between the minimum and maximum value (in this case, 50). The interpolation point that’s passed in is a float value between 0 and 1. The value that’s returned is defined by a third value, the interpolation point ( t) which returns a point on the scale between a and b. The Lerp calculation returns a value from a known range, which is specified using a minimum and maximum value ( a & b). It’s how it’s used that often becomes complicated and confusing. The first thing that you need to know about Lerp, that will make it much, much easier to use, is that the basic Lerp calculation is actually very simple.
Let’s start with the basic method… How to use Lerp Lerp slowing down at the end (how to stop it).In this post, I explain the basics of Lerp in Unity, when to use it and how to easily write a Lerp function (as well as how to add special effects, such as easing) with examples that you can use in your project. Or, if you’re anything like me, you might occasionally forget how to use Lerp and need to look it up again every time you need to use it.īut don’t worry, because that’s exactly what you’ll learn on this page. Surprisingly, it’s often misunderstood and can easily be used the wrong way, especially if you’re just getting started in Unity.Īnd, while this isn’t always a problem, it can lead to some unexpected results.įor example, it might mean that you can’t recreate the effect you want, or the Lerped animation may appear broken, or buggy or may not work at all. It’s an incredibly common feature of Unity, and development in general, Most commonly it’s used for moving or changing values over a period of time. Lerp, or Linear Interpolation, is a mathematical function in Unity that returns a value between two others at a point on a linear scale. Very often in Unity, you may want to animate a button, move an object to a new position, smoothly fade audio or graphics or, perhaps, change the colour of something gradually over time.Īnd for those tasks, as well as many others, you’re probably going to need to use Lerp, one way or another.