In game development, one of the most common problems is the smooth transition of objects from one position to another. A linear interpolation or Lerp
function is a popular technique used to achieve this in Unity. In this blog post, we will explore the lerp function in Unity and its implementation.
What is Lerp Function?
The Lerp function stands for linear interpolation
.
The function returns a value that is a linear interpolation between the starting and ending values, based on the weight parameter. It takes three arguments:
- The starting value
- The end value
- A weight between 0 and 1 representing the percentage of the value between the start and end values.
In case of linear transformation, the Lerp function calculates the position along the line that corresponds to the input value. When the input value is 0, the function returns the starting position, and when the input value is 1, it returns the target position. For values between 0 and 1, it returns a position that is between the two endpoints.
Implementation of Lerp Function
Unity provides a built-in Lerp function that can be used to interpolate between two positions. The function is called Lerp
and can be called using the following syntax:
Vector3.Lerp(startPosition, targetPosition, t);
This function takes the startingPosition
, targetPosition
, and a value t
between 0 and 1 representing the percentage of the distance between the two positions. It returns the position that is t
percent of the way from the starting position to the target position.
Transform example using Lerp function
Suppose we have an object that we want to move from its current position to a new position over a period of 2 seconds. We can use the Lerp
function to achieve this by calculating the position of the object at each frame using the following code:
public Transform target;
public float speed = 0.5f;
private float startTime;
private float journeyLength;
void Start()
{
startTime = Time.time;
journeyLength = Vector3.Distance(transform.position, target.position);
}
void Update()
{
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(transform.position, target.position, fracJourney);
}
In this example, we store the target position in the target
variable and the speed at which we want to move the object in the speed
variable.
In the Start
function, we calculate the distance between the current position of the object and the target position and store it in the journeyLength
variable.
In the Update
function, we calculate the distance covered by the object at each frame using the distCovered
variable. We then calculate the fraction of the journey completed using the fracJourney
variable. Finally, we use the Lerp
function to move the object from its current position to the target position based on the fraction of the journey completed.
Smooth camera movement using Lerp function
Suppose we have a camera in ourr game that follows the player’s movements. we can use the Lerp function to smoothly interpolate between the camera’s current position and the player’s position, creating a smooth camera movement effect.
fractionTime
can be calculated using the duration over which the transition needs to happen and the time lapsed. This value should be between 0 to 1.
// Smoothly move the camera towards the player's position
Vector3 targetPosition = player.transform.position;
Vector3 currentPosition = transform.position;
//Update function
transform.position = Vector3.Lerp(currentPosition, targetPosition, fractionTime);
Color Transition using Lerp function
We can use the Lerp function to create smooth transitions between different colors. For example, suppose we want to change the color of a material over time. we can use the Lerp function to smoothly interpolate between the current color and the target color.
fractionTime
can be calculated using the duration over which the transition needs to happen and the time lapsed. This value should be between 0 to 1.
// Smoothly transition the color of a material
Material material = GetComponent<Renderer>().material;
Color currentColor = material.color;
Color targetColor = Color.red;
//Update function
material.color = Color.Lerp(currentColor, targetColor, fractionTime);
Interpolation between values using Lerp function
The Lerp function can also be used to interpolate between different values, such as floats or integers. For example, suppose we have a variable that represents the player’s health. we can use the Lerp function to smoothly interpolate between the current health value and the target health value.
fractionTime
can be calculated using the duration over which the transition needs to happen and the time lapsed. This value should be between 0 to 1.
// Smoothly interpolate the player's health
float currentHealth = player.health;
float targetHealth = 100;
//Update function
player.health = Mathf.Lerp(currentHealth, targetHealth, fractionTime);
Conclusion
The Lerp function is a versatile and powerful tool in Unity that can be used to create smooth animations and transitions. By understanding how the function works and experimenting with different parameters, we can create a wide variety of effects in our game.
It can be used for multiple things like
- Smooth movement of a game object from one position to another position.
- Smooth movement of camera with player.
- Smooth transition from one color to another.
- Interpolation between any two values over a certain period of time.