WEBVTT

00:06.600 --> 00:10.620
In the previous lecture, we set up our base state class.

00:10.630 --> 00:16.770
We're now going to inherit from that and create an idle and a patrolling behavior for our character,

00:17.040 --> 00:17.340
right?

00:17.340 --> 00:26.400
So right underneath that base class state, we're going to create a public class called Idol and it

00:26.400 --> 00:29.910
is going to inherit from state.

00:31.730 --> 00:32.630
Like that.

00:33.010 --> 00:33.210
Okay.

00:33.230 --> 00:36.110
So the first thing it's going to need is its constructor.

00:36.110 --> 00:43.970
So we want public Idol and then it's going to have all of the stuff that our state has.

00:43.970 --> 00:52.520
So let's just copy all of that because there's no point us typing it all out again and just put that

00:52.520 --> 00:55.100
into there.

00:55.370 --> 01:08.510
And then we also after that, let's just go and put in the base and run through the NPC, the agent.

01:09.960 --> 01:11.250
The annum.

01:12.270 --> 01:14.070
And the player.

01:15.040 --> 01:21.340
Details just to pass them through since the base is actually going to store that for us anyway.

01:21.340 --> 01:28.540
And then all we really need to do inside of this constructor is to give our state a name.

01:28.540 --> 01:33.490
So name equals state dot.

01:34.670 --> 01:38.710
Idle so it knows what it is.

01:38.800 --> 01:39.020
Right.

01:39.020 --> 01:45.290
The next thing we're going to put in here, it's just scroll down a little bit is now our enter.

01:45.290 --> 01:56.030
So public override, void, enter and then inside of the enter before want to put the base enter in

01:56.030 --> 02:02.990
there automatically for you if you're using Visual Studio, which is okay because you want to run this

02:02.990 --> 02:03.320
anyway.

02:03.320 --> 02:09.560
And if we have a look back up here what our base enter is doing, it's setting the stage to update.

02:09.560 --> 02:13.460
So that means that we don't actually have to do it down in here.

02:13.820 --> 02:18.560
But what we need to do when we enter the Idol state is trigger the idol animation.

02:18.560 --> 02:22.430
So that's going to be anim dot set trigger.

02:24.630 --> 02:25.510
Is Otto.

02:25.540 --> 02:31.020
And if you have a look in the animator, you'll see that there is a trigger in there called is Otto,

02:31.020 --> 02:36.900
which takes you from any of the other states that aren't idle back to idle.

02:36.970 --> 02:37.320
Right.

02:37.320 --> 02:38.680
So that's our enter.

02:38.700 --> 02:44.190
Now, of course, we need a public override void for update.

02:44.940 --> 02:52.740
And inside of update again, we're going to run the base update as the last thing.

02:53.160 --> 03:03.210
But we also want to do in here is transition to our next state.

03:03.210 --> 03:10.440
So remember I said earlier on in the last video, is that somewhere inside the update, it has to break

03:10.440 --> 03:12.280
itself out of there.

03:12.300 --> 03:17.430
So this is the only place that's going to do it because update is essentially like a little mini game

03:17.430 --> 03:24.150
loop that's going to run over and over again for your state and look after the processing of it.

03:24.150 --> 03:29.010
And in order to get from update to exit, you've got to have some kind of condition in here.

03:29.010 --> 03:32.490
So we're going to do a random value.

03:32.490 --> 03:48.330
So if random dot range 0 to 100 is let's say less than ten, so 10% of the time what we're going to

03:48.330 --> 03:51.330
do is go to the next stage.

03:51.330 --> 04:04.260
So just inside some brackets here, we will put next state equals new patrol and that patrol is going

04:04.260 --> 04:09.540
to need the NPC agent, Adam and player.

04:10.020 --> 04:13.080
We haven't written patrol yet, but it's going to be the new state.

04:13.080 --> 04:23.040
And then in here we actually go stage equals event exit and what that's going to do next time the process

04:23.040 --> 04:31.980
which is up here comes around is it's going to throw you into the exit stage instead, which will then

04:31.980 --> 04:36.870
return the next state that we just set up down here.

04:37.140 --> 04:37.590
Okay.

04:37.620 --> 04:44.850
And then finally we have to put in our public override void and that will be for exit.

04:45.960 --> 04:56.760
And in here we have to keep our base and we're going to put in Adam Dot reset trigger.

04:57.910 --> 04:58.990
Is.

04:59.850 --> 05:00.540
Idle.

05:00.630 --> 05:06.450
Now, this is just a little bit of a trick to do with the McAdam system if you've triggered something

05:06.450 --> 05:07.180
up here.

05:07.200 --> 05:15.780
What it does is it kind of like puts an event into the system which doesn't get used up until the animation

05:15.780 --> 05:18.060
that it's meant to trigger runs.

