WEBVTT

00:01.470 --> 00:06.480
If you've worked with character animation or non-player character behaviors in a game, chances are

00:06.480 --> 00:12.780
you have implemented states at some time, or other states allow game objects to take on different behaviors

00:12.780 --> 00:17.910
when required, making them look like they are controlled by a completely different class.

00:18.000 --> 00:21.450
Unity makes use of states in the mechanism system.

00:21.480 --> 00:27.660
In the animator, you can create a simple graph of states from individual animations and define how

00:27.660 --> 00:30.000
to get from one to the other state.

00:30.000 --> 00:34.760
Finite state machines used in games are the embodiment of this design pattern.

00:34.770 --> 00:40.710
A finite state machine is a conceptual machine that can be in exactly one of any given number of finite

00:40.710 --> 00:42.270
states at any time.

00:42.510 --> 00:47.910
A finite state machine is used in the field of artificial intelligence to define, among other things,

00:47.910 --> 00:50.820
the behaviors of computer controlled characters.

00:51.060 --> 00:56.880
It is represented as a graph with nodes that represent the states and transitions that define the path

00:56.880 --> 00:58.560
from one node to the next.

00:59.490 --> 01:05.460
Once a state has been entered, the NPC stays in that state until a condition on the transition is met.

01:05.490 --> 01:09.240
Here we have a finite state machine for simple guarding behavior.

01:09.480 --> 01:15.120
If the NPC is in the patrol state, it will continue performing the associated behaviors until a condition

01:15.120 --> 01:18.360
is met on any of the transitions exiting the state.

01:18.390 --> 01:23.610
In this case, it is waiting until the player is a distance less than 20 away.

01:23.760 --> 01:28.860
Once that condition is met, the NPC transitions into the Chase State.

01:29.340 --> 01:34.560
Any state in a state machine has three processes that it runs at different times.

01:34.560 --> 01:38.650
It has an enter, it has an update and it has an exit.

01:38.670 --> 01:46.570
Enter runs as soon as the state is transitioned to the state, then enters the endless loop of the update.

01:46.590 --> 01:51.730
Update continues to run until the transition out of the state is triggered.

01:51.750 --> 01:55.710
At this point, before leaving the state, the exit method is run.

01:56.010 --> 02:02.550
For example, an NPC that is transitioning to the patrol state on entering will need to set the appropriate

02:02.550 --> 02:06.780
animation as well as determine where the waypoints for the patrol are.

02:06.810 --> 02:13.080
During its update, the NPC will simply walk from one waypoint to another, constantly checking when

02:13.080 --> 02:16.830
a waypoint has been reached and then looking for the next one.

02:17.220 --> 02:20.940
Something in the update must cause the exit to fire.

02:20.970 --> 02:25.400
This could be something as simple as the NPC deciding to have a sleep.

02:25.410 --> 02:32.010
The walk animation is stopped and the NPCs transition into the next state that takes care of the new

02:32.010 --> 02:34.410
behavior of sleep will begin.

02:34.440 --> 02:38.210
A state machine can be as easy to implement as an if statement.

02:38.220 --> 02:44.840
For example, let's say you have an NPC that you want to be in either of two states, idle or walking.

02:44.850 --> 02:49.650
This would be easy to handle in an update loop, and you've probably done this yourself before.

02:49.950 --> 02:56.070
In fact, the code that you see here is way too verbose for what you actually want to do in such a simple

02:56.070 --> 02:56.670
task.

02:56.670 --> 03:00.630
But it is pulled apart to illustrate how states work.

03:00.660 --> 03:06.460
So first of all, you have a state flag that records the state that the NPC is in.

03:06.480 --> 03:12.960
Now, in this case, I've used a string, but you could easily have an integer or even use an enum which

03:12.960 --> 03:14.790
would make it more meaningful.

03:14.910 --> 03:22.410
Now you could use a key press to indicate whether you want to walk or whether you want to be idle,

