WEBVTT

00:04.150 --> 00:09.260
In the previous lecture, we had a look at what finite state machines were, and we set up an environment.

00:09.280 --> 00:14.410
Now we're actually going to start coding for that environment and creating some states.

00:14.800 --> 00:21.280
Alright, so back in our assets what we're going to do is create a new script, so create a C-sharp

00:21.280 --> 00:26.950
script and this script will be called Game Environment.

00:28.030 --> 00:33.670
Open that up and then inside we're going to create our singleton.

00:33.790 --> 00:45.130
So this won't inherit from mono behavior and it's going to be a public sealed class like we've made

00:45.130 --> 00:45.760
before.

00:45.760 --> 00:51.520
And then we want to create our lists to hold those check points.

00:51.520 --> 01:04.030
So first of all, let's create a private static game environment instance for our singleton and then

01:04.030 --> 01:05.530
a private.

01:06.760 --> 01:09.880
List of game objects.

01:12.080 --> 01:19.070
Check points equals new list of game objects.

01:22.070 --> 01:29.540
And then a public call for that public list game object.

01:31.650 --> 01:33.540
Checkpoints.

01:36.240 --> 01:37.140
Get.

01:38.560 --> 01:41.500
Return checkpoints.

01:46.340 --> 01:46.760
Okay.

01:46.760 --> 01:50.540
We don't need a start and we don't need an update, so we'll get rid of those.

01:50.540 --> 01:52.850
And then all we need in here is our singleton.

01:52.850 --> 01:59.540
So we'll put in a public static game environment.

02:00.270 --> 02:03.120
Single Chin.

02:05.850 --> 02:06.870
Get.

02:13.400 --> 02:19.190
If instance equals null, then we will create it.

02:21.180 --> 02:26.400
Instance equals new game environment.

02:29.620 --> 02:35.080
And then we will populate those checkpoints for the actual instance itself.

02:35.080 --> 02:38.950
So instance checkpoints with the capital.

02:40.520 --> 02:45.920
Dot add range and that range will be game.

02:45.920 --> 02:47.060
Object.

02:47.480 --> 02:48.620
Find.

02:50.280 --> 02:55.920
Objects with tag the plural one and we want checkpoints.

02:55.920 --> 03:02.580
The tag that has the capital C just checkpoint, not checkpoints.

03:03.210 --> 03:05.070
And finish that off.

03:05.700 --> 03:06.390
Okay.

03:06.420 --> 03:15.180
And then we will return the instance and then make sure we have enough closing brackets for everything.

03:15.780 --> 03:17.820
Three of them at the bottom there.

03:17.970 --> 03:18.960
That's your game.

03:18.960 --> 03:24.300
Singleton, that we can now use to grab those checkpoints.

03:24.300 --> 03:30.780
And when we start creating the states, as you'll see in a moment, and the state classes are so nice

03:30.780 --> 03:33.340
to have a singleton that you can call upon.

03:33.360 --> 03:36.930
You don't have to pass it around between everything's okay.

03:36.960 --> 03:40.800
Time to create our base state class.

03:40.800 --> 03:45.630
So in the assets right click create C-sharp script.

03:45.630 --> 03:48.990
Let's call it state, which makes sense.

03:48.990 --> 03:52.170
Let's open that up in our editor.

03:52.170 --> 03:59.760
And it is again not going to be a mono behavior now it's going to have these libraries we've already

03:59.760 --> 04:03.240
got at the top, but we're dealing with the nav mesh.

04:03.240 --> 04:10.170
So we're going to need to include the unity engine dot AI system.

04:10.680 --> 04:11.070
All right.

04:11.070 --> 04:12.510
So we've got our class now.

04:12.510 --> 04:16.650
Let's get rid of the methods already existing in there.

04:16.650 --> 04:21.720
So the first thing, let's create a public enum for the state.

04:22.800 --> 04:28.830
And in here you want to list all of the possible states and you can add more later on.

04:28.830 --> 04:35.550
We are just going to put a few obvious ones in for a NPC guard so they could be in an idle state, a

04:35.550 --> 04:39.090
patrol state, they could be in pursuit.

04:39.090 --> 04:45.510
So pursue state, they could be attacking or they could even be asleep.

04:45.900 --> 04:51.150
Next, we'll have another public enum, which will be our event.

04:51.150 --> 04:59.100
So this will tell us whether we are in enter, update or exit.

04:59.550 --> 05:10.500
So where we are in the running of the particular state now in this state, we're going to bring in a

05:10.500 --> 05:19.800
whole bunch of values that are specific to our actual game and the way we've got it set up.

05:19.800 --> 05:26.760
But first of all, we need a public state and let's call that name.

05:26.760 --> 05:29.400
So this will record what the name of the state is.

05:30.060 --> 05:39.540
Then we want our protected event for the stage, which is one of these enter update or exit.

05:40.020 --> 05:43.260
And then we're going to have.

05:44.960 --> 05:53.600
Protected game object NPC a protected.

05:55.690 --> 06:01.330
Animator Adam because we're dealing with animations as well.

06:01.660 --> 06:05.410
Protected transform for the player.

06:05.800 --> 06:07.630
We've got a god that we're creating.

06:07.630 --> 06:14.320
So the guard needs to know where the player is so that it can face the player and know whether it should

06:14.320 --> 06:17.290
be shooting or chasing the player, depending on the distance.

