Overview
When you first start Unity game development, do you implement object movement and rotation by directly assigning coordinates like transform.position = targetPosition;?
With this method, the object instantly warps to the target position, resulting in "choppy and unnatural" movement. Especially when player character or camera movement feels unnatural, the game quality suffers greatly.
What we aim for is "smooth and pleasant" animation like professional games. To achieve this smooth movement, Unity developers almost always use two powerful functions: Lerp and Slerp.
This article explains the basic concepts of these two interpolation functions—Lerp (Linear Interpolation) and Slerp (Spherical Linear Interpolation)—along with practical C# code you can copy and paste directly into your Unity projects, all in a beginner-friendly manner.
What is Lerp (Linear Interpolation)?
Lerp stands for "Linear Interpolation." It's a calculation method that interpolates (fills in) between two values (A and B) at a specified ratio (t) in a linear manner.
Unity provides this function for various types, including Vector3.Lerp, Color.Lerp, and Mathf.Lerp.
Basic Function Form:
// A: Start value, B: End value, t: Interpolation coefficient (0.0f to 1.0f)
Result = Lerp(A, B, t);
- When
t = 0.0f, the result isA. - When
t = 1.0f, the result isB. - When
t = 0.5f, the result is exactly halfway betweenAandB.
Lerp is most commonly used for object movement, color changes, smooth numerical changes, and other situations requiring linear change.
What is Slerp (Spherical Linear Interpolation)?
Slerp stands for "Spherical Linear Interpolation." While Lerp performs linear interpolation, Slerp interpolates along the shortest path on a sphere.
Slerp is primarily used when dealing with Quaternion, i.e., object rotation.
Why is Slerp Needed for Rotation?
When trying to interpolate rotation with Lerp, the speed may become uneven during rotation, or it may not take the shortest path, resulting in unnatural movement. Using Slerp ensures always constant angular velocity for the most natural and smooth rotation.
Key Differences Between Lerp and Slerp
| Feature | Lerp (Linear Interpolation) | Slerp (Spherical Linear Interpolation) |
|---|---|---|
| Interpolation Method | Linear | Along shortest path on sphere |
| Main Use | Vector3 (position), Color, float (numbers) | Quaternion (rotation) |
| Speed | Decreases as approaching target (easing out) | Constant speed (angular velocity) |
| Computational Load | Light | Heavier than Lerp (due to rotation calculations) |
Common Pitfall for Beginners: Using Lerp in Update Functions
When using a fixed value (e.g., 0.1f) for the interpolation coefficient t like Vector3.Lerp(transform.position, targetPosition, speed), an easing out effect occurs where movement speed decreases as approaching the target.
This is fine if it's the intended animation, but if you want "constant speed movement," you need a different calculation using Time.deltaTime for the interpolation coefficient.
Practical Examples
Example 1: Smooth Movement with Lerp (Easing Out)
This code smoothly moves an object toward a target position. It easily achieves a natural deceleration animation (easing out) where speed decreases as approaching the target.
// File name: LerpMovement.cs
using UnityEngine;
public class LerpMovement : MonoBehaviour
{
// Start and end positions
public Vector3 startPosition = new Vector3(-5f, 1f, 0f);
public Vector3 endPosition = new Vector3(5f, 1f, 0f);
// Movement speed (used as Lerp interpolation coefficient)
[Range(0.01f, 1f)]
public float speed = 0.1f;
void Start()
{
// Set initial position
transform.position = startPosition;
}
void Update()
{
// Move from current position toward target position by speed ratio
// Multiplying by Time.deltaTime ensures frame-rate independent smooth movement
// Note: This implementation causes speed to decrease as approaching target (easing out)
transform.position = Vector3.Lerp(transform.position, endPosition, speed * Time.deltaTime);
// Reset position to loop when close enough to target
if (Vector3.Distance(transform.position, endPosition) < 0.01f)
{
// Swap end and start positions
Vector3 temp = startPosition;
startPosition = endPosition;
endPosition = temp;
}
}
}
Attach this script to any GameObject and adjust the speed value in the Inspector. Smaller values make it slower, larger values make it approach the target faster.
Example 2: Smooth Rotation with Slerp
This code smoothly rotates an object toward a target angle. By using Slerp, the rotation maintains constant angular velocity throughout, rotating elegantly without speed changes.
// File name: SlerpRotation.cs
using UnityEngine;
public class SlerpRotation : MonoBehaviour
{
// Target rotation angle (Euler angles)
public Vector3 targetEulerAngles = new Vector3(0f, 180f, 0f);
private Quaternion targetRotation;
// Rotation speed (used as Slerp interpolation coefficient)
[Range(0.01f, 1f)]
public float rotationSpeed = 0.1f;
void Start()
{
// Convert target Euler angles to Quaternion
targetRotation = Quaternion.Euler(targetEulerAngles);
}
void Update()
{
// Move from current rotation toward target rotation by rotationSpeed ratio
// Slerp performs spherical linear interpolation, always rotating at constant angular velocity, ideal for rotation animation
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
// Reverse target rotation to loop when close enough to target
if (Quaternion.Angle(transform.rotation, targetRotation) < 0.1f)
{
// Reverse target angle
targetEulerAngles *= -1;
targetRotation = Quaternion.Euler(targetEulerAngles);
}
}
}
Common Mistake: Direct Quaternion Manipulation
Unity's rotation is internally managed using a mathematical structure called Quaternion. While more complex than Euler angles (X, Y, Z angles), it's important for preventing gimbal lock (phenomenon where rotation on a specific axis is lost).
When dealing with rotation, either directly manipulate transform.rotation as Quaternion, or use Quaternion.Euler() to convert from Euler angles to Quaternion before using Slerp.
Summary
This article delved deep into Lerp and Slerp, the two pillars for achieving smooth motion in Unity.
Here are the key takeaways:
- Lerp (Linear Interpolation) is used for linear changes like position and color.
- Using
speed * Time.deltaTimefortinLerp(A, B, t)creates an easing out effect where speed decreases as approaching the target. - Slerp (Spherical Linear Interpolation) is mainly used for Quaternion (rotation), smoothly interpolating along the shortest path on a sphere at constant angular velocity.
- To avoid choppy motion, instead of direct assignment, you need to use Lerp or Slerp in the
Updatefunction to gradually approach the target each frame. - It's professional practice to use Slerp instead of Lerp for rotation to prevent unnatural movement.
Use this knowledge and code examples to bring professional "smoothness" to your Unity projects.