Unity : Cinemachine and Timeline (Part V)

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

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
_targets[0].transform.

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
_targets[0].transform;
if we press R
_targets[1].transform;

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

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matteo Lo Piccolo

Matteo Lo Piccolo

82 Followers

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!