WEBVTT

00:06.400 --> 00:06.820
All right.

00:06.820 --> 00:09.430
So now down into the update.

00:09.430 --> 00:10.810
Let's get rid of the base.

00:10.810 --> 00:12.250
You're not going to need that again.

00:12.460 --> 00:19.600
First of all, we need to calculate the direction to the player and the angle because we want to rotate

00:19.600 --> 00:20.560
the NPC.

00:20.560 --> 00:23.920
So they're shooting in the direction of the player.

00:23.920 --> 00:26.020
Otherwise it's going to look a little bit strange.

00:26.290 --> 00:28.270
So this is going to be a vector three.

00:28.270 --> 00:39.130
We want direction equals player position minus NPC transform position.

00:40.660 --> 00:45.370
Then we're going to get our float angle equals vector.

00:45.370 --> 00:55.690
Three dot angles is the same code that we've written before for finding the angle, direction and NPC

00:55.990 --> 00:59.320
transform dot forward.

01:00.980 --> 01:12.950
And once we have that direction, what we're going to do with it is set the why of it to be zero direction

01:13.220 --> 01:15.150
y equals zero.

01:15.170 --> 01:16.470
Now, why am I doing that?

01:16.490 --> 01:24.380
Because we want to rotate the character around, but we don't want the character to tilt in the X or

01:24.380 --> 01:25.700
the Z axes whatsoever.

01:25.700 --> 01:30.860
We only want it to rotate on the spot which is around its up vector.

01:30.980 --> 01:39.140
Therefore, we have to make sure that any difference between the player and the NPC itself is zero as

01:39.140 --> 01:41.600
far as the height is concerned.

01:41.690 --> 01:44.840
Now to do the rotation, we'll use a return in.

01:44.840 --> 01:53.930
So NPC transform dot rotation equals turn in dot slip.

01:54.620 --> 01:59.600
So we're going to do spherical looping between two angles.

01:59.600 --> 02:06.140
So NPC's transform dot rotation, it's current rotation.

02:06.500 --> 02:10.820
And then we put in a return in.

02:12.660 --> 02:20.760
Not look rotation in the direction we have just calculated, which is the vector towards the player

02:21.030 --> 02:24.690
and we want to rotate that using our speed.

02:25.110 --> 02:32.850
So that'll be timed at Delta time multiplied by rotation speed.

02:35.200 --> 02:35.360
Okay.

02:35.550 --> 02:41.670
So that's going to rotate your NPC around to always be facing the player as it's attacking.

02:41.700 --> 02:45.840
Now, remember, we need a way to get out of update.

02:45.840 --> 02:48.620
So when are we going to stop attacking the player?

02:48.630 --> 02:56.820
Well, if we can't see the player or actually if we can't attack the player anymore, so the player

02:56.820 --> 03:02.280
has gone out of range, then we will next.

03:04.430 --> 03:05.570
State.

03:07.330 --> 03:10.920
Equals New Idol and.

03:12.220 --> 03:13.450
Agent.

03:14.540 --> 03:16.550
Adam player.

03:17.600 --> 03:24.950
You could go back to any state from here, so you could go back to the pursue state or you could go

03:24.950 --> 03:27.320
back to the patrol state.

03:27.350 --> 03:29.930
It's up to you how you want this to work.

03:30.110 --> 03:36.740
The reason I'm doing this is that if you've been in idle, idle actually will let you get to any other

03:36.740 --> 03:37.520
state.

03:38.900 --> 03:43.640
If you wait long enough and you don't have to wait long enough, because what will happen in this case

03:43.640 --> 03:49.820
is if you get out of range, he's going to stop shooting at you, which will flick it back into the

03:49.820 --> 03:51.030
idle state.

03:51.050 --> 03:57.050
Then there'll be like a 10% chance you're going to go into the patrol state and then it's the patrol

03:57.050 --> 04:02.120
state that will determine if it can see the player and it can then walk towards the player and then

04:02.120 --> 04:02.810
attack the player.

04:02.810 --> 04:06.440
So it kind of going in a full loop to come back again.

04:06.440 --> 04:13.910
And it does mean if you run fast enough and get out of range of the NPC, then it's just going to forget

