Implementing a 3rd Person Camera in Unity

Introduction

A third-person camera is one of the most commonly used camera types in video games and interactive experiences. It allows players to explore the environment from an overhead perspective, giving them a sense of immersion and control.

Setting Up the Camera

Before we can implement a third-person camera, we need to set up our scene with a basic camera. To do this, open your Unity project and create a new scene by clicking on “File” > “New Scene”. From there, select “3D Object” from the hierarchy panel and add a Camera component to the scene. This will give you a default first-person camera that you can move around with using the keyboard.

Creating the Third Person Camera

To create the third person camera, we need to modify our existing camera component. First, open the Inspector window by clicking on “Window” > “Inspector”. Next, locate the Camera component in the hierarchy panel and click on it to open its properties.

The first thing we need to do is change the Projection type from Orthographic to Perspective. This will allow us to create a true 3D camera that can see objects at different distances. To do this, select “Perspective” from the dropdown menu under Projection.

Next, we need to adjust the Field of View (FOV) and Aspect Ratio settings. The FOV controls how wide or narrow the field of vision is, while the Aspect Ratio controls the aspect ratio of the screen. For a third person camera, we want a wider FOV and a higher Aspect Ratio to make the environment feel more immersive. Adjust these settings to your liking and click “Apply”.

Creating the Camera Controller

Now that we have our camera set up, we need to create a controller that will allow us to move around the scene. To do this, right-click in the hierarchy panel and select “Create Empty”. Rename this empty object to “Camera Controller” and add a Rigidbody component to it.

Next, add two Transform components to the Camera Controller object: one for the main camera transform and one for the target camera transform. These will be used to follow the player character as they move around the scene.

To create the movement controls, we need to add some script code. Create a new C script by right-clicking in the Project window and selecting “Create” > “C Script”. Rename this script to “Camera Controller” and open it in your favorite text editor.

Inside the script, we need to create two variables: one for the main camera transform and one for the target camera transform. We also need to create a variable for the player character’s transform. To do this, add the following code at the top of the script:

csharp
public Transform mainCamera;
public Transform targetCamera;
public Transform player;

Next, we need to create some movement functions that will allow us to move the camera around the scene. To do this, add the following code inside the script:

scss
void Update()
{
// Move the target camera to follow the player
targetCamera.position = player.position + Vector3.up 2f;
targetCamera.LookAt(player.position + Vector3.forward);
// Rotate the main camera to match the target camera’s rotation
Quaternion targetRotation = targetCamera.rotation;
mainCamera.transform.rotation = Quaternion.Lerp(mainCamera.transform.rotation, targetRotation, Time.deltaTime
5f);
}

This code will move the target camera to follow the player character as they move around the scene, and will rotate the main camera to match the target camera’s rotation. You can adjust the speed of the camera movement by changing the values in the Lerp function.