0:00:00.163,0:00:02.164 When creating complex state-based logic 0:00:02.164,0:00:04.623 systems in an animator controller 0:00:04.623,0:00:06.623 it can greatly help organisation to use 0:00:06.623,0:00:08.623 a hierarchy of state machines. 0:00:08.883,0:00:10.883 To create a state machine right click 0:00:10.883,0:00:13.125 on the background on the animator window. 0:00:13.125,0:00:15.656 and select Create Sub-State Machine 0:00:21.146,0:00:23.213 This will create a state machine within the current 0:00:23.213,0:00:25.213 layer or state machine level. 0:00:25.665,0:00:28.326 Note the hexagonal shape of sub-state machines 0:00:28.326,0:00:31.019 versus the standard rounded rectangular shape 0:00:31.019,0:00:32.607 of individual states. 0:00:32.607,0:00:35.271 Let's look at an example of when they are useful. 0:00:36.021,0:00:38.021 Here we have an animator controller 0:00:38.021,0:00:39.741 that's starting to look cluttered 0:00:39.741,0:00:42.268 so before many more states are added to it 0:00:42.268,0:00:43.931 it should be tidied up. 0:00:43.931,0:00:45.931 To do this some of the states 0:00:45.931,0:00:47.931 can be placed in to a state machine. 0:00:47.931,0:00:49.931 In this case we could group all of the 0:00:49.931,0:00:52.250 states that are to do with being Airborne 0:00:52.250,0:00:54.144 in to one sub-state machine. 0:00:54.144,0:00:56.442 To do this, first we make the state machine 0:00:56.442,0:00:58.442 by right clicking on the background of the 0:00:58.442,0:01:01.990 animator window and selecting Create Sub-State Machine. 0:01:04.605,0:01:06.605 We can rename the state machine by clicking on 0:01:06.605,0:01:08.605 it and changing it's name in the inspector, 0:01:08.605,0:01:10.317 just like single states. 0:01:11.403,0:01:14.191 Next we need to put some states in to this new state machine. 0:01:14.778,0:01:16.278 We do this by selecting all the 0:01:16.278,0:01:18.278 states that we want to move and dragging them 0:01:18.278,0:01:20.075 on to the state machine. 0:01:20.075,0:01:22.858 For this example we want to move Leap Takeoff, 0:01:22.858,0:01:26.469 Leap Airborne, Falling and Landing states. 0:01:27.267,0:01:29.267 Note the + icon that appears next to 0:01:29.267,0:01:31.267 your cursor as you are dragging. 0:01:35.430,0:01:37.866 When the states have been added to the state machine 0:01:37.866,0:01:40.745 they will disappear from the current state machine or layer. 0:01:41.226,0:01:43.226 To see them again in order to edit 0:01:43.226,0:01:45.226 any of their properties for example 0:01:45.495,0:01:48.041 simply double click on the sub-state machine. 0:01:51.079,0:01:52.945 To go back to the containing layer 0:01:52.945,0:01:55.989 or state machine double click on the Up node. 0:01:56.345,0:01:58.345 Note that the breadcrumb at the top of the window 0:01:58.345,0:02:00.345 shows you where abouts you are 0:02:00.345,0:02:01.855 within the state machines. 0:02:09.028,0:02:12.232 State machines can be used in a hierarchical structure. 0:02:12.232,0:02:14.415 This means that within each layer of an animator 0:02:14.415,0:02:17.310 controller there could be several state machines. 0:02:17.916,0:02:19.916 Each of those could, in turn, have 0:02:19.916,0:02:22.314 sub-state machines within them, and so on. 0:02:25.570,0:02:28.150 Transitions can go from states outside 0:02:28.150,0:02:30.150 to those inside a state machine. 0:02:30.477,0:02:32.477 We can see from this example that when 0:02:32.477,0:02:35.048 our states were added to the sub-state machine 0:02:35.048,0:02:38.568 their transitions were retained. 0:02:43.740,0:02:46.192 To make a new transition to a state machine 0:02:46.192,0:02:48.192 right click on the state or state machine 0:02:48.192,0:02:49.903 from which you are transitioning 0:02:49.903,0:02:51.903 and select Make Transition. 0:02:52.470,0:02:54.470 Then left click on the state machine you wish 0:02:54.470,0:02:56.097 to transition to. 0:02:57.008,0:02:59.219 From here a pop-up menu will appear 0:02:59.219,0:03:01.872 offering you the choice to transition to any state, 0:03:01.872,0:03:05.016 or state machine within the selected state machine. 0:03:05.449,0:03:07.449 This includes the state machine itself. 0:03:10.054,0:03:12.054 One way to transition out of a state machine 0:03:12.054,0:03:14.188 is to use the Up node. 0:03:14.188,0:03:16.188 This works like a link to all states 0:03:16.188,0:03:18.188 and state machines that are not contained 0:03:18.188,0:03:19.986 in the current state machine. 0:03:20.371,0:03:22.246 The up node is labelled Up 0:03:22.246,0:03:24.476 followed by the name of the parent state machine. 0:03:25.091,0:03:27.091 If there is not parent state machine 0:03:27.091,0:03:29.091 it will show the name of the layer it is in. 0:03:29.697,0:03:31.697 In this example there was no parent 0:03:31.697,0:03:34.063 state machine so the up node is labelled 0:03:34.063,0:03:35.717 (Up) Base Layer. 0:03:35.717,0:03:38.176 To make a transition out of a state machine 0:03:38.176,0:03:40.176 right click on the state or state machine 0:03:40.176,0:03:41.928 that you wish to transition from 0:03:42.312,0:03:44.513 and select Make Transition 0:03:44.513,0:03:46.513 then left click on the up node. 0:03:47.789,0:03:49.789 You are then offered the same choices as 0:03:49.789,0:03:52.515 when transitioning in to a state machine. 0:03:52.515,0:03:54.515 Only this time it is a choice of the states 0:03:54.515,0:03:56.361 and state machines above the 0:03:56.361,0:03:57.829 current one you were viewing. 0:04:00.059,0:04:02.059 In every layer and state machine 0:04:02.059,0:04:05.122 there are green entry and red exit nodes. 0:04:05.371,0:04:07.650 These are used to encapsulate state machines 0:04:07.650,0:04:09.650 so that they have less dependence on 0:04:09.650,0:04:11.650 states or state machines. 0:04:11.650,0:04:14.436 They represent an alternative to using the up node. 0:04:14.791,0:04:16.791 Transitions to and from state machines 0:04:16.791,0:04:18.791 can either use the up node or 0:04:18.791,0:04:20.427 the entry and exit nodes. 0:04:20.945,0:04:22.347 Using the up node, 0:04:22.347,0:04:24.347 transitions happen directly from 0:04:24.347,0:04:27.375 state to state or from state to state machine. 0:04:27.740,0:04:30.039 When using entry and exit nodes 0:04:30.039,0:04:32.039 the transitions are to and from 0:04:32.039,0:04:34.039 the state machine itself. 0:04:34.039,0:04:36.039 A transition to a state machine 0:04:36.039,0:04:38.039 will come in at the entry node 0:04:38.960,0:04:40.960 and leave via the exit node. 0:04:48.436,0:04:50.050 This may mean designing your transitions 0:04:50.050,0:04:51.847 in a very different way. 0:04:51.847,0:04:54.652 Instead of having a single transition through the up node 0:04:54.652,0:04:56.048 there maybe be many. 0:04:56.048,0:04:58.048 Let's take a look at an example. 0:04:58.048,0:05:01.279 This is the same controller we saw in our earlier example. 0:05:03.244,0:05:05.693 But the Locomotion and Airborne states 0:05:05.693,0:05:08.049 have been put in to their own state machines. 0:05:09.190,0:05:11.190 However instead of using transitions 0:05:11.190,0:05:13.190 between each of the states as before 0:05:13.190,0:05:15.506 entry and exit nodes are used. 0:05:15.506,0:05:17.506 In the previous example there were transitions 0:05:17.506,0:05:21.242 from Standing Locomotion to Leap Take Off and Falling. 0:05:23.641,0:05:25.641 And a transition from Landing 0:05:25.641,0:05:27.641 to Standing Locomotion. 0:05:29.455,0:05:31.455 The condition to go from Standing Locomotion 0:05:31.455,0:05:34.397 to Leap Take Off is the Jump trigger. 0:05:35.952,0:05:37.952 However, once we have encapsulated a 0:05:37.952,0:05:40.355 locomotion state in to a state machine 0:05:40.355,0:05:42.355 it no longer matters what state we are 0:05:42.355,0:05:43.758 transitioning to. 0:05:43.758,0:05:45.758 Instead it just matters that we no longer 0:05:45.758,0:05:47.758 want to be in this state machine. 0:05:47.998,0:05:49.998 So we transition to the exit node 0:05:49.998,0:05:51.998 with the jump trigger as a condition. 0:05:57.112,0:05:59.361 Back in the complex animator controller 0:05:59.361,0:06:01.361 the condition to go from standing locomotion 0:06:01.361,0:06:03.361 to Falling is that the parameter 0:06:03.361,0:06:05.361 "Grounded" is false 0:06:06.781,0:06:08.781 In the entry and exit node example 0:06:08.781,0:06:10.781 the same condition is required for a second 0:06:10.781,0:06:13.820 transition to exit the locomotion state machine. 0:06:21.074,0:06:23.431 Since we only have two state machines 0:06:23.431,0:06:26.222 we can assume that if we are leaving one state machine 0:06:26.222,0:06:27.769 we want to go in to the other 0:06:28.491,0:06:32.204 As such there are no conditions for transitions between them. 0:06:33.528,0:06:35.789 We have two transitions from the entry node 0:06:35.789,0:06:37.789 of the Airborne state machine. 0:06:38.480,0:06:40.480 The first is the default transition 0:06:40.480,0:06:42.067 from the entry node. 0:06:42.067,0:06:44.616 We can see this because it is orange in colour, 0:06:44.616,0:06:46.616 instead of the normal white. 0:06:47.374,0:06:49.759 This transition will be used when no other transition 0:06:49.759,0:06:51.759 from the entry node can be used 0:06:51.759,0:06:54.113 or if it's the only available transition. 0:06:54.594,0:06:56.392 The other transition is greyed out. 0:06:56.757,0:06:58.757 When a transition is grey like this 0:06:58.757,0:07:00.757 it means that it has no knowledge of the state 0:07:00.757,0:07:02.332 it is coming from. 0:07:02.332,0:07:03.910 This particular transition 0:07:03.910,0:07:05.910 is coming in to the state machine from 0:07:05.910,0:07:08.245 the entry node, so it doesn't know which 0:07:08.245,0:07:11.640 state it is coming from, as it could be from any source. 0:07:11.915,0:07:14.541 We can see that the transitions between the locomotion 0:07:14.541,0:07:17.518 and Airborne state machines were also greyed out. 0:07:19.504,0:07:21.504 Again this is because they don't know 0:07:21.504,0:07:23.504 which state they are transitioning from. 0:07:24.617,0:07:26.617 When a transition is greyed out, the only 0:07:26.617,0:07:28.617 editable aspect are it's conditions. 0:07:29.164,0:07:31.164 Since the transition doesn't know which state 0:07:31.164,0:07:33.164 it is coming from any other details 0:07:33.164,0:07:35.164 about the transition would be meaningless. 0:07:35.164,0:07:38.047 To control the duration and other aspects 0:07:38.047,0:07:40.956 use the transition from the originating state. 0:07:43.374,0:07:45.374 The transition to Falling has the condition 0:07:45.374,0:07:47.374 that "Grounded" is false. 0:07:47.691,0:07:49.546 These two transitions combined 0:07:49.546,0:07:51.546 mean that when we enter this state machine 0:07:51.546,0:07:54.259 if grounded is false Falling will be played. 0:07:55.343,0:07:58.353 Otherwise the default transition will be used 0:07:58.360,0:08:00.360 and "Leap Take Off" will be played. 0:08:01.610,0:08:03.610 The benefit of using entry and exit nodes 0:08:03.610,0:08:05.610 to encapsulate state machines like this 0:08:05.610,0:08:07.466 is reusability. 0:08:07.466,0:08:09.466 If a particular animator controller has a 0:08:09.466,0:08:11.466 state machine that could be used elsewhere 0:08:11.831,0:08:14.158 then the state machine can simply be copied 0:08:14.158,0:08:16.158 to the other animator controller. 0:08:16.158,0:08:18.544 Because the state machine is self-contained, 0:08:18.544,0:08:21.803 i.e. it doesn't have any dependencies outside of itself 0:08:21.803,0:08:23.803 it doesn't matter what other states or 0:08:23.803,0:08:25.803 state machines are in the other animators 0:08:25.803,0:08:27.189 that you copy to. 0:08:27.189,0:08:29.189 For more information on animators and 0:08:29.189,0:08:32.168 animator controllers please see the information linked below.