04:13.910 --> 04:18.860
about you and it'll go to idle anyway and then go back into patrol.

04:19.280 --> 04:29.180
Okay, so that's our get out of the update and we just need to go stage equals event exit to make sure

04:29.180 --> 04:30.500
that that happens.

04:32.400 --> 04:36.180
And then finish off with that bracket.

04:36.420 --> 04:43.320
So then we make our way down into the exit and we will have some usual stuff like we've done before

04:43.320 --> 04:43.830
with Adam.

04:44.040 --> 04:45.030
Reset.

04:45.150 --> 04:48.570
Trigger four is shooting.

04:50.490 --> 04:52.560
And then shoot.

04:52.680 --> 04:54.810
Don't stop.

04:57.100 --> 05:03.580
So that's pretty much the code for the attacking and also for the pursuing.

05:03.940 --> 05:09.130
What we don't yet have is a way to trigger those states from starting.

05:09.130 --> 05:18.550
So let's go right back up the top to our idol and we're going to go into the update and set it so that

05:18.550 --> 05:24.340
if the player becomes visible that we start to pursue them.

05:24.580 --> 05:28.930
Okay, so let's just go down in here and let's do it.

05:29.320 --> 05:30.880
In fact, I'll just do it above.

05:30.880 --> 05:33.310
We'll make this one an else if for that.

05:33.310 --> 05:36.580
And then we'll go above here and put if.

05:38.020 --> 05:40.220
You can see Playa.

05:43.080 --> 05:47.940
Next state equals new pursue.

05:49.160 --> 05:50.930
And P.S..

05:53.110 --> 05:57.130
Agent Adam and player.

05:59.390 --> 06:01.550
And then stage.

06:02.960 --> 06:05.270
Equals event dot.

06:06.400 --> 06:06.850
Exit.

06:06.850 --> 06:15.960
So we go straight into the pursue and we can also in the patrol use this exact same if statement.

06:15.970 --> 06:21.340
So if we are patrolling and we see the player will stop and start pursuing.

06:21.340 --> 06:28.240
So copy that and let's go down into patrol and we're going to put that inside of the update.

06:28.240 --> 06:30.010
Let's just put it after here.

06:30.820 --> 06:36.370
As long as it's in the update and it's going to get executed at some point and then it switches to the

06:36.370 --> 06:38.980
new state, which requires these two lines.

06:39.100 --> 06:40.600
Of course, we'll be fine.

06:40.600 --> 06:49.030
Now we know that if you're already in pursue and you get close enough, then it will trigger the attack.

06:49.030 --> 06:51.160
So we're not going to go straight into attack.

06:51.160 --> 06:52.600
We're going to give a pursue.

06:52.900 --> 06:59.200
Now, even if you do suddenly get extremely close to it and it could directly attack you, it will go

06:59.200 --> 07:06.280
via the pursue and not really cost you a lot extra to do it that way anyway.

07:06.280 --> 07:06.580
Right.

07:06.580 --> 07:08.590
So we've got that save that.

07:08.590 --> 07:15.430
Now let's go back and just before we try this out, if you select, I can go to the I code.

07:15.430 --> 07:22.600
We've got this PHP character that we put in there, but this character isn't actually where our actual

07:22.600 --> 07:23.980
character is.

07:24.310 --> 07:26.050
It's sort of holding on to our character.

07:26.050 --> 07:31.930
The character is basically defined by this capsule, so we're more interested in him being able to see

07:31.930 --> 07:34.690
this capsule in his visible range and distance.

07:34.690 --> 07:38.350
So I just get that and change it up in your code.

07:38.350 --> 07:42.010
So it's looking at that instead, right?

07:42.010 --> 07:44.320
So let's press play.

07:44.320 --> 07:47.740
And we might already be in his visual range.

07:47.740 --> 07:48.280
Yes, we are.

07:48.280 --> 07:50.890
So he's just going to start shooting us like that.

07:50.890 --> 07:55.840
Now, if you hold down the shift and se, you should be able to run away far enough.

07:56.080 --> 08:01.810
And then if we can get around these trees, I keep colliding into behind me you go, he'll lose interest

