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?

Tip: You can also use camera controller assets.

You can also watch my youtube video or continue reading the article

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 new to unity and wondering how to implement this code so don’t worry I will guide you to implement the same in unity so you will understand how you can use it in your game.

Before that, I also want to suggest to you one plugin that I use in my projects to move objects, it is DOTween. It makes your work easier. 

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

Excited? Let’s start!

It Makes Lerp and Smooth Movement Job Easy For You. Try It

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.

Aakash Solanki
Aakash Solanki

I am a professional unity game developer. I have a passion for creating immersive gaming experiences. For me writing articles feels like giving back to the community we have learned from. My hobbies involve playing games and writing articles and sometimes I also create youtube videos.


Share on social media

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top