One of the most important parts of making
the player move is the animator controller.
We will start off by creating a new
animator controller and calling it Player Animator.
Select the Animators folder in the project panel
and click the Create button.
Then choose Animator Controller.
Name it PlayerAnimator.
This animator controller is going to be
relatively simple. We will start out with
the parameters.
If you do not have it open already,
open up the Animator window.
Go to Window - Animator.
Create a float parameter called Speed.
Click the plus button to the right of
Parameters and choose Float.
Name this Speed, with a capital S.
Now we will need three Booleans.
First, one to say whether or not the player
is sneaking. Another to say whether
or not he is dead and finally
one for when the player is shouting.
We will call them Sneaking, Dead and
Shouting respectively.
Now we are ready to start making the states.
We will start by dragging in an idle animation.
This will be our default state.
You will find the FBX of this animation
in the Animations folder,
in the Humanoids subfolder.
Open up Animations in the project panel
and select Humanoid. You will then find
humanoid_idle.
Expand this FBX and drag and drop idle
in to the Animator window.
Because this is our default state it is
highlighted in orange, and we want
to see how this affects our character.
So let's dock our animator below
the scene view,
reselect our character
char_ethan in the hierarchy.
In order to make this animation applied
to our character we need to drop the animator
controller asset on to the control
property of the animator.
Select the Animators folder and drag
and drop the animator that we just made.
Now if we press play.
We will see that the idle clip is applied
to our character.
Press play once more and we will continue.
Reselect the Animator window and this time
we want to add some motion. To get a smooth
acceleration we are going to use a blend tree.
Create a new blend tree by right clicking
in empty space and choosing
Create State - From New Blend Tree.
This is a new state based on a blend tree
and we need to name both the state
and the blend tree. When we are in this view
we can see the state itself,
and we will name it Locomotion.
Then double click on this state to
enter the blend tree.
Now rename the blend tree Locomotion also.
We will add two motion fields to this
blend tree by clicking the plus button
and selecting Add Motion Field.
Next we will add the two animations for
the motion fields. First add Walk
and then Run. We will do this by clicking
the circle select button to the right
of the motion fields, and then choosing
the animation from the list.
Search for Walk
and select it here.
Then close the selection window.
Then click the circle select button to the
right of the second field
and search for run
and select it.
Finally we can compute the thresholds.
To do this uncheck Automate Thresholds
and select Compute Thresholds - Speed.
This will calculate appropriate values
of the speed parameter for each animation
based on their motion. Note the value of the
threshold for Run.
We will be using an approximation of this
later in our Player Movement script.
Next we can make the transitions too
and from these states. Returned to the base layer
and then right click the Idle state.
and click Make Transition.
This gives us a transition to drop
on to another state. Simply click on the
Locomotion state to apply it.
To edit the transition select it.
We then have properties for it in the inspector
and a condition for us to go from
Idle in to Locomotion.
The conditions need to be set so that speed
should be greater than 0.1.
So from the drop down select Speed,
leave the default of Greater
and say 0.1.
We also need Sneaking to be false
So click the plus button to add
another condition,
choose Sneaking and set it to false.
Now we need a transition to go back to
Idle from Locomotion.
So right click on Locomotion,
choose Make Transition and click on Idle.
Select the transition back
and the condition that we need here is
that speed is less than 0.1.
The next state is Sneak. The FBX of the animation
is again in the Humanoid folder
under Animations and is called
humanoid_sneak.
Expand this and then drag on the
animation called Sneak Forward.
We will arrange these a bit more tidily.
You can drag states around in the window
in order to make them neater.
Grab Locomotion and move it out of the way
and then move Sneak Forward up to the left
to form a triangle shape.
You can hold down ALT at any time
to drag around inside the animator window.
For simplicity and consistency let us also
rename our state to simply Sneak.
Make sure it is selected in the animator
and rename it at the top of the inspector
to simply Sneak.
Let us deal with the transitions to and
from Locomotion first.
In both cases we need to make sure that
speed is greater than 0.1
and if we are transitioning to Sneak
then we need the Sneaking parameter to be true.
If we are transitioning away we need it to be false.
Select the Locomotion state,
right click and make transition.
Then select Sneak.
Select the transition to edit it in the
inspector and in the conditions table
make sure that Speed is greater than 0.1
and then add a second condition,
which says that sneaking should be true.
Now we need the transition back.
Right click on Sneak
and click on Locomotion.
Select the transition back
and in the conditions ensure that Speed
is greater than 0.1
as it needs to be in order to walk or run,
then add a second condition
that says that Sneaking must be false.
Now we need the transitions
to and from Idle. To go to Idle from Sneak
we simply need Speed to be less than 0.1.
However to go from Idle in to Sneak
we need that Speed is greater than 0.1
and that sneaking is true. Right click on
Sneak and choose Make Transition
then click on Idle.
Select the transition and set the condition
for Speed to be less than 0.1.
Now right click on Idle and choose Make Transition.
Then select Sneak.
Select the transition and under the
conditions ensure that Speed
is greater than 0.1
and then add a second condition that says
that Sneaking must be true.
The final state that we are going to make
on this layer is Dying.
We do so, as before, by dragging
the animation in to the animator window.
The FBX is in the same folder as before
and is called humanoid_dying.
Expand this FBX to reveal the Dying animation.
We need this state to be near the
Any State as we are going to transition
from this. Rearrange your states so that
Any State is over to the left of our
existing states. Then drag and drop the dying
animation in to the animator window.
The Any State is there to allow us to
transition in to another state without
being in any particular state to begin with.
So, in order to die we could be in any
of the existing states, rather than make
transitions from all of them we can use
Any State to jump out of our other states
in to another state.
So we can right click on Any State
to choose Make Transition
and select Dying. Select the transition
and in the condition we need Dead to be true.
And there he is.
Next we are going to have an additional layer
of animation so that the player can have a
shouting animation whilst in any of the
states in our base layer.
Before we can do that though we need to make
an avatar mask so that only the parts of
the player that need to move do so
when shouting.
You will find the Shouting animation in the
same folder, it is called humanoid_shout.
Beneath this you should see the Shout
animation itself. Select the FBX
and you will see that when playing it back
the character raises his arm to his mouth
to shout. However we do not want
the rest of the motion from this to be
affecting our character, so we will
create the avatar mask.
We will save this in to the Animator folder
as it is used directly with our animator.
Select the animators folder and
create and avatar mask.
We will call this PlayerShoutingMask.
With this asset selected we can edit
it in the inspector. We only need to use the
body mask, so expand this area now.
Now deselect everything other than the
left arm, hand and head.
Now we can make our additional layer of
animation. Everything we have made so far has
been on the base layer. We can add
an additional layer of animation by clicking
on the plus icon to the right of Layers
in the top left of the Animator window.
We will name this new layer Shouting.
Next click the circle select button
next to Mask and choose a mask to apply.
Choose PlayerShoutingMask here.
By adding this second layer of animation
and masking it we will be able to
call it via script during any other state
of animation. Now we are going to add some
states to this layer. The first think we
want to have is an empty state.
Do this by right clicking and choosing
Create State - Empty.
We need this empty state so that when
this layer is not in the Shout state
then nothing is being played back by this layer
and therefore nothing is overriding
the animation on the base layer.
Now we will drag in the Shout animation.
Reselect the Humanoid folder under Animations
and expand Shout. Drag and drop it in to
the animator window.
Finally we need some transitions to and
from these states. The condition for
transitioning in to the Shout state
is that the Shouting Boolean is true.
So right click on the empty new state
and click Make Transition.
Then click on Shout.
Select the transition itself
and under Conditions choose Shouting.
By default it should be set to true.
Then right click on Shout, choose
Make Transition and select the orange empty state.
Select the transition back and you should
see that it is set by default to
Exit Time, which is the default that we need.
This means that when the animation is
finished playing it will simply transition
back in to the empty state.
That concludes our work on the players
animator controller.
Save your scene and save your project now.