06:17.500 --> 06:20.350
And then also a protected.

06:22.630 --> 06:23.830
State.

06:27.480 --> 06:30.120
Which will be our next.

06:30.950 --> 06:33.800
A date like that.

06:33.800 --> 06:37.850
This state is not the enum, which is this one up here.

06:37.850 --> 06:46.280
So the name is based on the enum and this next state is the state that gets to run after the one we're

06:46.280 --> 06:47.000
currently running.

06:47.000 --> 06:53.570
So if you're in idle and then you want to go into patrol, then the next state would be pointing to

06:53.570 --> 06:55.460
a patrol state.

06:55.940 --> 06:57.410
All right, great.

06:57.440 --> 06:57.770
Okay.

06:57.770 --> 07:06.110
Some other values we need for the actual I let's have a float this distance and we will set that to

07:06.110 --> 07:22.940
10.0 if float viz angle equals let's say 30 degrees and a float called shoot distance or dist, which

07:22.940 --> 07:26.270
I will set to 7.0 f.

07:26.270 --> 07:34.610
Now what these mean is that when the player is within a distance of ten from the NPC, then the NPC

07:34.640 --> 07:40.250
should be able to see it if it's facing within 30 degrees of its line of sight.

07:41.180 --> 07:48.410
If the NPC gets close enough within a range of seven, it can then actually go into an attacking state.

07:48.410 --> 07:51.340
We'll get to that logic in the next lecture.

07:51.710 --> 07:58.040
Let's continue by writing the method or the constructor, I should say, for this particular state.

07:58.040 --> 08:12.680
So public state, it's going to take a game object which will be the NPC, a nev mesh agent, agent

08:13.280 --> 08:14.810
and animator.

08:16.970 --> 08:22.550
And and a transform for our player.

08:23.360 --> 08:25.430
Then what we do with all of those.

08:25.640 --> 08:27.650
So we actually sit them in here.

08:27.650 --> 08:36.020
So NBC equals and Pixie Agent will equal the agent we pass through.

08:37.310 --> 08:40.790
That agent shouldn't have an underscore in front of it.

08:40.790 --> 08:46.760
Then we're going to have an atom which equals underscore score annum.

08:47.820 --> 08:51.810
Stage which will equal event.

08:52.880 --> 08:54.230
Dot enter.

08:56.510 --> 09:01.100
And then player equals on the score player.

09:01.130 --> 09:08.750
Now I've just noticed that I haven't declared agent let me finish this constructor and we'll just go

09:08.750 --> 09:09.940
back at the top and add that in.

09:09.950 --> 09:17.330
So a protected nav mesh agent called agent.

09:20.980 --> 09:21.550
All right.

09:21.550 --> 09:31.660
Now under there, we're going to put in the skeleton code for each of our different stages or phases

09:31.660 --> 09:33.760
as we go through the state.

09:34.030 --> 09:38.290
So these will be a public virtual void.

09:38.320 --> 09:39.250
Enter.

09:41.000 --> 09:47.880
Stage equals event dot update.

09:47.900 --> 09:49.070
Now the reason.

09:49.190 --> 09:50.240
This is the.

09:50.270 --> 09:50.750
Enter.

09:50.750 --> 09:51.200
So this.

09:51.200 --> 09:52.850
This should run first.

09:52.850 --> 09:59.090
When you come into a state and when it does it then sets the stage to be whatever's going to be next

09:59.090 --> 10:03.140
so that it will know later on in the process that that's where it's going.

10:03.470 --> 10:07.400
Public virtual void for update.

10:09.160 --> 10:17.650
And then this stage will equal event update because once you're in update, you want to stay in update

10:17.650 --> 10:30.280
until update actually throws you out and then public virtual void exit that will then set the stage

10:30.850 --> 10:39.310
equals to event dot exit so it knows what to run and clean up after itself there.

10:39.640 --> 10:52.030
Then finally, we need a public state process, which is the method that will get run from outside and

10:52.030 --> 10:56.830
progress the state through each of those different stages.

10:56.830 --> 11:07.330
So if stage equals event do not enter, then we will run the enter method.

11:09.510 --> 11:23.190
If stage equals event dot update in capitals because that is our enum value, then we're going to run

11:23.190 --> 11:39.330
update and then if stage equals event dot exit, we're actually going to call the exit method and we're

11:39.330 --> 11:44.310
going to return the next state.

11:45.650 --> 11:53.270
Notice that this particular method returns a state.

11:54.470 --> 11:58.400
So now if we're not returning the next state, we'll just do a return.

11:58.610 --> 12:02.390
This so that we keep returning the same state.

12:02.390 --> 12:07.340
And you'll see where this helps progress to the next state and the next state in line.

12:07.340 --> 12:16.040
As we go through now, we will create an idle state and a patrol state so we can actually get our NPC

12:16.220 --> 12:19.160
doing something so that you can see this working.

12:19.190 --> 12:30.800
Now I'm going to create my states that inherit from the base state class in this dot csv file.

12:30.890 --> 12:37.700
Better practice would probably be to have them in separate CSS files, but because we're sort of constructing

12:37.700 --> 12:42.470
this as we go and going back and forth between code and what not, I'm just going to have the whole

12:42.470 --> 12:48.050
lot in the same place just to make it a little bit easier for us so that we can see it all and we'll

12:48.050 --> 12:50.840
come back and keep putting that together in the next video.
