How to Make a Interactive Camera using Cinemachine and C#

Tyler Smallwood
3 min readNov 8, 2022

--

Objective: Create logic to switch targets and be able to zoom in.

We want to create a camera that can change it’s targets and zoom in to them. To start let’s make sure we have target’s and our Camera in the Hierarchy.

Next we will create our script. In your project Right click → Create → C# Script and name it FOV, or what you would like.

We will need to add the Cinemachine library to access functions of the camera. So at the top of the script we need to add a using statement, “using Cinemachine;”.

Next we will get a reference for the Cinemachine Camera. Next we will create two arrays. One array is the Transform for the target’s and next will be the float for the field of view amount to zoom in. The last thing we will need to have is an int for the current target and current setting FOV.

What we will do next is in Start(), grab the CinemachineVirtualCamera component of the camera. Set the FieldofView to 60 and lookat to the first target.

We what to change the target with the R key and field of view with the space key. So we will need to check for input for both R and Space.

For target we will need to increase the currentTarget by one. Next check to see if the currentTarget is bigger than the array and if it is reset to zero. Lastly we will assign the target to m_LookAt.

The Field of View system is the same as target. Only difference is to assign the array to _cams.m_Lens.FieldOfView.

Now just to call them when the key’s are pressed.

The last thing is to put the script on the camera and assign the targets and values to it.

Now we can change target’s, no matter how many there are, and zoom in on the target’s. Thank you for reading and have wonderful day!

--

--

Tyler Smallwood
Tyler Smallwood

Written by Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

No responses yet