How to make a smooth camera follow in unity c#?

Share on social media

So in this article, we will see how to make a smooth camera follow in unity, It is very useful for Third-person games or any game where the camera will follow our player it can car game or anything.

How to make smooth camera follow in unity
How to make smooth camera follow in unity

There are some basic things to understand to make camera follow player in unity that we will look into shortly. I will also guide you about the same in this article on implementing the below script in your unity project to make camera follow player.

So without wasting any further time let’s look into the code.

Smooth camera follow in unity

using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform target;

    public bool isCustomOffset;
    public Vector3 offset;

    public float smoothSpeed = 0.1f;

    private void Start()
    {
        // You can also specify your own offset from inspector
        // by making isCustomOffset bool to true
        if (!isCustomOffset)
        {
            offset = transform.position - target.position;
        }
    }

    private void LateUpdate()
    {
        SmoothFollow();   
    }

    public void SmoothFollow()
    {
        Vector3 targetPos = target.position + offset;
        Vector3 smoothFollow = Vector3.Lerp(transform.position,
        targetPos, smoothSpeed);

        transform.position = smoothFollow;
        transform.LookAt(target);
    }
}

Code Breakdown:-

1. Variables:- We will need 3 variables target whom the camera will follow, offset the offset between camera and player, and last is smoothSpeed or follow the speed of the camera.

There is one more variable called “isCustomOffset”, This will be helpful when you want to give a specific offset from the inspector window.

2. Execution Method:- We always prefer to use “LateUpdate” in camera follow scripts to smooth following. Because LateUpdate Executes after both update functions Update and FixedUpdate.

3. Logic for the Camera follow:- The main logic in this code to make camera follow player is the Vector3.Lerp, It is also known as Linear Interpolation. It gives a smooth movement to the camera. You can check here How Lerp Works In Unity?

Use the above script, To make smooth camera follow in unity, This script will make your camera follow the object or the player smoothly, You can use this script in your game. In unity camera follow player using this script.

But If you are someone who is new to unity and wondering how to implement this code in unity so don’t worry I will guide you to implement the same in unity and you can do it too in your game. 

We will see the results of how the camera follow player will work after using this script. 

Excited? Let’s start!

How to make camera follow player in unity ?

We have seen the code above but now let’s see how you can implement the same in unity. 

First, create a script in unity and copy the complete code I showed you above, then select all the code you have in your new script, delete it and paste the code you copied.

Then, Attach the script to the Main Camera.

unity camera follow player

We have attached the script to the camera. So If you noticed in the above image, our bool called “isCustomOffset” is default false and we have not made it true. So let’s see how it will look like if we play the game now.

That’s Looking Cool! Isn’t It?

Now let’s say we want to have a custom Offset Instead of the default one. So to do that simply make the “isCustomOffset” bool to true. 

camera follow player in unity

As you can see in the above image, We have checked the “IsCustomOffset” bool to true. Now it will use the offset that we have defined in the inspector. So let’s play the game in unity now and see what it will look like.

I think now it is looking much nicer than the previous one. What do you think? Let me know in the comments.

Now you should be able to implement this in your game as well and make camera follow the player.

So this is how you can make a smooth camera follow in unity using C#. Hope it was helpful, comment below if you have any other issues or queries.

Note:- Don’t Forget to attach this code to the main camera.

You can also read our other topics like – CoroutinesCamera ShakeDrag Objects With MouseCamera Clamping, etc.

You can also watch my youtube video on this topic


Share on social media

Leave a Comment

Your email address will not be published.