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

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


Smooth camera follow in unity

Code Breakdown:-

1. Variables:- Basically 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.

2. Execution Method:- We always prefer to use “LateUpdate” in camera follow scripts to smooth following.

3. Logic for the Camera follow:- Which I am gonna share below.

Use the below 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.

using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform target;
    public Vector3 offset;

    public float smoothSpeed = 0.1f;

    private void Start()
    {
        // You can also specify your own offset from inspector as it is public variable
        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);
    }
}

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

Leave a Comment

Your email address will not be published.