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