Unity : Cinemachine and Timeline (Part V)

Matteo Lo Piccolo
4 min readJan 28, 2022


It’s time to see how access the Virtual Camera with C#.

This is my scene.

Now we add a Virtual Camera, and for test we want to access to the field of view

We create a C# script and call it Field Of View aAnd we attach it directly to the virtual Camera

We open it.
To access the Cinemachine, we need
using Cinemachine;

And we need a reference to our Virtual Camera component

And in Start

AT this point, in my scene the Field of View is set to 60, we want change it to 20.

If we look at inspector we see the Field of View property is under

CinemachineVirtualCamera -> Lens -> Field of View

Know that, we can write

And we press Play

Field of view is now 20.

Now we try to expand this concept.

First of all I create another Cube and change its color to red.
Then I create an Array[] of GameObject and I drag and drop both of them in Inspector.

If now we want to use LookAt directly from the code, we can do it like this :

If we go over LookAt, we see that is a PROPERTY, so it has a get and a set.
To use it, after _camera.LookAt

LookAt ask us for a Transform, that’s why we write

With this, at the beginning we focus on Orange Cube.

Expand more :
if we press R we change the target from Orange Cube to Red Cube.
If we press again back to Red Cube

How can we do it?

First line is simply this

After we press R, becuase we are good programmer, we check
if the target is not null

if is not null, LookAt at the first target in the Array.

This works perfectly fine one time, because in Start we LookAt
if we press R

But we need to go back right?
From [1] to [0]

So after the check for null reference, we need another check :
if we look at [0] go to [1]

To finish the logic, we need an else

If we test it

NOTE : I don’t know if in readability level is a good idea, but technically for every if we have here there is only a “one line of code”.
So what we can do is

It’s not “wrong”, many times when I have an If with one line of code I personally prefer to delete the parentheses, but can be happen sistuation like this where you have three, four or five check and when we delete the parentheses it seems a little “odd”, but it’s ok.

In the next article we finish with Aim and start with Body.



Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!