How to make 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 two 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 is kept separate but the camera follows the player at a distance. This is because we need to show our players 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.

If you have a game character that doesn’t rotate during gameplay then you can use method number 1 to implement camera follow without any code. If the player has the ability to look around then you need to implement camera follow using a script.

Unity Camera Follow banner

2 major ways to implement Unity Camera follow Player.

Method 1: Making camera as child of the Gameobject

  • All you have to do is 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 case of 2D platformer games.

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

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 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 paste the below code 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 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.


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

Best Camera Follow script in ASSET 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.

You can check our post on UI basics to build great UI

8 thoughts on “How to make 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.