03:22.410 --> 03:25.200
which would then set that state flag.

03:25.320 --> 03:27.280
Later in the update loop.

03:27.300 --> 03:29.940
We test for the state.

03:30.090 --> 03:36.720
So as the state is changing, if it wants your NBC to be walking, then it's going to play a walking

03:36.720 --> 03:41.760
animation and actually translate the character in a forward direction.

03:41.970 --> 03:49.590
Whereas if it's idle the state, then you want to play the idle animation and you don't want to move

03:49.590 --> 03:50.910
the character.

03:50.970 --> 03:58.020
If, however code outside this class is able to set the state to dead, the key presses inside the update

03:58.020 --> 04:03.420
would overwrite this and you would have to deal with it by a little bit of refactoring which might look

04:03.420 --> 04:04.350
like this.

04:04.380 --> 04:09.840
Note how we are testing for the state being dead inside the update before doing anything else.

04:09.990 --> 04:16.080
Okay, so this isn't too messy, but once you start adding lots of other character states and have them

04:16.080 --> 04:22.410
mixed together, riding a state machine in this format gets way too over complicated.

04:22.920 --> 04:31.380
Therefore, the state pattern comes into play where a state is defined as a contained piece of code

04:31.380 --> 04:39.990
that deals with all of the behaviors and only the behaviors that belong to that particular state.

04:40.020 --> 04:46.080
Instead, we are going to write our own state based class that all other states will inherit from a

04:46.080 --> 04:51.870
state records the type of state it is, which will entirely depend on the type of state machine you

04:51.870 --> 04:52.860
are writing.

04:52.890 --> 05:00.570
In this case, it will be used to build a state machine for an NPC guard who will patrol an area and

05:00.570 --> 05:03.300
pursue an attack approaching players.

05:03.570 --> 05:06.140
Remember the three phases of a state?

05:06.150 --> 05:09.960
Well, they are enter, update and exit.

05:10.080 --> 05:13.110
The phase will dictate which method to run.

05:13.290 --> 05:21.390
You can see that there are methods for each of the stages and the base methods update the stages accordingly.

05:21.750 --> 05:25.080
Last but not least, there is a process method.

05:25.260 --> 05:33.000
This is called from outside of any state and it determines which method inside the state should run

05:33.000 --> 05:36.870
at any time based on the set stage.

05:37.380 --> 05:44.100
Through the use of this base class as a template for all states, we can be sure to keep the code related

05:44.100 --> 05:48.840
to each state compartmentalized from other NPC behaviors.

05:48.870 --> 05:52.530
It really is a quite clever design pattern.

05:52.560 --> 05:53.910
Okay, enough of the theory.

05:53.910 --> 05:59.010
Let's start building this thing once you get some code together and start creating more.

05:59.050 --> 06:02.040
More states inheriting from that base class.

06:02.050 --> 06:10.360
You'll see just how powerful this design method is and how it will make your life creating AI behaviors

06:10.360 --> 06:17.110
in your characters so much simpler than having to juggle which state they're in and which animation

06:17.110 --> 06:21.250
is playing and which animation is not playing, which cares press and all this sort of stuff, because

06:21.250 --> 06:24.400
they can get really, really messy very, very quickly.

06:24.520 --> 06:25.060
Right?

06:25.060 --> 06:28.090
So open up a new unity project.

06:28.090 --> 06:35.530
Now I want you to bring in the starter file that is attached to this lecture as a resource.

06:36.310 --> 06:41.890
Just be aware that when you first bring in that unity package, unity will want to restart itself,

06:41.890 --> 06:47.710
and that's because the controller has the new input system on it.

06:47.710 --> 06:52.090
So just let it restart itself so that it brings in the correct packages.

06:52.270 --> 06:58.510
And then once you've done that, you might get a few errors that come up in the console.

06:58.510 --> 07:03.850
You might not, but if you do, just like ignore them, okay?

