Adding State-Driven Cameras for your Animation States.
Cinemachine has a built-in virtual camera for state-driven animations, which makes our lives, as Unity Developers, easier!
On to the demonstration, I have four states I would like to switch cameras to:
- Idle
- Running
- Aiming
- Player Death
I created four Virtual Cameras for each state and set them up to my liking.
Next, I added a “State-driven” camera and placed the four virtual cameras inside of the state-driven game object.
I will go through the over-the-shoulder camera setup for my aiming animation state next.
Over-the-Shoulder (for aiming) Virtual Camera
I created a new virtual camera and called it “CM 3rd Person Aim.” I set the body mode to “3rd Person Follow” so that the camera stays behind the player. For the aim mode, I set it to “Same as Follow Target.”
I experimented with the Rig settings under the body to have that “Over-the-Shoulder” view.
The image above represents the position of the aiming state camera with a noise effect to imitate a hand-held camera.
State-Driven Camera
With all of my Virtual Cameras set, I put them inside of a State-Driven Camera game object. The way this camera works is that it gets the information from your animation states in the Animator window.
In my Animator window, I set my animation states to being:
- Dead
- Grounded
- Airborne
- Crouching
- Aiming
In the state-driven camera settings, I set the Follow Target to my player game object. The Look At is not mandatory since the Aim Mode to all my camera’s set to “Same as Follow Target.”
I also attached the Animator component of the player to the Animated Target. This will give the camera the information it needs to access the animation states.
For the State and Camera properties, I assigned the proper state to each camera. This is where the magic of this camera revolves around.
With everything in place, we now have a functional 3rd person camera system with no programming involved!
Gabriel