Switching Between Virtual Cameras in Unity / Cinemachine

Michael Hatfield
3 min readMar 3, 2023

--

Cinemachine is a powerful camera system in Unity that allows developers to create cinematic experiences with ease. One of the most useful features of Cinemachine is the ability to switch between multiple cameras seamlessly. In this tutorial, we will explore how to switch between cameras in Cinemachine using Unity.

Step 1: Setting Up the Scene

To demonstrate how to switch between cameras in Cinemachine, we will create a simple scene with two cameras. Create a new scene by going to File > New Scene. In the Hierarchy panel, create an empty game object and name it “Cameras.” Then, create two child game objects under “Cameras” and name them “Main Camera” and “Secondary Camera.” Add a Cinemachine Virtual Camera component to both cameras.

Step 2: Setting Up the Cinemachine Brain

Next, we need to set up the Cinemachine Brain. The Cinemachine Brain is responsible for controlling which camera is active in the scene. To add the Cinemachine Brain, select the “Cameras” game object in the Hierarchy panel and go to Component > Cinemachine > Cinemachine Brain. In the Cinemachine Brain component, you will see a list of virtual cameras. We will use this list to define which camera should be active at any given time.

Step 3: Setting Up the Camera Transitions

To switch between cameras, we need to define camera transitions. Camera transitions define how the camera will move from one position to another when the camera is switched. To add a camera transition, select the “Main Camera” game object in the Hierarchy panel and go to Component > Cinemachine > Cinemachine Virtual Camera. In the Cinemachine Virtual Camera component, click the “+” icon next to the “Transitions” field to add a new transition. Set the “Camera” field to “Secondary Camera” and set the “Blend” parameter to “Ease InOut.” Repeat this process for the “Secondary Camera” game object, setting the “Camera” field to “Main Camera” and the “Blend” parameter to “Ease InOut.”

Step 4: Switching Between Cameras

Now that we have set up the cameras and transitions, we can switch between cameras using code. To switch to the secondary camera, add the following code to a script:

This code finds the Cinemachine Brain component in the scene and sets the priority of the active virtual camera to 10 and the priority of the secondary camera to 11. This causes the secondary camera to become the active camera and the main camera to become inactive.

To switch back to the main camera, add the following code to the same script:

This code sets the priority of the main camera to 11 and the priority of the secondary camera to 10, causing the main camera to become active and the secondary camera to become inactive.

Conclusion:

In this tutorial, we have learned how to switch between cameras in Cinemachine using Unity. We set up a scene with two cameras, defined camera transitions, and used code to switch between cameras. With this knowledge, you can create cinematic experiences that take advantage of multiple cameras to capture different perspectives of your scene.

--

--