How to switch between Cinemachine cameras in Unity
One of the best features that comes with cinemachine is the ability to switch between cameras at your discretion. There are a few ways to do this, so let’s cover some examples in this article.
There are five virtual cameras that have been brought into the hierarchy from the cinemachine package.
In a previous article, I covered how using the Solo button is an effective way to show a specific camera in the game view. This is most useful when you are working on that particular camera view.
Each virtual camera is placed roughly around the perimeter of the player.
Here is Camera 1 at the highest Priority level.
Camera 2 with the Solo button pressed.
Camera 3 on Solo.
Camera 4 Solo.
and Camera 5 Solo.
Setting the Priority level is a great way to determine which camera will be acting as the main camera. In this example, I have made vcam1 a Priority value of 11, while making vcams2–5 all a Priority value of 10. The camera with the highest Priority value will be the featured camera showing in the Game view. In this case, vcam1 would be showing in the game view, being it currently has the highest priority level. If I had all 5 cameras set to the same Priority value, the closest camera would become the featured one. When it comes to switching camera with cinemachine, without the help of Timeline, camera Priority levels are your friend.
Disable Virtual Camera Game Object
Another way to change which camera is active is to simply turn others on and off. For this example, the Priority level of these cameras descend in chronological order. Vcam1 has a Priority level of 10, while vcams2–5 have Priority levels of 9–6 respectively. In this scenario, so long as vcam1 is active, it will be the dominant camera showing in the game view. If vcam1 were to become disabled as a game object, vcam2 would assume the lead being it has the next highest Priority level. In the example below, I disabled all but the lowest priority camera, which makes it the one showing in the game view.
Vcam5 is the rear-left shot.
Disable Cameras with C#
For this example, the first virtual camera has a Priority level of 10, and then the other cameras 2–5, have lower priority levels (9–6), with camera 5 having the lowest priority level of 6. This way, the first virtual camera will always be showing so long as it is enabled, because it has the highest priority level. With descending priority levels, as the higher level cameras become disabled, the lower level ones will take over the game view. Here I create a C# script and attach it to the parent Vcams game object.
The first thing is to store the virtual cameras into a variable. Because I have multiple cameras, and I may add or remove them, it makes sense to create an array of game objects to store the cameras. I currently have 5 cameras ready to go, so I reference those in the variable declaration.
A for loop is used to cycle through the camera array, sending a Log message to the console for each camera. This is all triggered by pressing the R button on the keyboard.
The cameras are assigned by dragging them into the inspector.
When the for loop runs, the name of each camera is printed to the console.
Fore reference…camera 1.
Another variable is added for an ID number to reference each camera.
These start at zero, so camera 1 has an ID of zero.
Another Log message is sent to the console for the ID number of each camera.
Now as the for loop runs, each camera has it’s name and then ID number printed.
This new command will set all cameras in the loop to be inactive.
You can see in this Gif, all cameras becoming inactive when the R key is pressed.
In this case, the original main camera is still visible when all virtual cameras are disabled.
This is the main screen you don’t want to accidentally show the player!
Disable an Individual Camera
You can use the “is equal to” == sign, to reference an ID number for a camera. Then place that in an if statement to check for that particular camera before disabling it.
Once the first camera is disabled (ID of zero), the view blends into the next camera with highest priority. In this case it’s camera two.
Referencing the string name of the camera exactly as it appears in the hierarchy is a good way to get around using the ID system if you want to disable a camera. This will provide the same function as the above process.
Show the Last Camera
Because the last camera has an ID of 4, but it also has the lowest priority level, it can be shown by using a condition to check for all ID’s with a value less than 4, and then disable those cameras.
Disable all but Target Camera
The “not equal to” sign != is really handy for filtering out all options that are not the one you want. In this example, all cameras that are not named CM vcam3, are disabled.
Now, pressing the R key turns off all but the 3rd virtual camera.
Then the view changes from the 1st camera, to a blend into the 3rd.
Disabling Multiple Cameras
You can turn off multiple specific cameras by name or number using the or operator | to add cameras into the condition. Here I check to see if the name of the camera before disabling it, and use the or operator to include the names of the first three cameras.
Now pressing the R key in play mode will disable the three cameras with those corresponding names.
Thanks for reading, and have fun changing those cameras!