Unity Cinemachine: State-Driven Camera

Josh P (Pixel Grim)
3 min readJan 4, 2023

--

Goal: Learn how to use state-driven cameras

As mentioned above, today was learning how to create a state-driven camera and learning how to create a unique effect for each of the player’s animated states.

Like the other cameras, we used in the past, Cinemachine has another pre-set camera called a state-driven camera. Much like the blend list camera, this camera also stores other virtual cameras as children. But one major difference between the two is that this camera is tied to Unity’s animator allowing us to change the camera behavior depending on the player’s animation state. For example, we can have an idle camera, a running camera with a fixed fov, and a camera for when the player aims.

Once again, I added the camera to my hierarchy and additional virtual cameras to the parent. I made sure to add the animator component that belongs to my player and attached it to the state-driven camera.

In this image, I have my state-driven camera setup for the scene. Here I have one animated state for shooting, Idle Walk(3rd Person), then a state for running, and a death state. Each of these states is assigned a child camera.

Once everything is assigned to the state-driven camera. I created a few scripts to transition between the different animated states. For example, my death animation. When I press R, the player dies, and an animation plays. I created a few scripts for the animation state, such as running and aiming. This about covers how I used the state-driven camera.

public class DEAD: MonoBehaviour
{
private Animator animator;
private bool _hasAnimator;
void Start()
{
_hasAnimator = TryGetComponent(out animator);
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
animator.SetBool("Dead", true);
}
else if(Input.GetKeyDown(KeyCode.T))
{
animator.SetBool("Dead", false);
}
}
}

--

--