- [Instructor] In this video, we're going to take a look
at how to create a state-driven
camera system in Cinemachine.
We're going to look at the state-driven camera component,
and how we can use an animator to drive camera behaviors,
as well as how to customize the transitions between states.
In this scene, we have a virtual camera,
and it's moving on a track following our character
as they walk across the screen.
If our character stands still,
they move into an idle animation state.
I'd really like to make this idle state more interesting
by changing how our camera looks.
I'd also like a special camera shot
that cuts into our character whenever they pick up an item.
We can use Cinemachine's state-driven camera system
to swap between different virtual camera setups
based on states have any animated component in our game.
So we're going to create different virtual camera behaviors
and transition between them
based on the state our character is in.
First, let's create a state-driven camera in our scene,
by going to Cinemachine, create state-driven camera.
This creates a state-driven camera game object
in our hierarchy.
The virtual camera children property in the Inspector
defines all of the virtual cameras
that our state-driven camera is going to use.
We might not need every Cinemachine camera
in our scene to be driven by this system.
So any cameras that already exists in our scene,
such as our original virtual camera,
will need to be parented to the state-driven camera
in order to be used.
It's also worth noting that we can press
the plus or minus button underneath
the virtual camera children property box
to automatically add or remove new virtual cameras
to a state-driven camera.
After we've set up the shots on each of our virtual cameras,
we're ready to set up our states.
States can be triggered
by any animated component in our scene.
For example, if we were making an action adventure game,
the state system could be driven by an animator
during an environmental set piece event,
such as focusing on an explosion in the background,
or perhaps as an enemy arrives to confront the player.
We could also create an animator specifically for the camera
and swap between states with custom scripted events.
In our game however, I'd like the camera changes
to be triggered by the states in the Players Animator.
So let's assign the Players Animator
to the Animated Target property.
Once we have an animated target,
our state-driven camera can then assign
any of these states to a specific virtual camera.
Let's assign the walking state to our first camera,
the idle state to our second camera.
And then finally, all of the other non-walking
animation states to our third camera.
If we run on game, we can see
that the camera immediately changes
and enters the idle state.
Ideally, we'd like the camera to change
after a period of inactivity.
So let's change the wait property
on our idle state to five.
Cinemachine will then wait five seconds
before transitioning to this camera
after the state has been entered by the animator.
By default, Cinemachine will perform a blend
when changing between the states.
We can define the type of blend performed
and how long the transition will take
by changing the default blend settings in the Inspector.
If however, we'd like to define a specific blend type
to a specific change of state,
such as the cut to a close up when interacting with an item,
we can create a custom blend asset
using the custom blend property and clicking Create.
Once created, we can choose specific blend settings
for each of our state changes.
Now, when we play our scene,
we have a much better looking set of transitions
between each of our camera types.
As you can see, the state-driven camera system
allows us to quickly set up
and define different short types
between different animation states with ease.
For more information on Cinemachine
and for more Unity tutorials, please follow the links below.
Thanks for watching.