Unity Camera follow Player tutorial with script

There are 3 major ways to implement Camera follow Player in Unity. In this tutorial, we will learn how to make Unity camera follow player using the following methods

Camera follow is mainly used in third person games like Batman. In those games, you can see that the camera rotation and player rotation are kept separate but the camera follows the player at a distance. This is because we need to show how the character is moving with respect to its environment.

Making the camera as child of the Player

  • Drag and drop the camera into the Player gameobject in the Hierarchy window. This makes the camera a child of your player.
  • Now you can set the camera view, distance, and angle.
  • When the player gameobject moves the camera follows the gameobject as the camera is a child of the player gameobject.

But there is one issue with implementing camera follow in this manner. The problem comes when the Player rotates. This method is most effective in the case of 2D platformer games and First-person view.

When the parent object rotates, the child rotates along with it. So, when the player gameobject rotates, the camera rotates along with it, sometimes making the view disturbed and skewed. This can lead to poor gameplay experience. It is often not advised to use this method unless you are very sure of your character movement. So, it’s recommended to use scripts to control the camera movement.

Free Unity asset banner

Using Unity Camera Follow Player script

Unity Camera follow Player script can give you great control over what transforms of the camera you want to change based on player movement. A common method is to set the initial view of the camera based on the player and calculate the offset based on the initial Vector3 position. This offset is maintained to keep the view intact.

Set your camera as required by adjusting the transform in the editor. This will be your initial camera position. We will calculate the offset based on this in the script.

  • Create a new script by name Camera_Follow in the project window using create>new C# script.
  • Copy and paste the code below in the script.
  • Add the script to camera using add component button in the inspector window or drag and drop the script to the camera.
  • Drag and drop the player gameobject to the Camera_Follow script.
  • Run the game and the camera will follow the player.
using UnityEngine;
using System.Collections;
public class Camera_Follow : MonoBehaviour
public GameObject player;
private Vector3 offset;
void Start ()
    offset = transform.position - player.transform.position;
void LateUpdate ()
transform.position = player.transform.position + offset;

Let’s see what the script does. We are initializing a public gameobject that will be our player. You can drag and drop your player to the script in the editor window. Then we set an offset for the camera from the player. You can just place the camera in a position based on the required view. The script will calculate the initial offset and maintain it when following the player. Then in the LateUpdate function, we move the camera based on the player position.

As you can see in the script, we are only changing the transform of the camera and not the rotation. So even if the player rotates the rotation of the camera remains unchanged.

“Lateupdate” is used to avoid jittering movements. Late update executes after the update function. So, if the player moves in the update, we can update the camera position after the player moves.

There is a small problem with the script above.

When the player jumps the camera jumps with the player, making the jump look unnatural. So rather than applying the complete transform, it’s better to use the exact axis. Mostly the player will be moving in the “xz plane”. So, update the camera position only based on X and Z axis. Here is the final script that you can use.

Final Unity Camera follow player script

using UnityEngine;
using System.Collections;

public class Camera_Follow : MonoBehaviour
public GameObject player;
private Vector3 offset;
private Vector3 newtrans;

void Start ()
    offset.x = transform.position.x - player.transform.position.x;
    offset.z = transform.position.z - player.transform.position.z;
//not taking y as we won't update y position. 

void LateUpdate ()
newtrans.x= player.transform.position.x + offset.x;
newtrans.z= player.transform.position.z + offset.z;
transform.position = newtrans;


If you are updating the camera position in the update function you can see that the player moves in and out of the scene if your game is running at a higher FPS. You might not see this in every case but it’s best to use “LateUpdate”. If you still find it difficult, you can just grab the asset below from the Unity asset store and solve all your Unity camera follow problems. You can read more about the Unity update function in our other blog post.

Implementing Unity Camera Follow Player using Cinemachine

With the release of Unity Cinemachine a lot of camera features have been inbuilt in the virtual camera. Let’s see how to make the camera follow the player using virtual Vcam in Cinemachine.

Step1: Install Unity Cinemachine

  1. Go to Windows>Package Manager.
  2. If you are using Unity 2021 then select Packages: Unity Registry. You can skip this step for older versions.
  3. Search Cinemachine and Install it.
Installing Cinemachine using Unity Package Manager

Step2: Add a virtual cam

  1. In older versions of Unity, you will see a Cinemachine option on the main menu. For Unity 2021 this option is moved inside Gameobject.
  2. Go to Cinemachine>Virtual camera.
  3. This will add a Vcam to the scene and a Cinemachine brain to the main camera.
  4. We need only one Vcam for this purpose.

Step3: Assign the player to the Virtual Cam

  1. Select the Virtual camera in the Hierarchy window.
  2. Go to the inspector window and find the CinemachineVirtualCamera component.
  3. Drag and drop your player to the follow option in the CinemachineVirtualCamera component.
  4. In the body component select framing transposer.
  5. Reduce x, y, z damping in the body component to reduce the delay in follow.

Play the game and the camera will follow the player.

This method cannot be used if you have multiple virtual cams for making cutscenes.

Cinemachine virtual camera inspector settings

Unity Camera follow player asset from Unity store

If you have downloaded any FPS assets or character controller assets before. It might have a script to make the camera follow the player. If not the asset below is the best camera controller in the assets store.

Camera Controller

Camera Controller

Camera Controller is super smooth and built for 3rd person games, 1st person games, and even strategy/MOBAs. Using ‘camera motors’, the Camera Controller is able to seamlessly transition from one view to another. You can even transition mid-play to give your game that AAA quality boost.

Feature rich, the Camera Controller supports off-center 3rd person views, advanced view obstruction detection, character fading, camera shakes, and lots more.

Top online courses in Development

10 thoughts on “Unity Camera follow Player tutorial with script”

    • Yes. You can set the camera as a child if your player doesn’t rotate. Or you can set the initial position as offset and move the camera based on player movement.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.