Switching Cameras Based on Animation States

Let’s take a look at using state driven Cinemachine virtual cameras, to help our player switch between cameras, while aiming at a target.

Start by adding two new animations to your Project folder. Here I add one for Aiming, and another for Idle.

Drag the Idle animation on to the 3D model that you want to have the camera change while aiming.

If there is not already an Animator on the object, one will be generated when you drag the animation into the inspector.

There should now be two animations and an animator controller in the Project folder.

Double-click the Animator controller on the 3D model to open your Animator window. The first animation you drag in, will be the default animation (you can change that later if desired). For this example, the default animation state is the idle state.

Create another state for the aiming animation state, and add a transition to it from the idle state.

Add another transition back to the idle state, from the aiming state.

Now it’s time to add a bool Parameter to determine if the Player is or is not, aiming. The name isAiming should do just fine.

The first condition moving from the standard 3rd person camera to the aiming camera, requires the isAiming bool to be true.

The second transition from the aiming camera back to the default 3rd person camera, needs the isAiming bool to be false.

Navigate to the Hierarchy and add a Cinemachine State Driven Camera.

There should be one virtual camera as a child by default. This will be the main 3rd person camera. I add another camera to be the close aiming cam.

The default 3rd person camera sits back a little on the Z-axis.

The aiming camera sits just over the right shoulder of the Player.

Select the State Machine Camera and add two new states.

This error is letting us know that we need a Target with an Animator component, to be assigned to the State Machine camera.

I drop the Player 3D model into the Animated Target assignment box.

Under the State header for each camera, there is an assignment drop down. The first camera is the Idle state cam, while the second camera is the Aiming state. Under Camera, the designated camera also needs to be assigned with the drop down selector.

To get rid of any lag when changing between animation states, make sure your transitions have Has Exit Time disabled.

To test this out, I make a new script called AimCamera.

This new Aim Camera script can be assigned to the player.

Here is the Debug.Log message from the code snippet below, telling me that I successfully got the Animator component.

At the top of the script a variable is declared for the Animator. Being the script is on the same object as the Animator, the GetComponent call is rather easy. I then null check the Animator and print a message to the console for if it is null or not.

Here in void update, the right mouse button is checked. When pressed, the playerAnimator is sent a message to Set the Bool for isAiming to be true. When the right mouse button is released, the bool is reset back to false.

Now testing in the game view, you can see that the camera animations are very responsive to user input.

I hope you enjoyed this introduction to changing cameras based on animation states, and as always, thanks for reading along!




I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

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
Jared Amlin

I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.