< Return to Video

2D Roguelike 8 of 14 : Player Animator Controller

  • 0:02 - 0:04
    In this video we're going to setup our
  • 0:04 - 0:06
    player animator controller so that
  • 0:06 - 0:08
    we can control what animation
  • 0:08 - 0:11
    is playing via a script.
  • 0:11 - 0:13
    The first thing that we're going to do is to add
  • 0:13 - 0:16
    the player prefab back to the hierarchy.
  • 0:16 - 0:18
    Let's go to Prefabs
  • 0:20 - 0:24
    select the player and drag to the hierarchy.
  • 0:25 - 0:30
    Next we're going to open our player animator controller.
  • 0:30 - 0:32
    Let's go to Animations
  • 0:33 - 0:35
    Animator Controllers
  • 0:36 - 0:38
    and double click Player.
  • 0:39 - 0:42
    Here we have the animations that we created
  • 0:42 - 0:44
    earlier which have been added to the animator
  • 0:44 - 0:46
    controller has states.
  • 0:46 - 0:48
    Let's lay these out so that we can see them a little more
  • 0:48 - 0:50
    clearly and we'll move the PlayerIdle up,
  • 0:51 - 0:53
    the PlayerChop will be over here and
  • 0:53 - 0:55
    the PlayerHit will be over here.
  • 0:55 - 0:59
    The PlayerIdle is orange because it's the default state.
  • 0:59 - 1:01
    So when the game starts playing
  • 1:01 - 1:04
    the player's idle animation is going to play.
  • 1:04 - 1:06
    What we're going to do is setup the transitions
  • 1:06 - 1:09
    when the PlayerIdle animation will stop playing and will
  • 1:09 - 1:11
    transition to either the Chop animation,
  • 1:11 - 1:13
    when the player attacks a wall,
  • 1:13 - 1:15
    or to the Hit animation when the player
  • 1:15 - 1:17
    is attacked by an enemy.
  • 1:17 - 1:19
    The first thing that we're going to want to do is setup
  • 1:19 - 1:21
    some parameters which are going to
  • 1:21 - 1:23
    be what we're going to use to trigger
  • 1:23 - 1:26
    those animations from our scripts.
  • 1:26 - 1:28
    You may need to click the Parameters button to
  • 1:28 - 1:30
    make the parameters visible,
  • 1:30 - 1:32
    and then we'll hit the + button
  • 1:32 - 1:35
    and add a parameter of the type trigger.
  • 1:36 - 1:38
    This first one is going to be called playerChop.
  • 1:39 - 1:42
    A trigger is like a boolean except it will
  • 1:42 - 1:46
    immediately be reset after it's used in a transition.
  • 1:46 - 1:48
    So it's a boolean that's going to remain true
  • 1:48 - 1:50
    for the frame in which it's called
  • 1:50 - 1:52
    and then become false again.
  • 1:53 - 1:56
    We're going to create another one called playerHit.
  • 1:56 - 1:59
    Next we want to create some transitions between our states.
  • 1:59 - 2:01
    Let's right click PlayerIdle,
  • 2:02 - 2:04
    select Make Transition,
  • 2:04 - 2:07
    and drag to our PlayerChop state.
  • 2:08 - 2:11
    We're then going to right click on PlayerChop
  • 2:11 - 2:13
    and make a transition back from
  • 2:13 - 2:16
    PlayerChop to PlayerIdle.
  • 2:16 - 2:18
    This means that PlayerIdle can transition
  • 2:18 - 2:21
    to PlayerChop and PlayerChop
  • 2:21 - 2:24
    can then transition back to PlayerIdle.
  • 2:24 - 2:26
    We'll click on the transition from PlayerIdle to
  • 2:26 - 2:29
    PlayerChop to make it visible in the inspector.
  • 2:29 - 2:33
    The transition from PlayerIdle to PlayerChop
  • 2:33 - 2:36
    is not going to have an exit time.
  • 2:36 - 2:39
    We're going to uncheck Has Exit Time.
  • 2:39 - 2:41
    What this means is that we're not going to wait
  • 2:41 - 2:43
    for the PlayerIdle animation to finish
  • 2:43 - 2:47
    or to reach any certain point before we can
  • 2:47 - 2:49
    transition to PlayerChop.
  • 2:49 - 2:51
    We're going to want the transition to happen
  • 2:51 - 2:55
    abruptly from PlayerIdle to PlayerChop
  • 2:55 - 2:58
    regardless of where we are in the Idle animation.
  • 2:58 - 3:00
    We're also going to open the settings and
  • 3:00 - 3:03
    set the transition duration to 0.
  • 3:04 - 3:06
    We're going to use a transition duration of 0
  • 3:06 - 3:09
    when we're working with sprite-based animation.
  • 3:09 - 3:11
    When we're working with 3D animation
  • 3:11 - 3:13
    it's common that we may want to blend
  • 3:13 - 3:15
    between animation states
  • 3:15 - 3:18
    but with sprite-based animation that's not possible
  • 3:18 - 3:20
    so we're going to want to immediately transition
  • 3:20 - 3:22
    from one state to another.
  • 3:23 - 3:25
    Next we're going to determine what is going
  • 3:25 - 3:27
    to cause this transition to happen.
  • 3:27 - 3:29
    In our list of conditions here we're going to click
  • 3:29 - 3:32
    the + button to add a new condition
  • 3:32 - 3:34
    which will determine when the transition
  • 3:34 - 3:36
    will be made.
  • 3:37 - 3:40
    A list of conditions will appear here,
  • 3:40 - 3:42
    in this case PlayerChop, which is the
  • 3:42 - 3:45
    default is actually the condition that we want.
  • 3:45 - 3:48
    If we wanted to change it we could choose from the list
  • 3:48 - 3:51
    and choose either PlayerChop or PlayerHit.
  • 3:51 - 3:53
    Now when the PlayerChop trigger is
  • 3:53 - 3:55
    set from our player script we'll
  • 3:55 - 3:58
    transition from PlayerIdle to PlayerChop.
  • 3:59 - 4:02
    For our transition from PlayerChop
  • 4:02 - 4:06
    back to PlayerIdle here we're going to keep
  • 4:06 - 4:08
    Has Exit Time active but we're going
  • 4:08 - 4:11
    to set it to 1, meaning that the PlayerChop
  • 4:11 - 4:13
    animation is going to need to be completely
  • 4:13 - 4:15
    finished before we're going to transition
  • 4:15 - 4:17
    back to PlayerIdle.
  • 4:17 - 4:21
    We're also going to set the transition duration here to 0.
  • 4:22 - 4:24
    Because we set Has Exit Time
  • 4:24 - 4:26
    to true from PlayerChop
  • 4:26 - 4:28
    to PlayerIdle we're not going to
  • 4:28 - 4:30
    need any additional conditions,
  • 4:30 - 4:32
    the transition will be made
  • 4:32 - 4:34
    when the exit time is reached.
  • 4:35 - 4:37
    Let's setup the same things for PlayerHit.
  • 4:39 - 4:40
    We'll select PlayerIdle,
  • 4:41 - 4:43
    choose Make Transition,
  • 4:43 - 4:45
    and drag to PlayerHit.
  • 4:46 - 4:49
    We'll click on PlayerHit, right click,
  • 4:49 - 4:51
    choose Make Transition,
  • 4:53 - 4:55
    and transition back to PlayerIdle.
  • 4:57 - 5:00
    We'll highlight the transition from PlayerIdle
  • 5:00 - 5:03
    to PlayerHit, and here we're going to set
  • 5:03 - 5:05
    Has Exit Time to false
  • 5:05 - 5:07
    because we want to interrupt the
  • 5:07 - 5:11
    PlayerIdle animation whenever the player is hit.
  • 5:11 - 5:15
    And we're going to set our transition duration again to 0.
  • 5:16 - 5:19
    In our list of conditions we're going to hit the + button,
  • 5:21 - 5:24
    and select PlayerHit from the drop down.
  • 5:25 - 5:28
    For our transition back to PlayerIdle.
  • 5:29 - 5:31
    We're going to use Has Exit Time because
  • 5:31 - 5:33
    we want the animation to finish playing
  • 5:33 - 5:36
    setting Exit Time to 1 because we want it to
  • 5:36 - 5:38
    transition at the end of the animation,
  • 5:38 - 5:41
    and we're going to set the transition duration to 0.
  • 5:42 - 5:45
    Before we move on we're going to give this a quick test.
  • 5:45 - 5:47
    Let's temporarily dock the animator window
  • 5:47 - 5:49
    down here on the bottom of the screen.
  • 5:50 - 5:52
    Play our game, and what we'll do
  • 5:52 - 5:54
    is we're going to manually fire the
  • 5:54 - 5:56
    triggers and see what happens.
  • 5:57 - 5:59
    First we'll try PlayerChop.
  • 6:00 - 6:02
    And then we'll try PlayerHit.
  • 6:03 - 6:05
    Excellent, so we can see that those are working.
  • 6:06 - 6:09
    Now that we've got our player animator controller working
  • 6:09 - 6:11
    we're going to move on in the next video
  • 6:11 - 6:13
    to writing the player script.
Title:
2D Roguelike 8 of 14 : Player Animator Controller
Description:

more » « less
Duration:
06:17

English subtitles

Revisions