Building a Custom Cinemachine Virtual Camera Zoom Feature with One Button in Unity

Chris Hilton
3 min readJan 6, 2022

Objective: Let’s give our Unity game the ability to zoom in on our Cinemachine Virtual Cameras LookAt target with only one button.

Cycling through the Field of View Zoom feature

We have all played that one game in our lifetime which has the ability to zoom in if your character is using a scope on a gun, binoculars or, it might even be a cutscene/sequence that uses zooming to focus on a significant event in a storyline and you are wondering how they achieved this? Well if it was the latter and they were using Cinemachine to approach this feature, then they were more than likely adjusting the ‘Field of View’ option that is available in Cinemachines Virtual Cameras. Let’s take a look at how we could set this up for our game.

Cinemachine Virtual Camera Component — Lens ‘Field of View’

To find this editable setting in the Unity Editor, we first need to make sure that we have Cinemachine installed in our Unity project and we also need to add a virtual camera to the scene which can be simply achieved by right clicking in the HierarchyCinemachineVirtual Camera.

You will now notice this ‘CM VCam1' game object in your scene and if you look over in the Inspector, you will see the CinemachineVirtualCamera component has automatically been added to this game object:

Let’s now create a script that is going to allow us to change this setting while our game in running.

Creating a One Button Cinemachine Virtual Camera Zoom Feature through Scripting

Firstly as we are going to be accessing Cinemachine’s classes through scripting, we need to make sure we add the Cinemachine namespace:

Next, we are going to grab that component and cache it.

Then let’s build our player input button which for this example I will use the ‘Spacekey. Within this we are going to tell the compiler that every time the player presses this key, reduce the ‘Field of Viewlens settings by 20, until we get down to 0, in which our ‘Ifcheck is then going to set this back to 60. This effectively allows the player to quite quickly cycle through the various zoom levels and back to the start:

ChangeFieldOfView script

This could very simply and easily be extended to allow a second key to decrease the ‘Field of Viewvalue effectively giving the player the ability to go back and forth between say 60 and 40 without the need to go down to 20 and 0.

Further to this, a timed feature could be added where the ‘Field of View’ is slowly decreased from 60 to 40 over a set period of time and then it might snap back to 60 once the cutscene/sequence has ended. This can help to create some very interesting cutscenes/animations that allow events of significant items to be focused on during a narrative/story to create greater immersion.

Have some fun with it!

--

--