07:03.850 --> 07:10.450
Or just clear them, they won't cause you any future issues because most of the standard asset stuff

07:10.450 --> 07:16.150
that's in here from the old standard assets, that does cause a few issues because the code has been

07:16.150 --> 07:16.690
deprecated.

07:16.690 --> 07:21.670
I have actually taken that out, but I can't promise I didn't miss anything though.

07:21.670 --> 07:26.200
I've just brought this in myself and I'm not getting any errors, so just be aware of that.

07:26.200 --> 07:28.900
As long as they're not read errors, you're pretty good to go.

07:29.920 --> 07:36.610
Now I've got the navigation tab up here as well, and if you've opened up my project, I believe you

07:36.610 --> 07:37.480
will get that as well.

07:37.480 --> 07:46.420
If you don't just go to window and I navigation to open up navigation, go to the back tab and then

07:46.420 --> 07:47.590
just click on Bake.

07:47.590 --> 07:54.760
Now that will re bake the surface of the nav mesh showing you the walkable areas in blue.

07:54.760 --> 08:01.870
And you can see here that the structure is excluded and the area around the trees as well.

08:01.930 --> 08:04.600
Now, ug, who is our God?

08:04.600 --> 08:06.760
Let's get in a little closer to him.

08:06.790 --> 08:08.410
Zoom in here.

08:08.830 --> 08:09.940
There he is.

08:10.240 --> 08:13.420
Okay, so he is guarding our structure.

08:13.570 --> 08:23.170
He's going to go on patrol and he has a nav mesh agent component attached to him, which will do all

08:23.170 --> 08:23.920
the work for us.

08:23.920 --> 08:25.540
For him moving around.

08:25.750 --> 08:34.420
He has an audio source for the gun shooting and his animator is all set up for you if we have a quick

08:34.420 --> 08:36.430
look at that animator.

08:36.430 --> 08:41.800
So let's just double click on the UG animator and just open this up.

08:41.800 --> 08:43.750
Press F to center it.

08:43.750 --> 08:48.280
You'll see that there's a idol so there's a rifle idol.

08:48.490 --> 08:57.160
There's also up here, a rifle walk, rifle run and a rifle firing.

08:57.160 --> 09:00.760
And there's a few other things down here that will get to a little later on.

09:01.870 --> 09:08.140
This in itself is a state machine because it's animation states, and these are the transitions that

09:08.140 --> 09:10.120
trigger between them.

09:10.210 --> 09:11.590
But we're not writing this.

09:11.590 --> 09:15.730
So this is just the animation one, but you'll see if you click on any of the transitions.

09:15.730 --> 09:25.930
So for going from Rifle Idol to rifle walk, then you have to trigger the is walking condition and we

09:25.930 --> 09:30.160
will do that all in the code so you don't have to set that up.

09:30.160 --> 09:35.560
It's there ready to go for you, but there's no code currently on UG.

09:36.310 --> 09:46.060
The first thing that we're going to do is we're going to write a singleton class for the game environment

09:46.060 --> 09:55.090
because like our previous projects that we've created, there are some checkpoints set out on the surface

09:55.090 --> 10:00.190
and you'll see them in the scene if we go up here.

10:00.850 --> 10:08.110
And if I just click on all of them, there's five of them in total and there they are.

10:08.110 --> 10:13.630
So they give us a circuit to go around.

10:13.990 --> 10:21.130
Each checkpoint has a tag of checkpoint with a capital C.

10:22.080 --> 10:22.620
Okay.

10:22.620 --> 10:29.670
Well, that's pretty much the whole set up of this environment, which has taken up a whole video to

10:29.670 --> 10:30.510
get used to.

10:30.540 --> 10:36.480
So investigate this a little bit more if you want to see how it's all set up with the animator and all

10:36.480 --> 10:37.400
those sorts of things.

10:37.410 --> 10:43.890
When we come back in the next video, we'll actually start programming our finite state machine.
