Working with Cinemachine
Learning how to switch between multiple virtual cameras
Now that we have the basics understandings of virtual cameras and their ability to look at directed objects. (if not, click here to learn more) We can finally put the knowledge to test, especially when applied to a real world situation and one such situation is working with multiple virtual cameras.
An example would be that the player has triggered a cutscene within your game, and that cutscene has several focus shots of different angles, instead of animating the Main Camera to move around, we can just create several virtual cameras stations at the correct position and allow the Main camera to inherited the virtual camera data when required to. Remember that virtual cameras are NOT actual cameras, instead they can be treated like pylons with data such as position, rotation that can be inherited by the main camera.
How to switch between Virtual Cameras:
To understand how we can swap between virtual cameras, we need to understand what are “Priorities” are. Priority is basically a determining factor for which virtual camera is the dominate one. The higher the Priority the higher the chances that the Main Camera is going to inherit its data.
(ie. If I have one Virtual Camera with an priority of 20 and another virtual camera with an priority of 18, the camera with the priority with 20 is going first)
Now if I were to turn off the camera with the highest priority, Unity is smart enough to make the Main camera inherit the next camera with the second highest priority.
With this knowledge we can create a C# script that can toggle on and off the virtual cameras that is tied to the input system. (so when I push any button, the virtual cameras would be turned off)
In the newly created C# script, I first created a new array of Game Objects called virtual cameras. This will contain all the virtual cameras within the scene. With the “Set Active” function we are going to manually turn on and off these cameras that is aligned with our input system.
Now with the array system in place, in the Update method, we are going to trigger the toggle between Virtual Cameras with the R and T buttons by using the Set Active function.
And with the script active, we can test it out and see that the Main camera is inheriting the proper Virtual Camera settings when pushing the R and T buttons.
This can be further customized when you add in priorities. Right now I have both Camera 1 and 2 turn off, and since the 3rd camera has the 3rd highest priority it will take control. But if I were to make the 4th virtual camera have an higher priority the 4th camera will suddenly take control over the 3rd virtual camera, even thought it was 3rd in line.