Camera in unity is the most important gameobject. If you don’t get the camera angle right the game play experience will suffer. Making a camera follow script in unity is simple yet many people get it wrong. In this post we will see how to implement camera follow script in unity properly.
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. The reason for separating camera and player rotation is discussed below.
Camera follow can be implemented in two ways.
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. So , its recomended to use scripts to control the camera movement.
2. Using 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.
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;
Late update 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 update function you can see that the player moves in and out of the scene if your game is running at a higher FPS.
Best Camera Follow script in ASSET store
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