Virtual Cameras w/ Cinemachine in Unity 2020 — Part 4

GameDev Dustin
2 min readFeb 26, 2022

In this article, we’ll cover using C# to change Virtual Camera (VCam) targets and change the Field-of-View (FoV).

First of all, I’m continuing to use my previous articles’ VCams, but will just be utilizing VCam1 for demonstration purposes.

I’ve also added a 2nd cube and given them each separate animations.

The functionality we will implement is for the player to press the “R” key will switch between the two targets and the player pressing the Spacebar key will change the zoom (FoV) from 40 > 60 > 20 > 40.

Continuing to use my GameLogic script from previous articles, we of course have our using Cinemachine declaration and storage for the VCams transforms and VCam components in arrays.

For this article I’ve added the target game objects, current target and current zoom variables.

Our Start() method is unchanged, assigning our _vCams[] components array from the _vCamsTransforms[] array which is set in the Unity Editor.

We’ll also set our target game objects in the Unity Editor as shown above.

Our camera switching with numbers 1 to 5 still remains in the Update() method but is not pertinent to this article.

I’ve added Keydown checks for “R” and Spacebar which call the appropriate methods.

In our SwitchTarget() method, I check what the current target is and switch to the other target game object using _vCams[].LookAt and our new _targetGOs[] array.

In the ChangeFoV() method, I check what the current zoom is and cycle to the next zoom using the _currZoom variable.

With _vCams[].m_Lens.FieldOfView we can easily change the field of view at runtime.

That’s it!

--

--