State Driven Virtual Camera in Unity 2020

GameDev Dustin
4 min readFeb 28, 2022

Cinemachine State Driven Camera

“The Cinemachine State-Driven Camera component activates a child Virtual Camera when an animation target changes states.

For example, consider your avatar’s local-motion system and orbit camera.

Your game feels more alive to the player when the camera shakes more as your avatar runs.

When the avatar walks, blend for example to a Virtual Camera with more damping.

The animation target for a State-Driven Camera is a GameObject with an Animator component controlled by an Animator Controller.

Assign normal Look At and Follow targets to each child Virtual Camera.

If a child Virtual Camera has no Look At or Follow target, State-Driven camera uses its own, respective targets.

State-Driven Camera has a list that assigns child Virtual Cameras to animation states.

You can define default and custom blends between the State-Driven children.

In the Inspector, the State-Driven camera lists its Virtual Camera children.

Use this list to add and delete child Virtual Cameras, and assign priorities.”

Adding the State Driven Camera to your scene is straight-forward, just click Cinemachine > Create State-Driven Camera.

To use a state driven camera, you need more than one state!

So, I’ll add a second animation to my Target2 game object.

Not shown above, but I animated Cube_AltMovement so that the cube would move side to side rather than up and down during this animation.

In the Animator window, we’ll assign a transition back and forth between each animation state.

Let’s add a Boolean parameter.

Now if we were to click on our states in Animator, our Inspector tab will show that they automatically transition back and forth between the 2 animations.

We are going to set a condition so that this does not always happen.

In our original state 1, or in my case, “Cube_Movement”, we want to add the bool as a condition and leave it as true.

In our State 2, or “Cube_AltMovement”, we add the bool and set it to false.

So, if we hit play now on our scene, only the Cube_Movement animation, or state 1, is playing in a loop.

Back to our StateDrivenCamera1 game object, we’ll add a 2nd child VCam.

I want to make it obvious when these cameras switch between each other for demonstration purposes.

So, the first child VCam will be a Group Composer camera that only aims at both the orange blue cubes.
The 2nd will be a 3rd person follow that gets right up on the blue cube.

The trigger to change cameras will occur when the blue cube changes animation states.

We tweak our 2nd child VCam as previously stated.

Now we’ll drop our blue cube, “Target2”, which is an animated object, onto the “Animated Target” field.

We’ll add two new states and set one to each animation and each camera.

Normally, the Boolean value would be changed due to some code source in our game, but just for demonstration purposes, we can just toggle it off an on in our Animator window.

As you can see, when the state changes between animations, it also changes between VCams as we have properly setup our State Driven Camera!

Excellent.

Now, this example might not have a direct use-case that comes to mind, but you can imagine other use-cases.

For instance, you could have two third person cameras following the player character.
When the player character animates to aim its weapon, this trigger a state change and you could switch to a more zoomed in 3rd person camera with slight offset adjustments.

--

--