< Return to Video

Animator Scripting - Unity Official Tutorials

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

more » « less
Video Language:
English
Duration:
07:23

English subtitles

Revisions