08:01.810 --> 08:04.300
and go back to patrolling.

08:04.300 --> 08:08.410
So we should at this point be able to sneak up behind him.

08:10.990 --> 08:11.410
Okay.

08:11.410 --> 08:14.230
Let's just show you something else in here.

08:14.230 --> 08:21.430
So if I get past him and get in his face again, he will start chasing after me again.

08:22.420 --> 08:25.420
Shift down to run away.

08:27.260 --> 08:29.660
Eventually he's going to lose interest.

08:31.220 --> 08:32.330
And go back.

08:32.330 --> 08:40.250
Now, if we follow him and have a look, what happens is he will actually head straight back to Waypoint

08:40.250 --> 08:48.590
Zero because he is programmed to create a new patrol state every time.

08:49.190 --> 08:52.190
So that's not particularly realistic.

08:52.190 --> 08:59.690
He should, when he finishes chasing us, go back to the nearest waypoint and follow that.

08:59.690 --> 09:05.720
Now, this will inherit some issues that you've got with walking through geometry and that.

09:05.720 --> 09:11.990
But because he's a nav mesh agent and because we have colliders and that and the environment, he should

09:11.990 --> 09:14.780
be able to kind of get around those.

09:15.200 --> 09:18.380
Okay, so let's just stop playing.

09:18.650 --> 09:25.400
Now, what we're going to do instead is just update our code to find the nearest waypoint.

09:25.670 --> 09:26.320
Resist.

09:26.360 --> 09:38.210
Okay, so let's open up our code for the state and we're going to put that in to the patrol state into

09:38.210 --> 09:44.330
the enter, where we will find the closest rather than just using zero.

09:45.050 --> 09:51.560
So let's get rid of this current index and instead we're going to figure it out.

09:51.560 --> 10:00.380
First of all, we need a float to store in the distance that we're calculating between each of the waypoints

10:00.380 --> 10:04.850
and the NPC and to remember the smallest distance.

10:04.850 --> 10:12.350
So we'll start with a float called lastest and we're going to set it to something very big and very

10:12.350 --> 10:14.480
big means infinity.

10:14.750 --> 10:20.600
You can't get much larger than that so that every other distance we compare to begin with is going to

10:20.600 --> 10:22.040
have to be smaller.

10:22.220 --> 10:26.330
Okay, now we need for int i equals zero.

10:26.480 --> 10:32.000
I is less than and here comes our fantastic singleton.

10:34.040 --> 10:37.220
Dot check points don't count.

10:38.770 --> 10:41.270
I plus plus.

10:41.980 --> 10:53.020
So this is our for loop that we're going to go around them all then we're going to get the singleton.

10:53.020 --> 10:56.170
So game object.

10:57.940 --> 11:02.560
This way point equals.

11:05.120 --> 11:07.550
Game environment.

11:07.850 --> 11:09.830
Dot singleton.

11:09.950 --> 11:12.710
Dot check points.

11:13.310 --> 11:14.210
I.

11:16.650 --> 11:18.240
Then we work out the distance.

11:18.240 --> 11:34.320
So float distance equals vector three dot distance between the NPC dot transformed position and this

11:34.320 --> 11:38.460
waypoint dot transformed position.

11:38.790 --> 11:40.990
I'm all writing this out in separate lines.

11:41.010 --> 11:43.890
Of course, if you want to put it in one great big line, that's fine.

11:43.890 --> 11:49.290
But as far as your readability goes, if I did that, you wouldn't be able to see what was going on.

11:49.320 --> 11:52.200
Okay, so we've now got our waypoint.

11:52.200 --> 11:53.730
We've got the distance that we're going to go.

11:53.730 --> 12:02.370
If the distance we just calculated is less than the last dist than this way, a point must be closer.

12:02.490 --> 12:05.910
Therefore, current index is going to equal.

12:07.110 --> 12:12.780
And last distance is going to equal distance.

12:12.840 --> 12:15.210
So we're updating each time.

12:15.210 --> 12:21.780
So every time that we get a smaller distance, last distance, we'll get smaller and smaller until we've

12:21.780 --> 12:24.540
found the one that we want.

