How to code that the camera follows the player in Unity 2019.4?
How to achieve a smooth camera following in a 3D game in Unity
What is the use of lerp? And specifically in camera movement in Unity?
How to create a camera follow script in Unity?
Generally, in games, the camera is a very important object. In this article, we are discussing how we can achieve that the camera follows the player in 3D games.
Steps to create your own camera follow script for a 3D game:
Unity Project Setup
- Create a new project in Unity 3D.
- Assign project name and location path.

Create a new Scene
- There is sample scene available in the project.

- Create a new scene in the Scenes Folder.
- Right clicking on folder and navigate to Create > Scene

- Rename this scene to “CameraFollowDemo“.
- Open the CameraFollowDemo scene.
- There are only 2 Objects Available in the Scene
- Main Camera
- Directional Light

What is the Camera object? : https://docs.unity3d.com/Manual/class-Camera.html
Create a new 3D Character & Ground in Unity
We need one player whom the camera will follow.
- Add a new 3D cube object in the Hierarchy Window.

- Set the Player_Body object position as shown in below image.


- Create another cube object the same as Player_Body object with names as shown in the upcoming images.
- Change Position, Rotation, and Scale of the object.





- Create an empty game object and rename it to “Player“.

- Drag all player objects onto this Player object.

- Finally our player object is ready

- Create a plane object in the Hierarchy Window.
- Rename the plane to “Ground”.
- Set position and the scale of the Ground Object.


Create a new materials in unity
- Create a new empty folder and rename it to “Materials“.

- Create Materials for the player & the ground object.

Output:

Change the Position of the Camera

Create a script for the Player Movement
- Write the code for a basic player movement.
- Create a new script with the name “PlayerMovement”.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float playerSpeed = 10.0f;
public float playerRotationSpeed = 100.0f;
void Update()
{
// Get the horizontal and vertical axis using arrow keys.
// The value is in the range -1 to 1
// Make it move 10 meters per second instead of 10 meters per frame...
float translation = Input.GetAxis("Vertical") * playerSpeed* Time.deltaTime;
float rotation = Input.GetAxis("Horizontal") * playerRotationSpeed* Time.deltaTime;
// Move translation along the object's z-axis
transform.Translate(0, 0, translation);
// Rotate around our y-axis
transform.Rotate(0, rotation, 0);
}
}
- Here we use Input.GetAxis to get virtual input from the arrow keys.

- In this Script, we move the player with the Translate function.
- Assign this script to the player object.

Input.GetAxis : https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
Create a script for Camera Follow
- You will notice that the camera value is static so if the player goes outside of the camera view then the player is not visible.
- So write code so the camera will follow the player when the game is running.
- Create a new script with the name “CameraFollowScript” and open the script in Editor.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
- Now create a reference variable with the name “targetObject” object. (here Target=Player)
//The target object
public Transform targetObject;
- Offset means default distance between the player and the camera or we can say minimum distance between the camera and the player.
//Default distance between the target and the player.
public Vector3 cameraOffset;
- Find out the distance between the camera and the target and store the value in camera Offset
void Start()
{
cameraOffset = transform.position - targetObject.transform.position;
}
- A follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update. And LateUpdate is called after Update each frame.
// Update is called once per frame
void LateUpdate()
{
Vector3 newPosition=targetObject.transform.position+cameraOffset;
}
- Add new a variable for the smoothing factor.
//Smoothing factor which we will use for the Camera rotation
public float smoothFactor=0.5f;
- Using the Vector3.Slerp function the camera will move from the current position to new position.
transform.position = Vector3.Slerp(transforn.position, newPosition, smoothFactor);
- create a bool variable
//This will check if the camera looks at the target or not.
public bool lookAtTarget=false;
- If this bool variable is true then the camera will look at the target otherwise only follow target’s position.
//Camera Rotation Change
if (lookAtTarget)
{
transform.LookAt(targetObject);
}
Vector3.Slerp : https://docs.unity3d.com/ScriptReference/Vector3.Slerp.html
Final Output
- Add the “CameraFollowScript” to the camera object.

Output

Summary
As you can see, it’s not too much work to make the camera follow the player in a smooth manner. This will help you create games that look just that bit more professional and the playing experience is improved significantly. What do you think? Is this going the be useful for your projects? If you, leave a comment here or at the YouTube video.