< Return to Video

Animator Sub-state machine hierarchies - Unity Official Tutorials

  • 0:00 - 0:02
    When creating complex state-based logic
  • 0:02 - 0:05
    systems in an animator controller
  • 0:05 - 0:07
    it can greatly help organisation to use
  • 0:07 - 0:09
    a hierarchy of state machines.
  • 0:09 - 0:11
    To create a state machine right click
  • 0:11 - 0:13
    on the background on the animator window.
  • 0:13 - 0:16
    and select Create Sub-State Machine
  • 0:21 - 0:23
    This will create a state machine within the current
  • 0:23 - 0:25
    layer or state machine level.
  • 0:26 - 0:28
    Note the hexagonal shape of sub-state machines
  • 0:28 - 0:31
    versus the standard rounded rectangular shape
  • 0:31 - 0:33
    of individual states.
  • 0:33 - 0:35
    Let's look at an example of when they are useful.
  • 0:36 - 0:38
    Here we have an animator controller
  • 0:38 - 0:40
    that's starting to look cluttered
  • 0:40 - 0:42
    so before many more states are added to it
  • 0:42 - 0:44
    it should be tidied up.
  • 0:44 - 0:46
    To do this some of the states
  • 0:46 - 0:48
    can be placed in to a state machine.
  • 0:48 - 0:50
    In this case we could group all of the
  • 0:50 - 0:52
    states that are to do with being Airborne
  • 0:52 - 0:54
    in to one sub-state machine.
  • 0:54 - 0:56
    To do this, first we make the state machine
  • 0:56 - 0:58
    by right clicking on the background of the
  • 0:58 - 1:02
    animator window and selecting Create Sub-State Machine.
  • 1:05 - 1:07
    We can rename the state machine by clicking on
  • 1:07 - 1:09
    it and changing it's name in the inspector,
  • 1:09 - 1:10
    just like single states.
  • 1:11 - 1:14
    Next we need to put some states in to this new state machine.
  • 1:15 - 1:16
    We do this by selecting all the
  • 1:16 - 1:18
    states that we want to move and dragging them
  • 1:18 - 1:20
    on to the state machine.
  • 1:20 - 1:23
    For this example we want to move Leap Takeoff,
  • 1:23 - 1:26
    Leap Airborne, Falling and Landing states.
  • 1:27 - 1:29
    Note the + icon that appears next to
  • 1:29 - 1:31
    your cursor as you are dragging.
  • 1:35 - 1:38
    When the states have been added to the state machine
  • 1:38 - 1:41
    they will disappear from the current state machine or layer.
  • 1:41 - 1:43
    To see them again in order to edit
  • 1:43 - 1:45
    any of their properties for example
  • 1:45 - 1:48
    simply double click on the sub-state machine.
  • 1:51 - 1:53
    To go back to the containing layer
  • 1:53 - 1:56
    or state machine double click on the Up node.
  • 1:56 - 1:58
    Note that the breadcrumb at the top of the window
  • 1:58 - 2:00
    shows you where abouts you are
  • 2:00 - 2:02
    within the state machines.
  • 2:09 - 2:12
    State machines can be used in a hierarchical structure.
  • 2:12 - 2:14
    This means that within each layer of an animator
  • 2:14 - 2:17
    controller there could be several state machines.
  • 2:18 - 2:20
    Each of those could, in turn, have
  • 2:20 - 2:22
    sub-state machines within them, and so on.
  • 2:26 - 2:28
    Transitions can go from states outside
  • 2:28 - 2:30
    to those inside a state machine.
  • 2:30 - 2:32
    We can see from this example that when
  • 2:32 - 2:35
    our states were added to the sub-state machine
  • 2:35 - 2:39
    their transitions were retained.
  • 2:44 - 2:46
    To make a new transition to a state machine
  • 2:46 - 2:48
    right click on the state or state machine
  • 2:48 - 2:50
    from which you are transitioning
  • 2:50 - 2:52
    and select Make Transition.
  • 2:52 - 2:54
    Then left click on the state machine you wish
  • 2:54 - 2:56
    to transition to.
  • 2:57 - 2:59
    From here a pop-up menu will appear
  • 2:59 - 3:02
    offering you the choice to transition to any state,
  • 3:02 - 3:05
    or state machine within the selected state machine.
  • 3:05 - 3:07
    This includes the state machine itself.
  • 3:10 - 3:12
    One way to transition out of a state machine
  • 3:12 - 3:14
    is to use the Up node.
  • 3:14 - 3:16
    This works like a link to all states
  • 3:16 - 3:18
    and state machines that are not contained
  • 3:18 - 3:20
    in the current state machine.
  • 3:20 - 3:22
    The up node is labelled Up
  • 3:22 - 3:24
    followed by the name of the parent state machine.
  • 3:25 - 3:27
    If there is not parent state machine
  • 3:27 - 3:29
    it will show the name of the layer it is in.
  • 3:30 - 3:32
    In this example there was no parent
  • 3:32 - 3:34
    state machine so the up node is labelled
  • 3:34 - 3:36
    (Up) Base Layer.
  • 3:36 - 3:38
    To make a transition out of a state machine
  • 3:38 - 3:40
    right click on the state or state machine
  • 3:40 - 3:42
    that you wish to transition from
  • 3:42 - 3:45
    and select Make Transition
  • 3:45 - 3:47
    then left click on the up node.
  • 3:48 - 3:50
    You are then offered the same choices as
  • 3:50 - 3:53
    when transitioning in to a state machine.
  • 3:53 - 3:55
    Only this time it is a choice of the states
  • 3:55 - 3:56
    and state machines above the
  • 3:56 - 3:58
    current one you were viewing.
  • 4:00 - 4:02
    In every layer and state machine
  • 4:02 - 4:05
    there are green entry and red exit nodes.
  • 4:05 - 4:08
    These are used to encapsulate state machines
  • 4:08 - 4:10
    so that they have less dependence on
  • 4:10 - 4:12
    states or state machines.
  • 4:12 - 4:14
    They represent an alternative to using the up node.
  • 4:15 - 4:17
    Transitions to and from state machines
  • 4:17 - 4:19
    can either use the up node or
  • 4:19 - 4:20
    the entry and exit nodes.
  • 4:21 - 4:22
    Using the up node,
  • 4:22 - 4:24
    transitions happen directly from
  • 4:24 - 4:27
    state to state or from state to state machine.
  • 4:28 - 4:30
    When using entry and exit nodes
  • 4:30 - 4:32
    the transitions are to and from
  • 4:32 - 4:34
    the state machine itself.
  • 4:34 - 4:36
    A transition to a state machine
  • 4:36 - 4:38
    will come in at the entry node
  • 4:39 - 4:41
    and leave via the exit node.
  • 4:48 - 4:50
    This may mean designing your transitions
  • 4:50 - 4:52
    in a very different way.
  • 4:52 - 4:55
    Instead of having a single transition through the up node
  • 4:55 - 4:56
    there maybe be many.
  • 4:56 - 4:58
    Let's take a look at an example.
  • 4:58 - 5:01
    This is the same controller we saw in our earlier example.
  • 5:03 - 5:06
    But the Locomotion and Airborne states
  • 5:06 - 5:08
    have been put in to their own state machines.
  • 5:09 - 5:11
    However instead of using transitions
  • 5:11 - 5:13
    between each of the states as before
  • 5:13 - 5:16
    entry and exit nodes are used.
  • 5:16 - 5:18
    In the previous example there were transitions
  • 5:18 - 5:21
    from Standing Locomotion to Leap Take Off and Falling.
  • 5:24 - 5:26
    And a transition from Landing
  • 5:26 - 5:28
    to Standing Locomotion.
  • 5:29 - 5:31
    The condition to go from Standing Locomotion
  • 5:31 - 5:34
    to Leap Take Off is the Jump trigger.
  • 5:36 - 5:38
    However, once we have encapsulated a
  • 5:38 - 5:40
    locomotion state in to a state machine
  • 5:40 - 5:42
    it no longer matters what state we are
  • 5:42 - 5:44
    transitioning to.
  • 5:44 - 5:46
    Instead it just matters that we no longer
  • 5:46 - 5:48
    want to be in this state machine.
  • 5:48 - 5:50
    So we transition to the exit node
  • 5:50 - 5:52
    with the jump trigger as a condition.
  • 5:57 - 5:59
    Back in the complex animator controller
  • 5:59 - 6:01
    the condition to go from standing locomotion
  • 6:01 - 6:03
    to Falling is that the parameter
  • 6:03 - 6:05
    "Grounded" is false
  • 6:07 - 6:09
    In the entry and exit node example
  • 6:09 - 6:11
    the same condition is required for a second
  • 6:11 - 6:14
    transition to exit the locomotion state machine.
  • 6:21 - 6:23
    Since we only have two state machines
  • 6:23 - 6:26
    we can assume that if we are leaving one state machine
  • 6:26 - 6:28
    we want to go in to the other
  • 6:28 - 6:32
    As such there are no conditions for transitions between them.
  • 6:34 - 6:36
    We have two transitions from the entry node
  • 6:36 - 6:38
    of the Airborne state machine.
  • 6:38 - 6:40
    The first is the default transition
  • 6:40 - 6:42
    from the entry node.
  • 6:42 - 6:45
    We can see this because it is orange in colour,
  • 6:45 - 6:47
    instead of the normal white.
  • 6:47 - 6:50
    This transition will be used when no other transition
  • 6:50 - 6:52
    from the entry node can be used
  • 6:52 - 6:54
    or if it's the only available transition.
  • 6:55 - 6:56
    The other transition is greyed out.
  • 6:57 - 6:59
    When a transition is grey like this
  • 6:59 - 7:01
    it means that it has no knowledge of the state
  • 7:01 - 7:02
    it is coming from.
  • 7:02 - 7:04
    This particular transition
  • 7:04 - 7:06
    is coming in to the state machine from
  • 7:06 - 7:08
    the entry node, so it doesn't know which
  • 7:08 - 7:12
    state it is coming from, as it could be from any source.
  • 7:12 - 7:15
    We can see that the transitions between the locomotion
  • 7:15 - 7:18
    and Airborne state machines were also greyed out.
  • 7:20 - 7:22
    Again this is because they don't know
  • 7:22 - 7:24
    which state they are transitioning from.
  • 7:25 - 7:27
    When a transition is greyed out, the only
  • 7:27 - 7:29
    editable aspect are it's conditions.
  • 7:29 - 7:31
    Since the transition doesn't know which state
  • 7:31 - 7:33
    it is coming from any other details
  • 7:33 - 7:35
    about the transition would be meaningless.
  • 7:35 - 7:38
    To control the duration and other aspects
  • 7:38 - 7:41
    use the transition from the originating state.
  • 7:43 - 7:45
    The transition to Falling has the condition
  • 7:45 - 7:47
    that "Grounded" is false.
  • 7:48 - 7:50
    These two transitions combined
  • 7:50 - 7:52
    mean that when we enter this state machine
  • 7:52 - 7:54
    if grounded is false Falling will be played.
  • 7:55 - 7:58
    Otherwise the default transition will be used
  • 7:58 - 8:00
    and "Leap Take Off" will be played.
  • 8:02 - 8:04
    The benefit of using entry and exit nodes
  • 8:04 - 8:06
    to encapsulate state machines like this
  • 8:06 - 8:07
    is reusability.
  • 8:07 - 8:09
    If a particular animator controller has a
  • 8:09 - 8:11
    state machine that could be used elsewhere
  • 8:12 - 8:14
    then the state machine can simply be copied
  • 8:14 - 8:16
    to the other animator controller.
  • 8:16 - 8:19
    Because the state machine is self-contained,
  • 8:19 - 8:22
    i.e. it doesn't have any dependencies outside of itself
  • 8:22 - 8:24
    it doesn't matter what other states or
  • 8:24 - 8:26
    state machines are in the other animators
  • 8:26 - 8:27
    that you copy to.
  • 8:27 - 8:29
    For more information on animators and
  • 8:29 - 8:32
    animator controllers please see the information linked below.
Title:
Animator Sub-state machine hierarchies - Unity Official Tutorials
Description:

more » « less
Duration:
08:33

English subtitles

Revisions