Cinemachine is an asset capable of performing a vast array of complex camera behaviours for your game project. Cinemachine is free of cost, powerful, and provides a whole host of functionalities that enables players to achieve AAA quality camera behaviour. It suits a range of project scopes, from hyper-casual mobile games to a fully loaded RPG game.

So, today we will discuss the installation process and some basic functions of Cinemachine.

Installation of Cinemachine

First and foremost, go to the top menu and select Windows > Package Manager.

Screenshot (584).png
Location of Package Manager

The Package Manager window will pop up. This is where you will be installing Cinemachine.

If you don't see the Cinemachine package, look for a drop-down menu above the list and select All Packages. You can search for Cinemachine in the search bar to find the option to import it.

Once you locate the package, click on the Import button.

Screenshot (585)_LI.jpg
Importing Cinemachine

Usually, it won't take much time to import the Cinemachine assets. However, it depends on your computing resources.

Using Cinemachine

After the import is completed, you can see an option on the top menu called Cinemachine. You will see a list of options to create various Cinemachine cameras when you click on it.

For this purpose, we will be selecting Create Virtual Camera. A virtual camera is an object that instructs the main camera on what behaviour to perform. When you create a virtual camera, you will notice a script called CinemachineBrain attached to the main camera.

This script relays the behaviour from the virtual camera to the main camera. We also have various kinds of Cinemachine cameras to achieve a variety of behaviours. For example, the Free Look camera achieves the third person's free-look view.

Camera Follow Using Virtual Camera

Step 1: Creating Virtual Cameras

Once you select Create Virtual Camera, you will see an object called CM vcam followed by a number.

Now that you have added a virtual camera to your project, your next step is to assign a subject to the virtual camera to follow.

Step 2: Adding a Subject

To achieve that, you can simply drag the subject (your GameObject) you want your camera to follow and drop it on the slot named Follow. Now, from Body > Follow Offset, you can adjust the offset of your camera to the player object based on your game's preference and tweak the values in X, Y and Z coordinates. We will keep the binding mode to Lock To Target With World Up as it works fine.

Step 3: Adjusting Rotation Movement

After that, your camera must follow your player object to find it. But you can see that when the player object makes a turn or changes direction, it gets out of the camera's view. The camera follows the player's movement but does not track their rotation. In other words, the camera is not looking at the player.

So, to achieve that, we again drop the player object onto the slot named Look At. Now, your camera must stare at your player at its dead centre. That might not be the exact behaviour you want your camera to make. So to offset your camera where it looks, you can go to Aim > Tracked Object Offset and tweak the values in X, Y and Z coordinates (Euler angles).

Screenshot (585).png
Tweaking Values of the Euler Angles

We will not be tweaking other values of the virtual camera because these settings will be enough for us to get our desired camera to follow behaviour. Our camera must follow the player now without writing any complicated scripts.

Blending Between Multiple Virtual Cameras

In many games, we must switch our camera angles seamlessly. We can easily achieve that using Cinemachine with great flexibility with just a little code.

Step 1: Creating a State-Driven Camera

We must create a state-driven camera to blend between two or multiple cameras. To do that, we go to the top menu Cinemachine > Create State-Driven Camera. Then you will see a CM State-Driven Camera on your hierarchy.

Screenshot (588).png

Step 2: Adding Child Virtual Cameras

Now, you have to delete the child virtual camera of the state-driven camera and set all the virtual cameras you want to blend as its child. You can simply select the virtual cameras and drop them onto the CM State-Driven Camera to make it your child. You can check if the virtual cameras are referenced in the Virtual Camera Children list to be sure.

Screenshot (590).png
List of Virtual Camera Children

Step 3: Setting up Animator Component

Add a component Animator to your state-driven camera object and assign it to the Animated Target slot.

Screenshot (587)_LI.jpg
Animator Class
Screenshot (588)_LI.jpg
Animated Target in Animator Class

Create an Animator Controller by right-clicking on the Assets panel Create > Animator Controller. Assign the animator controller to the Animator's empty slot, which we just added.

Screenshot (592).png
Creating an Animator Controller

Double click on the Animator Controller and add empty animation clips based on the number of virtual cameras you want to switch between. Name the animation clips appropriate to its virtual camera counterpart.

Screenshot (594).png
Multiple Virtual Cameras on Animator Controller 

Step 4: Attaching Animation Clips

Now under Custom Blends, you'll see a list where you can attach an animation clip corresponding to its virtual camera. We should click the + button to achieve that and select All Unhandled State.

Select the virtual camera under the Camera drop-down.

Screenshot (596).png
Attaching an Animation Clip 

Step 5: Scripting

For the scripting part, we essentially play the animation clip, and the main camera will switch to its virtual camera counterpart when we play the animation clip.

So, we create a script named BlendCameras.cs and add it to the state-driven camera object.

private enum CurrentCamera
{
    VCam1,
    VCam2,
    VCam3
}

private Animator _animator;
private CurrentCamera _currentCamera;

private void Awake()
{
    _animator = GetComponent<Animator>();
}
BlendCamera.cs

Here, we create an enum that lists all the possible virtual cameras as items and sets the _currentCamera of the same type to keep track of which virtual camera is currently in use. We also refer to our Animator in the Awake()function method to play the empty animation clips.

public void SwitchCamera()
{
    switch (_currentCamera)
    {
        case CurrentCamera.VCam1:
            _animator.Play($"VCam1");
            _currentCamera = CurrentCamera.VCam2;
            break;
        
        case CurrentCamera.VCam2:
            _animator.Play($"VCam2");
            _currentCamera = CurrentCamera.VCam3;
            break;
        
        case CurrentCamera.VCam3:
            _animator.Play($"VCam3");
            _currentCamera = CurrentCamera.VCam1;
            break;
        
        default :
            Debug.Log("Not a proper CurrentCamera enum", this);
            break;
    }
}
SwitchCamera() Function

Then we create a simple SwitchCamera() function method to switch between cameras using a switch case statement. Here, we are simply checking which virtual camera is currently in use.

Based on this, we switch to the next virtual camera by calling the corresponding animation clip. Finally, we assign _currentCamera to the currently active camera. We also check for an exception case, using the Debug.Log() function, in case something goes wrong.

private void Update()
{
    if (Input.GetKeyDown(KeyCode.C))
    {
        SwitchCamera();
    }
}

To ensure our camera switch is working fine, we check it on the Update() method by calling the SwitchCamera() method when we press down the C key on our keyboard.

Customizing Camera Blends

When you play on the editor, you should experience a smooth transition between various virtual cameras. But, your game might need a different customized blend among the cameras. Well, that also can be achieved using Cinemachine very easily.

To create a custom blend between the cameras, we first go to the Custom Blends option and there you will find an empty slot and, right next to it, a button named Create Asset.

Screenshot (589)_LI.jpg
Create Asset Button

Click the button and save the custom blend file generated in an appropriate location in your project. Now, you'll be able to see a list where you can add in what style or easing you want to blend between two virtual cameras.

Screenshot (598).png
Options for Blending Cameras

You have options to choose from under Style on how to transition and the duration of transition under Time.

The list of styles of ease that you can choose from are:

  • Cut
  • Ease In Out
  • Ease In
  • Ease Out
  • Hard In
  • Hard Out
  • Linear
ℹ️
For more detailed knowledge of these transition styles and their easing type, check the official documentation here Blending between Virtual Cameras.
Thank you for reading till the end. I'll be posting more detailed guides like this in the future. Please leave any comments if you have any questions regarding the topic. Have a good day!