Unity Camera follow Player

In this article you will see how to make the camera follow player using a script in Unity. In the below sections, you can find more methods like making the camera as the child of player and using Unity Cinemachine.

Camera Follow Player with Script

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;
private Vector3 newtrans;

void Start ()
{
    offset.x = transform.position.x - player.transform.position.x;
    offset.z = transform.position.z - player.transform.position.z;
    newtrans=transform.position;
//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.

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.

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

10 thoughts on “Unity Camera follow Player”

    • 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.

      Reply
  1. Thanks for the info on camera angle.
    Will the same we can implicate for Agumented reality game using Unity??

    Reply

Leave a Reply

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

%d bloggers like this: