In this tutorial I will go over 'Lerping' in Unity and how to use it in your code. There are a variety of lerping methods that can be used in Unity. Mathf.Lerp allows you to lerp between two float values, Vector2 and Vector3 allow you to Lerp between two vectors and Color.Lerp allows you to lerp between two colors. Lerping is a linear interpolation between two values. This means that it does increase or decrease in value by more than some delta whereas delta is the change in value. In this tutorial we will go over each different lerping functoin and how to use it in Unity.
Mathf.Lerp can be used to interpolate between any two float values. You can use it for a variety of things such as moving a character in one direction or changing a single color variable as we will do in the example below. In the example below we are changing the shade of red that the a gameobjects material has. Notice that the first value in the Lerp function is the minimum value or start value. The second parameter in the Lerp function is the destination value which we would like to get to. The third and last parameter is the percentage as a float that we are to achieving the final value. This is important to note that it is a percentage between 0.0 and 1.0f. The last if statement in the code is simply swapping the minimum and maximum so that we can go back and forth. Also note that Color is denoted as a value between 0 and 1 (This killed me for about half an hour as I couldn't figure out why my color was always at 255).
Vector2.Lerp / Vector3.Lerp
Vector2.Lerp and Vector3.Lerp require a bit more calculation to be used effectively. The code below will cover how to use Vector2.Lerp to move a sphere from one corner of the screen to another. Vector3.Lerp works the same way, just with three variables instead of two. I will go through each part as it is pretty extensive.
This first part is located just below the class declaration and is all of the variables that we will be using in this script. StartPosition is the position that our sphere will start in. DestinationPosition is the coordinates of where we would like to the sphere to end up. The next four float values are defined just to make it easier to change the start and destination positions. Speed is how fast the sphere will move. StartTime is the time at which we start moving. TravelLength is the distance that we need to trave from start to destination. Distcovered is how much distance we have currently covered. And fractravel is the fractional value of the amount of distance we have covered, also known as our t.
The next part that we will cover is the start initializations and calculations. The first two lines are setting our start and destination position based on the mins and maxes that we set above. The third line is setting our gameobjects start position to the position that we listed above. The next line sets our start time to the current value in Time.time. The next line defines the total distance that we must travel to get from start to finish.
The final part that we will cover is the bread and butter of the movement script. We will cover it line by line so pay close attention to what is happening. The first line calculates how far we traveled time wise and then multiplies that by our speed. The next line sets our fracTravel equal to the time distance covered divided by the total length that we have to travel. This is otherwise known as our 't' value. Remember that it must be between 0 and 1. The next line updates our position using the Vector2.Lerp between our start position, destination position and the fraction of our journey that we are on. The final contents of the if statement are doing a similar function to the color swap that we used in the color changer above. Therefore, I will not go over it again here, just know that we are moving back.
The final Lerp that we will cover is the Color.Lerp function. This function is used to interpolate between two color values. First, we define two colors; red and blue for this tutorial. Then we define a Color variable for our lerpedValue. Then in the update method, we set the lerpColor using the Color.Lerp function. We also introduce a new function to replace the if statement that we were using before on the t. Mathf.PingPong will bounce the numbers back and forth once we reach 1. Finally we set the gameobjects color to the lerpedColor.
For more tutorials, follow us on Twitter and Youtube!