Beginner’s guide to make your camera follow player in Unity

Camera in Unity is the most important gameobject unless you are making a UI-based game. If you don’t get the camera angle right the gameplay experience will suffer. Making a Unity camera follow script is simple yet many people get it wrong. In Unity, the camera follow can be executed in three different ways. Which method to use mostly depends on the type of game you are making. In this post, we will see how to implement the camera follow script in Unity properly with code samples.

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. There are two major ways you can implement camera follow in Unity. The method of implementation depends totally on the features of the player character in your game.

Method 1: Making the camera as child of the Gameobject

  • Drag the camera gameobject into the gameobject you want the camera to follow.
  • 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 gameobject 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.

Method 2: Using Unity camera Follow script

Camera follow script can give you great control over what transforms of a 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.

To calculate the offset. 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 Camera follow 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;
    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.

Method3: Implementing Camera Follow using Unity 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

Using Camera Follow script in Unity asset 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.

10 thoughts on “Beginner’s guide to make your camera follow player in Unity”

    • 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

Leave a Reply

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