Switching Cameras Based on Animation States
Another pre packaged component we can use in Cinemachine is the State Driven Camera.
The State Driven Camera makes use of multiple Virtual Cameras as children of it’s game object. We can then switch between these cameras based upon another objects animation states.
The main component features all the standard Body and Aim features. But in addition, each child Virtual Camera can override these as required.
To create the switch between cameras, we can access a drop down list of all the available animation states, and then assign them a camera, as well as calling a default camera to cover any other states. For further depth we can add priority to each camera and have many different combinations.
A simple example of how Animation State cameras can be used is to have different cameras for the player character, such as a zoomed out, noise driven camera when the player is sprinting, and a more subtle close up when walking or idle.
Alternatively, we can use animation states to enable aiming mechanics for our player, allowing for a first or third person look down the sights of a gun.
I’ve mocked up a grey cube to act as our gun, and have created an animation that ‘draws’ the weapon, then simply made two states for it, with the “Holster” state running in reverse.
Creating a script on our Player, allows us to get access to it’s Animator function.
Simply checking for mouse input then setting the animation bool to be true or false, will allow the animation to occur in our game.
I’ve quickly created two separate 3rd Person Follow cameras, one for each state.
Now when we press our input, we can see our animation state change, as well as our virtual camera blend between it’s two child cameras.