C# and Cinemachine — The Basics

Jack Leavey
3 min readJul 1, 2022

In-game, scripts are going to be the primary method of triggering camera swaps. Let’s look at how to use C# to interact with Cinemachine!

First off, we need to add a new library to the C# script.

Add Cinemachine to the list.

Now we need to get references to our cameras. The simplest way of doing this is using an array.

Gameobject Array

Recompile and back in Unity, you can drag your cameras into your array.

Now we can reference our cameras inside our script.

For a simple example, when I press the “R” key, my first camera will turn off, and Cinemachine will automatically move to the next available camera.

Pressing R disables the first camera, Cinemachine moves to the next camera.

Camera Priority dictates which camera Cinemachine will go to next. However, it is also good practice to have only the virtual camera you need active at a time.

We can also reference anything inside of the Cinemachine camera control window with script. We need a reference to the active camera:

Make sure you have this reference, or the following example will not work.

In Update, I add the following line, setting the FoV to 40 (from default of 60).

Pressing Space.

You can access any of the properties in the CinemachineVirtualCamera component on each camera in script, including transitions, where to look, what to follow, Aim, Noise, and transitions.

With most of the setting, I would recommend taking some time and trying them out, seeing what each does and how you can use it to improve your camera shots!

--

--

Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!