Cinemachine: Making Interactive Cameras Using Cinemachine & C#
Intro
In this article, we are going to cover how to access Cinemachine’s Virtual Cameras using scripts to make them interactive.
This will be shown using a few examples:
- Switching Camera Look At & Zoom
- Switch 3rd Person to Orbital Camera
- Switching Cameras Using Triggers
Each example will show the basics in getting started in added custom cameras to your game.
Camera Target Look & Zoom
This example will have the virtual camera switch looking at two objects by pressing a button and zooming in on the object by pressing another button.
We need to create a virtual camera first.
We need to be able to access the virtual cameras Look At and Field of View (zoom). In the script, we need to access the Cinemachine library and we will create a Game Object/Cinemachine Virtual Camera type variable with a serialized field attribute. This will allow us to assign the virtual camera in the inspector.
In the inspector, we will attach the script to the virtual camera and assign the same camera to the script.
We will need to grab the two objects (targets) the camera will look at, so in the script, we will declare two Game Object type variables with Serialized Field attributes.
Assign the two objects to the script in the inspector.
We will also need to a boolean variable to know when we should look at the first object(target) or the second object(target).
In Start, we can access the different properties of the virtual camera by appending the virtual camera variable using dot (.). We want to access the Look At property and we can either set or get the transform we want the camera to look at and in this case, we are going to set it to target 1’s transform.
We are looking at the first target, so we can set the bool true.
Now, when the R key is pressed the boolean is checked. If we are already looking at the first target, we will have the virtual camera (_cam) look at (.LookAt) and we can set it to target 2’s transform. The targets are flipped flopped if we are not looking at the first target.
Zoom
With the same script, we want to be able to have the camera zoom in the target. This requires changing the Field of View value (higher value = zoom out, lower value = zoom in).
The Field of View is starting at 60. When the Z key is pressed, we are going to access the Field of View value by appending virtual camera variable (_cam) with (.m_Lens.FieldOfView).
We subtract 20 each time the Z key is pressed and if that value goes below 10, we set it back to 60.
Switch 3rd Person to Orbital Camera
In this example, we are going to switch between two different types of virtual cameras looking at the same target.
We are going to switch between the cameras by turning on or off the game object. Now, we will need Game Object type variables and serialized them so we can assign them in the inspector. This method of switching between cameras doesn’t require Cinemachine library.
You will setup your cameras and assign to the script.
It is important to understand the virtual camera that is the main camera is the one with the highest priority. So, the camera I want to start with be set to a higher priority than the one we will switch to.
In Start, since we stored the virtual cameras as Game Object types we can use SetActive() method to turn them on or off. We set the one camera on by passing in true and the second camera off by passing in false.
We use this logic to have one camera turn on and the other turn off when the mouse right button is held down and it will reverse when the button is released.
Switching Cameras Using Triggers
In this example, we are going to switch between cameras using the priority values and having the environmental triggers make the switch.
Create a few cube objects and disable the Mesh Renderer and place them in your scene. When player enters these areas that will trigger the cameras to switch.
Make sure for each of the trigger box collider’s, Is Trigger is enabled.
In the script, we will need access to the Cinemachine library and we are going to create an empty array of Cinemachine Virtual Camera types and serialize it.
Assign the virtual cameras you want to switch between in the inspector.
We are going to create a method that will allow you to pass in the name of the camera to you want to have top priority.
This method will take in a string parameter, which will be the name of the camera. Using a For Each loop we loop through each element (cam) in the array (_cameras). If the camera name that is passed in is not the same as the one in the array we want, we set those camera’s priority value to 1 and the camera we want we be set to priority 10.
We create a script to put on each of the trigger objects, so we can call that method and pass in the new camera. So we add a Cinemachine Virtual Camera type variable.
Assign the camera you want to switch to when the player enters that area.
Using On Trigger Enter, we check to see if the other Collider is the Player (make sure to tag your Player with the player tag) and grab the player component so we can access its method. Then we call the method and pass in the camera’s name that is attached to that trigger object, which will make reset the other cameras priority to 1 and set its priority to 10.
Conclusion
There are three ways you can make cameras more interactive and different ways you can switch between cameras and modify their properities.