Unity : Cinemachine and Timeline (Part VI)
We continue from last article and see how set the Field of View.
In practice, we hit Space Bar and Field of View changes from 60 to 40, press again and go to 20, press again and it’s back to 20.
The easiest way is to create another If Else statement and check :
If it’s 60 go to 40, if it’s 40 go to 20 and if it’s 20 go back to 60.
Another way can be with Enum
Both cases are fine
And if we want we can use a Switch
As always there is more than one method to find a solution.
95% of the time, when I use an Enum probably I go for Switch statement over the If statement, because Switch + Enum is a great combo, but again, feel free to test different solutions.
And of course we can extract the method like this
In always good to have Update() as clean as possible.
To clean more, we can create three variable of type int for the distance of Field of View
In the code now we change the number with the variables
NOTE : I take here a bit of time to talk about enum.
If we pass the mouse over Away, Medium and Close, we see that every variable correspond to a number.
Enum follow the basics of numeration the collections, so the first is 0, second is 1, and so on.
And if want we can assign a specific number to it
With this we can do something incredible powerful with enum : we can CAST THEM AS INTEGER.
For example, we have three variables to define the distance of Field of View.
With them, we can do this
As we can notice, we can’t assign a variable int to an enum, but if we
CAST IT AS INT, everything works fine!
Why this is so important?
In our specific case maybe is not, but it can help you in some situations.
For example, we have three elements in the enum, and we have three variable of type int.
What can I do now is this :
I create an Array of int [60, 40, 20]
And now we can do the exact opposite : we cast INT as ENUM
Now we can assign this variable to the Filed of View.
This is how powerful can be the enum!
In the next section we talk about Virtual Camera Body Component.