Virtual Cameras w/ Cinemachine in Unity 2020 — Part 3
There are two main approaches to switch between VCams.
The first would be to enable or disable the game objects of each VCam and expect the next highest priority VCam to be the correct VCam to take over.
The second approach is to change the priority settings by either lowering the current VCam’s priority and increasing the desired new VCam’s priority.
In code, I tend to do both.
If a VM isn’t in use currently, I don’t want the extra processing overhead.
As you can see above, I’ve set the VCams’ priorities in descending order.
When the game runs, it will always choose the VCam with the highest priority.
Once I disable that VCam though, it automatically switches to the VCam with the next highest priority.
We can modify the priority of the VCam we wish to be dominant, so long as we keep track of all priorities on all VCams.
As you can see above, changing VCam 4 from a priority of 7 to 12, makes it higher than VCam 1’s priority of 11 and thus VCam 4 becomes active.
Setting it back to a priority of 7 causes VCam 1 to retake control.
VCam Processing Efficiency
One thing to note, even though VCams have very little processing overhead compared to Unity’s normal cameras, they do have overhead.
If you are targeting the mobile platform or other low-end devices, or you’re just an efficiency freak like I can be at times, you may want to disable all VCams other than the currently active VCam by default.
Now, if you are rapidly switching between VCams for some reason, I’m not sure if the processing overhead of starting and stopping VCams so frequently would cause more processing overhead or not.
But, I would make that general assumption and try to avoid it by leaving the necessary VCams as enabled and just changing their priorities.
Controlling our VCams with Code
To control our VCams with code, we’ll need a script on a game object that is always active.
I’ve created the game object, “GameLogic” and a matching script.
Some very basic pseudo code for what we are trying to accomplish is shown above.
Essentially, I want to track when the player hits the numbers 1 through 5 on their keyboard and switch to the corresponding camera.
So we have an if statement for each possible keydown event in our update() method, and I’ve added a Transform array to the class to store our VCams.
Back in the Unity Editor, we drag and drop our VCams onto our newly created array.
We’ll need to add a using declaration for Cinemachine.
I’ve added variables for the VCams transforms as well as each of their Virtual Camera components.
I then have a placeholder Virtual Camera to hold the camera that is to be switched to.
In the Start() method, I assign the Virtual Cameras from the Transforms array and set each game object to inactive except for the first Virtual Camera.
In Update() method, we check for key downs, assign the next camera to switch to, set it to active, and give it a priority that will be higher than all others.
Then we call the ResetVCamsPriorities() method and pass along the newly activated VCam that should remain unaffected.
Here we reset the priority of all the inactive cameras to 10 as well as making sure their associated gameobjects are indeed set to inactive.
If we run our game and hit our 1 through 5 keys, we can see the game is behaving as intended.
We’ll take a break here and continue covering Virtual Cameras in the next article.