How To Do Smooth 2D Camera Follow In Unity: Best Guide

In this article we’ll explore different ways to implement 2D camera follow in unity. We’ll use three methods to do 2D camera follow in unity which are smooth damp, Lerping, Slerping and Move Towrads.

While all three of these are good for smooth camera follow but in my experience Smooth Damp Function is best for having a “Smooth” 2D camera follow in unity.

But if you have time I recommend you to test all four ways we can make the camera follow a target, so without wasting any time let’s implement 2D camera follow in unity with the best method in my opinion

2D Camera Follow In Unity Using Smooth Damp

The best way to do camera follow in Unity for a platformer game is using the smooth damp function because if you ask me it just feels too smooth and satisfying.

So let’s start using the smooth damp function in our script to follow the player.

1. Setting Up the Script

The first step is to create a new script named “CameraFollow” and attach it to the main camera in your Unity scene to move one step closer to making your smooth camera follow in Unity.

create camera follow script

So let’s create four variables, one for offset for the camera offset from the player, another for smoothingTime with a float data type, another velocity of the camera with Vector3, and a target as a Transform data type variable to reference the player.

using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    [SerializeField] private Vector3 offset;          
    [SerializeField] private float smoothingTime;  
    private Vector3 velocity;       
    [SerializeField] private Transform target;       
}

2. Initializing Variables

Now, let’s create a target position variable with data type Vector 3, we’ll later use this variable as the actual position for the camera.

private void Update()
{
    // Calculate the target position for the camera
    Vector3 targetPosition = target.position + offset;
}

3. Updating The Camera Position

We then use Vector3.SmoothDamp to smoothly move the camera towards the target position, the initial position will be transform.position, and the target position will be the variable that we just made in the update method.

Make sure to change the value of the smoothTime variable according to your needs to make a smooth 2D camera follow in Unity.

private void Update()
{
    // Calculate the target position for the camera
    Vector3 targetPosition = target.position + offset;

    // Use Vector3.SmoothDamp to smoothly move the camera
    transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothingTime);
}

4. Inspector Configuration

You can now attach the script to the main camera in the Unity Editor.

2d camera follow in unity

Assign the player’s transform to the target variable and adjust the offset and smoothing time according to your game’s requirements.

Other Ways To Do Camera Follow In Unity

While Vector3.SmoothDamp is a good and effective method, there are other ways to implement camera follow in Unity. Here are a few alternative approaches:

1. Lerping (Linear Interpolation):

Linear interpolation, or lerping, provides a simple and smooth transition between the current camera position and the target position.

The Vector3.Lerp function calculates a point along a straight line between the two positions based on a specified interpolation factor (smoothingTime * Time.deltaTime).

This results in a gradual and consistent movement of the camera towards the target in a straight line, we’ll also discuss the method which makes a curve movement.

private void Update()
{
    Vector3 targetPosition = target.position + offset;
    transform.position = Vector3.Lerp(transform.position, targetPosition, smoothingTime * Time.deltaTime);
}

2. Slerping (Spherical Linear Interpolation):

Spherical Linear Interpolation, or slerping, introduces a smooth rotation while following the target which means it moves in a curve path to the target.

Similar to lerping, Vector3.Slerp calculates a point along a spherical path between the current and target positions.

This can be particularly useful if you want to smoothly rotate the camera while following the player, providing a more dynamic visual feel.

private void Update()
{
    Vector3 targetPosition = target.position + offset;
    transform.position = Vector3.Slerp(transform.position, targetPosition, smoothingTime * Time.deltaTime);
}

3. Move Towards Function

Move Towards Function allows you to implement a movement logic according to your particular needs.

In this code snippet, Vector3.MoveTowards is used to move the camera towards the target position gradually. This approach gives you more control over the interpolation process.

It can be adapted to create unique camera follow behaviors based on your game’s needs.

private void Update()
{
    Vector3 targetPosition = target.position + offset;
    transform.position = Vector3.MoveTowards(transform.position, targetPosition, smoothingTime * Time.deltaTime);
}

After this, your job might be to make a player controller in Unity so make sure to create one using this tutorial.

FAQs

What Is The Difference Between Lerp And Slerp?

Lerp (Linear Interpolation) moves a point in a straight line between two positions at a constant rate, suitable for straightforward transitions. Slerp (Spherical Linear Interpolation) operates on a curved path, especially beneficial for smooth rotations in 3D space.

What is the default method for implementing camera follow in Unity?

The best and commonly used method for implementing smooth 2D camera follow in Unity is Vector3.SmoothDamp. It provides a straightforward way to smoothly move the camera towards a target position.

Are there alternative methods to achieve camera follow?

Yes, there are alternative methods to implement camera follow in Unity. Some alternatives include lerping, slerping and move towards function.

What advantages do alternative methods offer?

Alternative methods offer flexibility and customization. Lerping provides simplicity, Slerping introduces rotation, and the Move Towards function allows for a unique movement logic for to the specific needs of your game.

Conclusion

In conclusion, achieving a smooth 2D camera follow in Unity is essential for enhancing the gaming experience. This tutorial discussed the fundamental approach using Vector3.SmoothDamp provides a solid foundation for camera tracking.

Additionally, alternative methods such as Lerping, Slerping, and Move Towards functions were explored, each offering unique advantages and customization options.

By choosing the method that best aligns with your game’s requirements, you can create an enjoyable player experience with smooth camera follow in unity.