12:24.930 --> 12:30.810
Now, there's one little tweak that I have to do in here to get this to work.

12:31.110 --> 12:41.010
We know that we're getting the index of the waypoint that's closest to us, but where the NPC is going

12:41.010 --> 12:44.850
to head is to the one that's after that.

12:44.880 --> 12:46.740
Now, how do I know that?

12:46.740 --> 12:56.640
Well, down in the update, I'm adding a one to it or setting it back to zero before I set the destination.

12:56.730 --> 13:07.470
Therefore, we actually have to sit this one here to minus one or some way said it inside of the update,

13:07.470 --> 13:11.430
which is going to make it a little bit difficult because the updates are constantly running around.

13:11.430 --> 13:19.020
So it's going to be easier here if we go minus one and then when we come in to update, let current

13:19.020 --> 13:20.160
index update it.

13:20.160 --> 13:26.640
So if the closest one is say three then.

13:27.630 --> 13:29.150
And we don't have the minus one.

13:29.160 --> 13:32.280
It's going to come in here and add one to it and give us four.

13:32.520 --> 13:34.320
And so we're going to head to the wrong one.

13:34.320 --> 13:37.920
So we need that minus one up in there.

13:38.460 --> 13:38.910
Okay.

13:38.910 --> 13:47.160
So just telling you that, so that you don't use this sort of code to find your closest proximity to

13:47.160 --> 13:51.420
a list of things without getting rid of this minus one.

13:52.260 --> 13:57.300
Okay, let's press play and have a look at his behavior now.

13:57.390 --> 14:02.090
So we're going to get shot at immediately because of where we are.

14:02.100 --> 14:03.290
So this is Waypoint zero.

14:03.300 --> 14:05.010
Where is actually standing?

14:05.400 --> 14:09.270
Let's let's go over here.

14:09.450 --> 14:11.910
He should just follow us around.

14:12.570 --> 14:15.960
It's easier if I go forward because I'm bound to run into something.

14:16.170 --> 14:21.480
Okay, so now I can go fast back here until he loses interest.

14:22.490 --> 14:23.820
Okay, so he's walking.

14:23.940 --> 14:26.840
Should turn around shortly if he doesn't catch up with me.

14:26.850 --> 14:27.240
There we go.

14:27.270 --> 14:33.210
Okay, so let's sneak back and follow him to see which waypoint he goes to first.

14:33.210 --> 14:40.350
So he should pick up his closest waypoint, which is that one.

14:40.470 --> 14:46.260
And then when he gets there, he will continue on his route, as you just saw then.

14:46.260 --> 14:52.470
So he doesn't need to go back to the start each time because that makes no sense whatsoever.

14:53.010 --> 14:53.640
Okay.

14:53.640 --> 14:59.670
So that is the state pattern in finite code.

14:59.670 --> 15:03.750
And the AI is extremely short and we're.

15:05.920 --> 15:16.480
Totally relying on these states and the code in these states to embody a single state, but also tell

15:16.510 --> 15:20.880
the NPC how to move from one state into another.

15:20.890 --> 15:27.720
So it's basically once you've gone into another state where you came from is completely forgotten about.

15:27.730 --> 15:35.680
And if you did need a system that was like that, then you may actually want to keep some kind of link

15:35.680 --> 15:40.810
to the previous state, which you could if you go to the top here.

15:41.110 --> 15:44.960
And where do we have our next state declared?

15:44.980 --> 15:46.870
We've got our next state here.

15:46.870 --> 15:52.000
You could have another one that says the last state that you came from.

15:52.270 --> 15:57.670
In a way, it'd be like a double E linked list where you can go back and forth between the states if

15:57.670 --> 15:58.870
that's what you needed.

15:59.530 --> 15:59.920
All right.

15:59.920 --> 16:05.230
So I hope you enjoy learning about the state pattern, because I think it's really, really powerful

16:05.620 --> 16:07.630
and a lot of fun to play around with.

16:07.630 --> 16:10.490
And you can build so many different systems with it.

16:10.510 --> 16:19.450
In fact, the GOP's system that I talk about in my course actually uses a state class system very much

16:19.450 --> 16:20.440
like this one.
