Hide chapters

Unity Apprentice

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

10. Advanced Camera Controls With Cinemachine
Written by Matt Larson

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

The camera is your portal into your game so don’t underestimate how much good camera control can contribute to the feel of it. However, properly implementing complex camera behaviors isn’t trivial. It’s easy to go wrong. The Cinemachine plugin provides a turnkey solution for all your camera needs. Time to explore Cinemachine and take the veggie battles to the next level!


Cinemachine is a package provided in the Unity Registry. It offers a complete solution to managing multiple cameras and complex camera behaviors.

Don’t reinvent the wheel by attempting to build custom camera scripts to control the camera in your games! Cinemachine handles all of the responsibilities via Virtual Cameras to allow you to build multiple camera views — each with unique and complex logic — to follow GameObjects and aim at targets. Cinemachine provides out-of-the-box solutions for 3rd-person-following cameras, orbiting cameras and many more.

Open the Chapter 10 starter project in Unity and begin by loading the RW / Scenes / Arena scene. Before working with the advanced camera components, you need to add the Cinemachine package via the top menu: Window ▸ Package Manager. Choose Packages: Unity Registry from the drop-down and select Cinemachine.

Select Install to add the package. After it’s added to the Unity project, a new top menu named Cinemachine is immediately added to the Unity Editor.

Cinemachine Components

Cinemachine coordinates multiple camera views through a central hub called the Cinemachine Brain. Virtual Cameras, also called vcams for short, provide means to customize the behaviors of your camera views and assign special camera locations in your scenes.

Cinemachine Brain

The CinemachineBrain is a component script that acts as the connector between multiple vcams and a single active Main Camera GameObject.

Virtual Cameras

You’ll start by adding some Virtual Cameras to the scene to provide different in-game views.

Gate Camera

Again, duplicate the Zoom vcam and rename the GameObject to Gate vcam. Set the Follow and Look At targets to GateWall 1.

Transitioning between cameras with scripting

Each virtual camera in the scene has a priority value; the camera which has the highest priority for the scene will be assigned to the Unity Camera. In order to change the active camera by a script, you simply need to change which camera has the highest priority.

using Cinemachine;
public CinemachineVirtualCamera zoomCamera;
public CinemachineVirtualCamera playerCamera;
public CinemachineVirtualCamera gateCamera;
public void ActivateCamera(CinemachineVirtualCamera camera)
    zoomCamera.Priority = lowPriority;
    playerCamera.Priority = lowPriority;
    gateCamera.Priority = lowPriority;
    camera.Priority = highPriority;

Tank destruction

A neat effect you can introduce is to show the tank being destroyed when the battle is lost.

// Hide player.

// Replace with destroyed tank.
DestroyedTank.transform.position = player.transform.position;
DestroyedTank.transform.rotation = player.transform.rotation;


If you want to go further with the Cinemachine system, try these challenges:

Key points

  1. Cinemachine is a package that provides easy-to-use camera controls.
  2. Virtual Cameras, or vcams, are customizable cameras representing different views in your scene.
  3. CinemachineBrain is a component that should be assigned to your Main Camera. It determines the active vcam view based on the highest priority and configures how camera transitions behave.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2023 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now