Space Flight Simulator: Camera Switching System

Ryan McCoach
6 min readApr 5, 2023


In this article we will be covering how to use Cinemachine to create a camera switching system that will move the camera back and forth between a follow and cockpit camera.

Camera Manager

Since we are going to have two cameras and I plan on at more, it would be a good idea to create a Camera Manager to keep all of them in one place. Create an empty game object called “Camera Manager”. Parent the Main Camera the “Camera Manager”.

Create two empty game objects “FollowCam” & “CockpitCam”. These two objects will house the two different virtual cameras.

I am using Unity 2021 and Cinemachine is integrated into the editor, but if you are using earlier verisons, you can import Cinemachine through the Package Manager.

We are going to create two Cinemachine (CM) Virtual Cameras.

Parent the virtual cameras to the FollowCam and CockpitCam game objects.

Follow Camera

This camera will follow behind the space ship given the player a 3rd person view. The virtual camera allows you to assign a game object for the camera to Follow AND Look At. We are going to assign the Player (space ship) game object for the camera to Follow and Look at.


The Body properties focus on the FOLLOW aspect of the virtual camera.

Please check out my previous article that goes more in-depth of these properties.

The main focus is the Follow Offset that determines the position of the camera as it follows the target. Adjust these values to get the view you what the player to have with the 3rd person camera.

The Pitch, Roll, Yaw damping will have control how well the camera tracks the rotation around these axes.

The smaller the number the more responsive it will be, so I want to have some delay with the follow camera as the player pitches, rolls, and yaws.


The Aim properties are for the rotation of the camera for the LOOK AT target.

The only thing adjusted here was the Tracked Object Offset to rotate the camera up more to get a better view of the top of the space ship.

Cockpit Camera

The cockpit camera will give the player a first person view from the cockpit of the space ship.

The cockpit is a separate game object in the player game object and is housed at the front of the Spaceshp game object.

The Follow target from this camera will be the cockpit game object. The Look AT target will be the joystick game object in the cockpit game object.


The body properties will be again focus on the Offset and Pitch, Yaw, Roll Damping. The follow offset is very minimal as we want the the camera to be in the cockpit.

We want no damping to make the camera locked to the cockpits rotation.


The Aim properties focus on the Look At target. We only focus on the offset.

We increase the Y offset to rotate the camera up when it looks at the joystick.

You can see the end result below, with the camera being locked into the cockpits rotation and movement.

Camera Switching Script

This script will cycle through each camera each time a key is pressed. Create a new script and attach it to the Camera Manager.

We will need access to Cinemachine, so we will need to declare we are using it.

Next, we are going to create an empty array of Cinemachine Virtual Cameras and give it a Serialized Field so we can assign the cameras in the inspector.

After that, we create an int variable that will keep track of what camera is active in the array.

Assign the virtual cameras from the inspector in the order you want them to cycle through with the one you want to start with being last in the array. I am going to have the Follow camera be first, so it will be in the last element and it will move from that camera to the first in the array.

In the Start, we will use a For Loop to get the Cinemachine Virtual Camera component of each camera, which allows use to set the cameras priority. The highest priority camera will be the active camera.

We set the last camera in the array (_camera[i]) priority to the index. This means it will have the highest priority of the rest of the cameras in the array since the index starts at 0 and increments by 1.

Also, we set the _camID to the index (i) value, which puts the value to be equal to the last element value of the array (1).

When the C key is pressed, we set the _camID to the current value + 1 divided by the length of the camera array. Using the % operator will return the remainder of this equation. This line of code will increment the _camID by 1 and when it reaches the last element value of the array it will cycle back to 0.

Using a For Loop, we will increment through each camera in the array and check if the _camID is the same as the index (i). If it is, we set that camera in the array to priority 1 and the rest to priority 0. This makes that camera active and the rest inactive.

If we add more cameras to the array this will continue work without have to change the code. Just remember to set your first camera to the last element in the array and then the second at the first element and so on.