Switching Cameras Based on Animation States

Tyler Smallwood
3 min readDec 12, 2022

--

Objective: Change camera’s based on Animation States

To get camera’s to changed based on the Animation State, we will need to create a State Driven Camera. Go to Cinemachine → State Driven Camera.

In the State Driven Camera, we need to have an animated target. What ever the cameras will be looking at with a Animator on it might be the target.

I am using the capsule for the target this time.

Next we will need to align the two virtual camera’s to where we would like. I would like to hold left shift and the camera zoom in on the right shoulder.

While the other camera is set up for more of a 3rd person camera.

We can move into the Animator. Here we will create two states by right clicking and click Create State → Empty.

We will need a bool parameter, we will call it IsAiming.

Now to add the transition lines from State 1 to 2 and back.

Next we will add the conditions. From State 1 to State 2 add condition if IsAiming is true. From State 2 to State 1 set if IsAiming is false.

We will set up the logic to change states. In our Player Script we will create a variable of type Animator and in Start use GetComponent. Make sure the script is on the same object as the animator.

Now it’s time to check for input on the Left Shift key. If the Left Shift is down, set IsAiming to true. If the Left Shift is up return to false. We set the Animation bools by using SetBool(id, true or false).

Back in the State Driven Camera, we need to set up the State Change. We will need to add two new states.

Next, select State 1 and select the corresponding camera and again for the second one for State 2.

And just like that our camera’s will swap to match the animation we need.

Thank you for reading and have a 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