05:18.150 --> 05:26.580
If that animation doesn't run or doesn't need to be triggered, then that little event still sits there.

05:26.580 --> 05:32.400
And what it can do is cause a few problems later on when you go to set trigger to walking and then your

05:32.400 --> 05:38.940
character doesn't walk is because there might have been an old is idle still sitting there in the queue.

05:39.000 --> 05:44.760
So by resetting it in exit it means any that we threw in there which didn't need to be, is just going

05:44.760 --> 05:49.080
to get taken out and it just going to clean up that animation for us.

05:49.080 --> 05:58.050
And this is what I mean by containing all of the necessary behaviors just inside the one state.

05:58.050 --> 06:04.570
So this is stuff pertaining to idle and how you get out of idle, and that's it.

06:04.980 --> 06:06.570
We don't want anything else.

06:06.570 --> 06:11.250
We don't want to check any other states that relate to other things and and stuff like that.

06:11.250 --> 06:13.320
It's just about the idle.

06:13.320 --> 06:19.440
And now we're going to create the patrol because idle calls on patrol and then we can actually get something

06:19.440 --> 06:20.910
running that you can see.

06:21.210 --> 06:28.020
So underneath the idle state in the states, dot KSFO will add patrol.

06:28.170 --> 06:35.490
So it'll be a public class patrol which is going to inherit from state.

06:36.590 --> 06:40.100
The State needs to move your character around the different waypoints.

06:40.100 --> 06:46.760
So we need an index to count through those waypoints which are in our array that we've set up or at

06:46.760 --> 06:49.190
least we've set up in the game environment.

06:49.190 --> 06:55.760
So let's start with an int for current index and let's make that minus one.

06:57.070 --> 07:00.570
And let's create our constructor.

07:00.580 --> 07:02.500
So public patrol.

07:03.310 --> 07:08.340
Now, this is going to have all of that same stuff that we put for Idle.

07:08.350 --> 07:11.980
So let's just grab all this to save us, typing it out again.

07:11.980 --> 07:15.940
Copy it, and let's just put it in here.

07:19.160 --> 07:19.630
Okay.

07:19.640 --> 07:26.060
Then inside of this, what we're going to do is set the name to state.

07:26.780 --> 07:28.610
Dot patrol.

07:30.880 --> 07:42.070
Agent speed to to and agent dot is stopped equal to.

07:43.570 --> 07:44.450
False.

07:44.470 --> 07:51.670
Now you can stop an agent and have an agent moving around on a particular path.

07:51.700 --> 07:55.610
If you change, this is stopped value.

07:55.630 --> 08:02.170
So if the agent is currently doing something else, we actually want to stop it at this point because

08:02.170 --> 08:07.030
we're going to give it another, I guess, destination to follow.

08:07.750 --> 08:13.990
The other thing you'll notice in here is this Agent Speed, which is another nav mesh agent property,

08:13.990 --> 08:17.380
and that's how fast your character is going to move.

08:17.410 --> 08:26.320
Now, in this case, when I've got UG walking around or, you know, doing the walk animation, then

08:26.320 --> 08:32.410
a speed of two when he moves seems to be not too bad as far as making sure that he doesn't look like

08:32.410 --> 08:34.360
he's sliding around on the ground.

08:34.360 --> 08:40.410
So that's something else that you can consider working with and coordinating.

08:40.420 --> 08:44.320
We haven't added an agent speed up into idle.

08:44.380 --> 08:51.880
Obviously in the idle state the agent is going to be stationary, but because the agent doesn't have

08:51.880 --> 08:58.630
a path that it's moving along on your nav mesh, it will be stationary by default anyway.

08:58.720 --> 09:02.980
The speed of the agent in this case only comes into play.

09:02.980 --> 09:08.020
If the agent needs to move along a path, it doesn't mean if you set it to two, the agent will just

09:08.020 --> 09:09.730
start moving at speed of two.

09:10.000 --> 09:12.100
It's only if it has a path, right?

09:12.100 --> 09:15.700
So that's all you need inside of your patrol.

09:15.820 --> 09:21.670
Okay, so let's go down and we will now put in our enter our update and our exit.

09:21.670 --> 09:28.180
So it's going to be a public override, void enter.

09:28.420 --> 09:32.110
And let's just copy this and we'll have three of them.

09:32.500 --> 09:35.860
So we want one for update.

09:35.980 --> 09:37.270
Make this update.

09:38.150 --> 09:39.200
Like this.

09:40.550 --> 09:44.660
Update and then make this one the exit.

09:46.280 --> 09:48.080
And exit.

09:50.040 --> 09:53.370
All right, so in the enter, what shall we do?

09:53.400 --> 10:02.130
Well, we want to grab hold of our list of our waypoints, and we will actually come back and do a bit

