How to Make the Camera Follow the Player Using Cinemachine - Unity Game Development Tutorial

In this Unity game development tutorial we're going to look at how we can have the camera follow the player so that they’re always centred in the view.

You can either watch the video version below or continue reading for written instructions.

OK, we'll start with this scene that has a character we can move around. 

Character moving and interacting with obstacles

We created this scene in our post on using the 'Character Controller', so take a look if you want to know how it was done.

At the moment our camera is static, and we are free to move the character completely out of view.

Moving the character out of view

We're going to change this so that our camera tracks the character using a feature of Unity called Cinemachine. Cinemachine provides a suite of tools for creating various different camera behaviours without writing any code.

Before we can use this feature, we first need to install the package. We'll select Window->Package Manager from the main menu.

Selecting the package manager

We'll search for Cinemachine and install this package.

Installing the Cinemachine Package

Once this is installed we'll see we now have a Cinemachine option on the main menu. We'll select this and then select 'Create Virtual Camera'

Creating a Virtual Cinemachine Camera

In our hierarchy we now have a new Virtual Camera and our main camera has been updated to have a Cinemachine brain.

The Virtual camera now controls the position and rotation of our main camera. We can add as many of these as we want to set up various different shots. The Cinemachine brain is responsible for monitoring all the virtual cameras and choosing the next Virtual Camera to control the main camera.

In this tutorial, we're just going to setup one virtual camera to track our character. 

Let's select the virtual camera in the hierarchy and have a look at the properties in the Inspector.

The Cinemachine Virtual Camera Properties

The Follow property allows us to provide a game object we want the camera to follow, so we'll drag our character into this slot.

We can see that our character is now centred in the Game View.

The Character centred in the view

The 'Look At' property allows us to specify a game object for our camera to aim at. This will automatically rotate the camera to face this GameObject at all times. We just want our camera to follow and not rotate, so we'll leave this blank.

In the Body section, we can choose the algorithm that moves the camera. There are various options to play with, but we're going to select the 'Framing Transposer'.

The Framing transposer options

Let's press play to try this out.

The Character being tracked by the camera

Now the camera tracks our character.

It's a bit too close to the ground though, so let's stop the game and change the angle we're looking at our character.

We'll set the rotation to 45 on the X axis of the Transform.

Setting the rotation angle of the camera

You can see that Cinemachine has automatically changed the position of our camera, so that we are now looking down on our character at a 45 degree angle.

New view of the character

We'll also zoom in on our character a bit, by setting the Camera Distance to 8.

Zooming the camera

If we try this out now, it gives us a much better view of our character as it moves around.

Tracking the Camera with Cinemachine

You'll have noticed that we now have an overlay of various coloured boxes on our game view.

The yellow square in the middle shows the target that the camera will follow.

The light blue box highlights what is called the soft zone. When the target is in this region, the camera will realign to the desired position over a period of time. We can see this when we move the character. The crosshairs aren’t quite on the character while it moves, but then it realigns once we stop.

Let's stop the game and make a few more changes.

We'll raise the yellow box slightly to focus on the centre of our character by changing the Y offset to 0.4.

Changing the tracked offset on the Cinemachine camera

We can change the speed that the camera catches up in the soft zone by altering the damping values. Increasing these values will make the camera catch up slower and decreasing them will make the camera catch up faster. If we want the camera to always keep up, we can set all of these values to zero.

Changing the damping values of the Cinemachine camera

Let's play our game again. 

Camera tracking the character exactly

The camera now follows the character exactly and there is no catch up time.

That covers everything for this tutorial. We hope that you found it useful. Please leave any questions or feedback in the comments below, and don't forget to subscribe to get notified when we publish our next post.

Thanks.

Comments

Popular posts from this blog

Rotating a Character in the Direction of Movement - Unity Game Development Tutorial

Creating Terrain from Heightmaps - Unity Game Development Tutorial

Simple Movement using Gamepad and Keyboard - Unity Game Development Tutorial