-
In this lesson we will be addressing
-
some of the more common uses of the animator class.
-
For complete documentation on the animator class
-
see the documentation linked below.
-
In order to affect transitions between the states
-
of an animator controller we need to set the
-
animator parameters to appropriate values.
-
To do this we need to use some of the
-
functions of the animator class.
-
Here we have a scene with a model Ethan in it.
-
Ethan has an animator component
-
and a script called Ethan Script attached.
-
The animator component is using a
-
controller named EthanController.
-
If we open EthanController
-
we can see that there are 4 states.
-
Idle, Walk, Run and Jump.
-
Additionally there are 2 parameters.
-
A float named Speed and a
-
trigger named Jump.
-
the controller is setup in such a way
-
that Ethan will transition from Idle to Walk
-
to Run based on the Speed parameter.
-
Furthermore, if the Jump parameter is triggered
-
while Ethan is running Ethan will enter
-
a Jump state.
-
All of the animations of the Ethan model
-
are controlled by these 2 parameters,
-
which we will now learn to control
-
with our Ethan script.
-
Inside the Ethan Script we first need to create
-
an animator variable named anim
-
to contain our animator object.
-
We get a reference to this animator object
-
in the start method by typing
-
anim = GetComponent
-
For further details on the GetComponent function
-
see the lesson linked below.
-
Are animator controller is setup
-
so that the Speed parameter will dictate
-
which moving animation is being played.
-
We can use the player's input to
-
control that behaviour.
-
The first thing we will need to do is create
-
a float variable which we will call Move
-
in the update method.
-
Then we will set the Move variable
-
equal to the vertical input axis
-
by typing Input.GetAxis
-
and in parenthesis we will parse the string Vertical.
-
Next we will want to parse the
-
value of the Move variable to the
-
Speed attribute of the animator controller.
-
This is done with the SetFloat function
-
of the animator class.
-
We are using SetFloat because Speed
-
is a float parameter.
-
If it had been a Boolean parameter
-
we would use SetBool.
-
If had been a trigger parameter we would use
-
SetTrigger and so on.
-
For each of these functions there are
-
multiple ways that they can be called.
-
The most common way is to first parse
-
in the name of the parameter you are setting
-
as a string.
-
Then the next item is the value
-
you wish to set that parameter to.
-
In this instance we will call the SetFloat
-
method of our animator variable by typing
-
anim.SetFloat
-
and then in parenthesis the string Speed
-
and our variable Move.
-
Back in Unity we can run our scene.
-
We can see that if we press the W key
-
or the up arrow our model now moves.
-
The Speed parameter and the movement
-
animation played is now controlled by the player.
-
Parsing in a string name for the
-
parameter we wish to set can be inefficient
-
because it means that the animator needs to
-
evaluate the string every time it is called.
-
A faster way of setting parameter values
-
is by using hashIDs.
-
Each string that an animator can use is
-
given a hashID.
-
This is an integer value that is unique
-
for every different string.
-
We can store this value and then set
-
parameters using this ID instead.
-
To do this, first we will need
-
to store the value.
-
Let's create an integer variable named jumpHash
-
to store the hashID in.
-
We get the value for our hash variable by
-
calling the static method of the animator class
-
string to hash.
-
Since this method is static we access it
-
by typing Animator.StringToHash.
-
In the parenthesis we parse in the string
-
we wish to know the hashID value of.
-
In order to make our model jump
-
we will need to set the Jump trigger parameter
-
in the animator controller.
-
In our case we want to make the model jump
-
whenever we press the space bar.
-
So we will start by typing
-
if Input.GetKeyDown and then in the parenthesis
-
we will use KeyCode.Space.
-
Inside our if statement we will set
-
the Jump trigger parameter using the
-
anim.SetTrigger method.
-
Inside the parenthesis we will
-
parse our jumpHash variable.
-
Remember that this variable contains
-
the hasID of our Jump parameter.
-
Note that since Jump is a trigger
-
we do not need to also parse in a value.
-
Back in Unity we can run our scene.
-
Notice that while we are running we can also
-
press the spacebar to jump.
-
We have now utilised both our
-
float parameter Speed and our
-
trigger parameter Jump to make our model
-
more dynamic.
-
Occasionally, when writing the logic
-
behind animation we may want to
-
find out what state the controller is in.
-
Let's say for example we want to be able to
-
set the Jump trigger only when the model
-
is in the Run state.
-
The animator has some functions that we can call
-
to find out what state the controller
-
is currently in.
-
We can do this by creating an
-
animator state info variable
-
which we will call StateInfo.
-
The animator state info class
-
stores the name, name hash,
-
and various other pieces of information
-
about a state.
-
We will set our state info variable equal
-
to the anim.GetCurrentAnimatorStateInfo function.
-
Inside the parenthesis we will parse the index
-
of the layer we want to know more about.
-
Since we are dealing with the base layer
-
we will parse in a 0.
-
Again we could compare the current
-
animation states name with a string
-
but since it is more efficient to compare
-
hashIDs let's do that instead.
-
We will start by creating an integer named
-
runStateHash and setting it equal to our
-
Animator.StringToHash function.
-
We will parse in to the parenthesis
-
the string Base Layer.Run
-
We have to include the base layer. part
-
because we can have states of the same name
-
on different layers.
-
Now we can modify our if statement
-
to also only allow our model to jump
-
if stateInfo.nameHash
-
equals our runStateHash variable.