10:02.130 --> 10:04.110
more work in here later on.

10:04.110 --> 10:09.570
We don't need to save our waypoints into anything because remember the game environment, Singleton

10:09.570 --> 10:10.920
has those.

10:11.310 --> 10:14.070
So for now, we'll just leave that out of the enter.

10:14.070 --> 10:24.210
But what we don't want to do is put and set trigger and set is walking to make that animation start

10:24.210 --> 10:30.270
so that your agent will be obviously walking now to move around those waypoints.

10:30.270 --> 10:32.670
We'll do it inside of our update.

10:32.850 --> 10:35.820
And well, first of all, check that the agent.

10:36.860 --> 10:41.630
Hasn't finished its current walk between one waypoint and the other.

10:41.630 --> 10:47.660
So if agent remaining distance is less than one.

10:49.460 --> 10:52.910
Then we're going to move to the next waypoint.

10:52.910 --> 11:02.660
So if current index is greater than the number of waypoints, so game environment, this is where our

11:02.660 --> 11:03.950
singleton comes in.

11:03.950 --> 11:08.420
Dot singleton dot check points, dot count.

11:10.160 --> 11:10.520
Okay.

11:10.520 --> 11:15.500
I've just realized after I've gone a lot further in this video, is that this line is going to give

11:15.500 --> 11:16.520
you an error.

11:17.360 --> 11:24.170
Before you go any further, make sure that your current index is greater than or equal to the check

11:24.170 --> 11:27.650
point count minus one.

11:27.650 --> 11:32.870
So that's the line that you should be updating and keep it like that.

11:32.870 --> 11:38.090
Otherwise, the rest of the code is actually going to count outside of the range of the number of check

11:38.090 --> 11:39.770
points that you've got.

11:39.770 --> 11:46.190
So you won't see this line like this in the rest of the video, but make sure you keep yours like this

11:46.190 --> 11:55.070
to avoid that error happening, then current index is going to be equal to zero.

11:55.070 --> 12:01.550
So we're going to reach the end of the list of waypoints and switch back around to zero.

12:02.030 --> 12:05.030
Otherwise, we just want to go to the next one.

12:05.030 --> 12:12.710
So current index equals current index plus one or current index plus plus like that.

12:12.710 --> 12:16.220
And then once we have that, we can set the destination.

12:16.220 --> 12:26.210
So we'll go agent dot set destination and that destination will be that particular waypoint.

12:26.210 --> 12:33.440
So it will be game environment, dot singleton dot check points.

12:35.560 --> 12:39.220
At the index of current index.

12:39.640 --> 12:47.890
Now the set destination actually needs a vector three, so it will be the transform dot position of

12:47.890 --> 12:50.530
that particular waypoint.

12:51.240 --> 12:51.750
Okay.

12:51.840 --> 12:52.650
Got all that?

12:52.650 --> 12:59.400
And then we just have to end that bracket limit, make this just a bit bigger so you can see all of

12:59.400 --> 13:06.660
that code and you should be getting a really good feel of the singletons at this point is how easy that

13:06.660 --> 13:11.970
after we set up that list of check points, we can actually come back and just grab them in here without

13:11.970 --> 13:14.130
having passed them all through.

13:14.130 --> 13:24.150
So this is going to count through all of our waypoints and then move the agent on one after the other.

13:24.240 --> 13:29.850
Now, current index, if you remember rightly at the top here, is set to minus one.

13:29.850 --> 13:36.570
We actually don't want to try and start moving from minus one because there is no minus one for current

13:36.570 --> 13:37.170
index.

13:37.170 --> 13:45.510
So in the enter we'll also add in current index equals zero so that we start at the first one.

13:45.900 --> 13:46.260
Okay.

13:46.260 --> 13:47.850
So that's the update.

13:47.850 --> 13:50.310
Now, what goes into the exit?

13:50.310 --> 13:52.440
Well, not much really.

13:52.440 --> 13:57.810
All we want to do is make sure that we reset the trigger for the walking like we did with our idol.

13:57.810 --> 14:01.050
So Adam Dot reset trigger.

14:02.640 --> 14:03.720
Is.

14:04.860 --> 14:05.790
Walking.

14:07.090 --> 14:08.890
And that's pretty much it.

14:08.920 --> 14:13.960
Now, this patrol does not reference.

14:15.310 --> 14:17.140
Any other states.

14:17.480 --> 14:17.850
Okay.

14:17.860 --> 14:21.580
And the other one, the idle references to patrol.

14:21.580 --> 14:28.780
So as far as we're concerned, at this point in time, this is kind of enough to get us going to use

14:28.780 --> 14:29.860
the patrol.

14:30.580 --> 14:30.970
Okay.

14:30.970 --> 14:38.410
So when we come back in the next video, we'll write the eye class, which will drive all of these states.
