How to Make an Interactive Camera using Cinemachine and C#

Dennisse Pagán Dávila
5 min readNov 12, 2022

Virtual Cameras can be adjusted via script. You can use this to ease testing by appending particular camera views to keys or even creating gameplay elements. Let’s look at how you can make a camera interactable via C#!

Objective: Learn how to use Cinemachine’s Virtual Cameras with C# and how to use their components via code. In this example, we’ll take a look at changing the camera’s targeting to aim at different objects, and changing the lens’s zoom at the press of a key.

Table of Contents

· Accessing Cinemachine Via Script
· Changing Camera Target on Key Press
The Result
· Lense Zoom-in
The Result

Accessing Cinemachine Via Script

Most things in Unity can be accessed by including the correct namespaces. By adding the cinemachine namespace in your script, you will have access to all its utilities.

Changing Camera Target on Key Press

  1. Now that we have our namespace we can access the virtual camera. Let’s declare our global variable to identify this. This will be a SerializeField so that we can assign this via the Inspector.

2. Let’s get into the main bulk of the programming logic.

  • We want to change the camera’s target whenever we press a key.

If you’ve been following along with the previous articles, you know that Virtual Camera has a Look At component that makes them aim at a given object in the Inspector. So we need to…

  • Access the Look At component via code.
  • Use our desired object’s transform.

We’ll start with the objects, our new global variables will account for that.

3. The last global variable we’ll be adding is a bool to keep track of which object is currently targeted.

Since this example uses two targets, we can simplify this by checking for only one of them, by default when the first target is not active, we know the second target must be active. Don’t worry, we’ll take a look at this in just a bit:)

4. To keep things clean, let’s create a function that handles the input logic for the Look At component.

I know that’s kind of a chunk, but we’ll break it down now!

  • If the R key is pressed, we check whether or not the first target is active. The input check will be in the Update function so don’t worry about it for now.
  • If it is active, we’ll get the Look At component and direct it at target1.
  • Then we reverse the value of our bool variable so that we can cycle between the two. This happens on both if-statements.

Note 1: Remember, objects are transforms which is why we use that here to identify the object.

Note 2: You can get the Look At component in the Start Function as well if you like, but it’s more optimized to get this component when and only if it’s going to be used.

  • Else if — if target1 was not active, then we get the Look At component and direct it to target2.

5. Alright, all that’s left of the script is to call the Switch Target function with our Input check in the Update.

6. Assign your respective variables in the Inspector. We made these fields available by using SerializeField on our global variables.

There are instances in which you’ll want to use more complex structures such as lists or arrays but this example is simple enough to stick to object variables.

The Result

When I press the R key, you can see the Look At/aim indicator(yellow square) switch between the objects.

Lense Zoom-in

You can also do some pretty interesting stuff by simply modifying our code a bit. Let’s have a quick look at this!

If I were to use the Field of View Component instead to change the zoom at the press of a key, it would look something like this:

  • We decrease the zoom(zoom-in) by 20 units.
  • This is stored in a variable so that it can be reused.
  • We check if the FOV(field of view) is less than 10 so that we can reset it to how it was at the start(the starting value was set to 60 via the Inspector)

The Result

I hope you have found this information valuable! Follow me for more Unity Development articles! :) I am a video game narrative designer on a journey to learn more about Unity Development and Software Engineering.

Check out my LinkedIn and Twitter!

--

--

Dennisse Pagán Dávila

Software Engineer that specialize in Game Development. Currently looking for new opportunities. LinkedIn: https://rb.gy/xdu8nu