2 simple ways to implement Unity Camera Follow player

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 script can be executed in two different ways. The method used 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

2 major ways to implement Unity Camera follow.

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 and and angle and forget it. When the player gameobject moves the camera follows the gameobject. But, There is one issue with implementing camera follow in this manner. The problem comes when the gameobject rotates.

When the parent object rotates, the child rotates along with it. So when the player gameobject rotates the camera rotates along with it, sometime 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 , its recommended to use scripts to control the camera movement.

2. Using Unity camera Follow script

Camera follow script can give you great control on what transforms of a camera you want to change based on player movement. A common method is to set the initial view of camera based on player and calculate the offset based on the initial position. This offset is maintained to keep the view intact. Let look at a sample script for basic understanding.

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;
}
}

Lets see what the script does. We are initializing a public gameobject that will be our player. You can drag and drop your character 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.

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

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

4 thoughts on “2 simple ways to implement Unity Camera Follow player”

Leave a Reply

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