Skip to content

How to make the camera follow the player in a 3D Game in Unity 2019.4

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

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.
Unity Project Creation

Create a new Scene

  • There is sample scene available in the project.
Sample Scene in unity
4
  • Create a new scene in the Scenes Folder.
  • Right clicking on folder and navigate to Create > Scene
New Scene in unity
  • Rename this scene to “CameraFollowDemo“.
  • Open the CameraFollowDemo scene.
  • There are only 2 Objects Available in the Scene
    1. Main Camera
    2. Directional Light
Camera & Light in scene

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.
New 3D Cube Object
  • Set the Player_Body object position as shown in below image.
Playerbody object - Transform property
List of Player Object Parts
  • 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.
Player Left Hand
Player Right Hand
Player Face
Player Left Leg
Player Right Leg
  • Create an empty game object and rename it to “Player“.
Player Object
  • Drag all player objects onto this Player object.
Parent - Child list of Player Object
  • Finally our player object is ready
Player Output
  • Create a plane object in the Hierarchy Window.
  • Rename the plane to “Ground”.
  • Set position and the scale of the Ground Object.
Ground object
Player Character & Ground Without Material

Create a new materials in unity

  • Create a new empty folder and rename it to “Materials“.
Create New Material
  • Create Materials for the player & the ground object.
Player & Ground Material

Output:

Add Material to Player & Ground

Change the Position of the Camera

Main Camera Position & Rotation change

Create a script for the Player Movement

  • Write the code for a basic player movement.
  • Create a new script with the name “PlayerMovement”.
  • Here we use Input.GetAxis to get virtual input from the arrow keys.
Create a new C# Script
  • In this Script, we move the player with the Translate function.
  • Assign this script to the player object.
Assign Player Movement script to Player

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.
  • Now create a reference variable with the name “targetObject” object. (here Target=Player)
  • Offset means default distance between the player and the camera or we can say minimum distance between the camera and the player.
  • Find out the distance between the camera and the target and store the value in camera Offset
  • 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.
  • Add new a variable for the smoothing factor.
  • Using the Vector3.Slerp function the camera will move from the current position to new position.
  • create a bool variable
  • If this bool variable is true then the camera will look at the target otherwise only follow target’s position.

Vector3.Slerp : https://docs.unity3d.com/ScriptReference/Vector3.Slerp.html

Final Output

  • Add the “CameraFollowScript” to the camera object.
new Camera Follow Script

Output

Camera Follow Player

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.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide