Using Lerp in Unity

As a Unity game developer, I need to create smooth animations, movement, and transitions all the time. I don’t want my character to instantly teleport from one spot to another. And, I need my hit effect to transition in and out, not switch on and off.
To make smooth transitions, I use the Lerp method.
What you need to know
- Lerp helps you to transition from one value to another value.
- Lerp takes three inputs. The initial value, the final value, and a percentage.
- The percentage tells lerp where the result should land between the initial and final value.
- To use lerp with a float value, call
Mathf.Lerp(float initial, float final, float percentage)
What you’ll learn in this article
If you want to create a great game, you need to use smooth transitions and animations.
In this article, I’ll tell you everything that I know about the lerp function.
If you stick through to the end, you will learn what Lerp is. You’ll see how it works. And, you’ll have practical examples of how to use it in your Unity projects.
What is Lerp?
Linear interpolation, or Lerp, is a math function. It calculates the intermediate values between one value and another. In Unity, you can use this function to transition between two values over time.
I’ll give some examples. You can use Lerp to smoothly move a game object. You can use it to gradually change the color of a material. And, you can use it to slowly scale an object’s size up or down.
If you want to linearly interpolate a rotation, you should use the Slerp method.
What parameters does Lerp take?
The Unity Lerp function, Mathf.Lerp() takes three parameters.
- Start value
- End value
- Interpolator
The function then returns the interpolated value. The interpolated value is a single value between the start and the end values. That value is based on the interpolator parameter.
What is an interpolator value?
Sometimes, you will see the interpolator referred to as the “time” parameter, or the “percentage” parameter.
What you need to know is that the interpolator is a value between zero and one.
- If the interpolator is 0, lerp returns the start value.
- If the interpolator is 0.5, lerp returns the midpoint between the start and end values.
- If the interpolator is 1, lerp returns the end value.
How has Unity implemented lerp?
Unity has implemented lerp for various classes. You can find lerp implementations for:
Create a free account, or log in.
Gain access to free articles, game development tools, and game assets.