WEBVTT

00:06.300 --> 00:06.810
Okay.

00:06.810 --> 00:14.940
It's time to start getting a little bit of AI behavior built into our NPC and we're going to do that

00:14.940 --> 00:19.920
in the States so we don't actually have to touch the AI script at all.

00:20.040 --> 00:26.670
We'll go into the States and just transition different states to change the behavior of the NPC, which

00:26.670 --> 00:28.410
is what they're all about.

00:28.440 --> 00:34.980
Let's go right to the top of our states code into the base state.

00:34.980 --> 00:39.630
What I'm going to do in here is add a few little helper methods.

00:39.990 --> 00:47.460
First of all, the logic we're going to use is can the NPC see the player?

00:47.460 --> 00:54.570
So we're going to use a sort of simple line of sight calculation, which means that the player has to

00:54.570 --> 01:03.960
be within a certain distance, but they also have to be in front of the NPC, at which point the NPC

01:03.960 --> 01:10.860
can then decide whether he's going to chase the player down or whether he's going to start shooting

01:10.860 --> 01:11.700
at the player.

01:11.700 --> 01:19.050
So we will have it so that it has to be a little bit closer to the player in order to shoot, which

01:19.050 --> 01:25.950
means that you can run away and have the embassy actually run after you until it gets into the shooting

01:25.950 --> 01:28.440
range when it will switch back to shooting.

01:28.440 --> 01:34.530
So it's a little bit more dynamic than have it just shoot at you want to get it to move around the map

01:34.530 --> 01:35.520
and follow you.

01:36.150 --> 01:38.370
First of all, let's build these two methods.

01:38.370 --> 01:41.790
So first we want a public bool.

01:41.790 --> 01:47.040
So it's going to return true or false and it will be can see player.

01:48.520 --> 01:52.880
So we're just asking a very simple question can the NPC see the player?

01:52.900 --> 01:58.960
Now, in order to do that, we have to calculate the distance and also the angle.

01:59.110 --> 02:03.100
So let's start with the vector three.

02:03.100 --> 02:09.730
That will be a direction from the player dot position.

02:11.500 --> 02:16.180
Minus the NPC transform position.

02:17.320 --> 02:23.890
So this is giving us the vector from the NPC to the player.

02:24.560 --> 02:24.940
Okay.

02:24.940 --> 02:27.070
Then we can find the angle.

02:27.160 --> 02:41.760
Float angle equals vector three dot angle built in unity math using the direction vector and the NPC

02:41.770 --> 02:48.550
is trance form dot forward vector.

02:48.940 --> 02:55.630
Then with that angle, we can figure out whether we are within the line of sight.

02:55.690 --> 02:55.960
Okay.

02:55.960 --> 02:59.470
So the if statement is going to go a little bit like this.

02:59.530 --> 03:07.240
If direction, which is that vector between the player and the NPC dot magnitude, which is the length

03:07.240 --> 03:08.110
of that vector.

03:08.110 --> 03:14.340
So the distance between the player and the MBC is less than vs dist.

03:14.350 --> 03:19.840
Remember, that's our float we created very early on up the top.

03:19.840 --> 03:23.320
In fact, we're going to use all of these in this little set of methods.

03:23.320 --> 03:30.460
So we've got our visible distance at ten, our visible angle at 30 degrees and our shooting distance

03:30.460 --> 03:31.000
at seven.

03:31.000 --> 03:36.660
And you can change those and play around with them as you like to get different behaviors.

03:36.850 --> 03:44.560
Okay, so there's our visible distance and we also want the angle to be less than the viz angle.

03:44.560 --> 03:51.310
So the visible angle is set to 30 and we're going to be finding the angle between the direction and

03:51.310 --> 03:56.980
the forward vector of the NPC, which will be half of its total visibility.

03:56.980 --> 04:03.880
So if you want like a 60 degree field of view for your NPC, then we're just give it a viz angle of

04:03.880 --> 04:05.950
half of that, which is the 30.

04:05.980 --> 04:15.130
Now if that if is correct, then we can say that we're going to return.

04:15.130 --> 04:15.820
True?

04:15.820 --> 04:18.940
Yes, I can see the player.

04:20.310 --> 04:20.700
Okay.

04:20.760 --> 04:26.580
Just fix up these brackets and we're done with that one.

04:27.720 --> 04:28.530
Oh, of course.

04:28.530 --> 04:34.560
We also need to return false as our fallback that we can't see the player.

04:34.800 --> 04:41.730
The other method that will add just after this one will be to determine if we're close enough to attack

04:41.730 --> 04:49.020
the player so we make it a public ball again, can attack player.

04:51.300 --> 04:58.410
And then inside of here, what we're going to do is get our direction vector again so we can just copy

04:58.410 --> 04:59.610
this one here.

05:01.850 --> 05:05.680
And we're simply going to check the length of it.

05:05.690 --> 05:19.550
So if direction magnitude is less than the chute, distance or dist in this case, then we will return.

05:21.460 --> 05:22.570
True.

05:24.400 --> 05:26.590
Otherwise, we'll return.

05:27.310 --> 05:28.390
False.

05:29.380 --> 05:29.660
Okay.

05:29.710 --> 05:34.720
So I think I just lost a bracket in there for the end of the class and we put that back in.

05:34.950 --> 05:35.200
Okay.

05:35.200 --> 05:44.440
So these are the two methods that we're now going to use further down in other states to determine if

05:44.440 --> 05:48.550
we should then start following the player.

05:48.580 --> 05:53.770
Now, we don't yet have those states, so we're going to add them at the bottom.

05:53.980 --> 05:59.740
So scroll right down to the bottom or open up a new file if you're putting all of these in separate

05:59.740 --> 06:01.270
class files.

06:01.420 --> 06:04.760
And let's start with pursue.

06:04.780 --> 06:08.800
So public class pursue.

06:09.340 --> 06:11.500
And that will be a state.

06:12.690 --> 06:15.690
Then the first thing we need to do is our constructor.

06:15.690 --> 06:16.860
So public.

06:18.050 --> 06:19.520
Pursue.

06:20.150 --> 06:25.700
And it's pretty much going to have the same stuff as our other states, which is this here, because

06:25.700 --> 06:28.400
we need to pass through all of these details.

06:28.610 --> 06:31.250
So let's go and add this in.

06:32.630 --> 06:33.980
Like that.

06:34.010 --> 06:35.060
Okay.

06:35.420 --> 06:38.390
Then inside of here, we're going to set the name.

06:38.390 --> 06:43.970
So the name will be equals state dot pursue.

06:45.740 --> 06:48.050
Not that state.

06:48.050 --> 06:49.640
They're like that.

06:50.240 --> 06:52.130
Then we need agent speed.

06:52.130 --> 06:53.780
So agents speed.

06:53.780 --> 06:59.000
In this case, when he's chasing after you, let's make that five.

06:59.420 --> 07:04.010
So and actually be running rather than walking when he's pursuing you.

07:04.010 --> 07:11.900
So you do need to make it a bit bigger and then just make agent dot is stopped equal to false.

07:15.870 --> 07:19.140
All right, now let's get into our enter.

07:19.140 --> 07:22.020
So public override.

07:22.410 --> 07:23.250
Void.

07:23.250 --> 07:24.060
Enter.

07:25.570 --> 07:28.360
And it's going to.

07:30.650 --> 07:34.970
Set the animation for running so animate set trigger.

07:37.040 --> 07:45.380
He is running and that's all we need to do inside of the enter the method that's going to do most of

07:45.380 --> 07:47.060
the work will be update in this case.

07:47.060 --> 07:54.230
So public override void update.

07:55.560 --> 07:59.750
And it will have not the update as we've learnt before.

07:59.760 --> 08:08.010
Let's just get rid of that because what it doesn't isn't any use to us agent set destination and this

08:08.010 --> 08:13.290
will set a goal position for the agent for the nav mesh stuff.

08:13.290 --> 08:19.830
So it's player dot position which will set it.

08:19.890 --> 08:29.670
Then we're going to say if the agent dot has a path, which means it's actually following the player,

08:30.060 --> 08:33.000
we're going to test if we can attack.

08:33.840 --> 08:41.130
So we're in pursuit and then if we're close enough, we want to actually switch over into an attack.

08:41.130 --> 08:46.590
So if can attack player.

08:48.960 --> 08:56.040
And then inside of that we will put next state equals new attack.

08:57.150 --> 09:10.860
And that again we'll have our NPC agent Adam and player passed through and then stage will be equal

09:11.340 --> 09:13.050
to event.

09:13.800 --> 09:14.850
Dot.

09:15.690 --> 09:23.160
Exit because we're now finished with this pursue state and we want to then switch over into the exit.

09:23.430 --> 09:28.360
Now, if we can't attack the player, then we go.

09:28.380 --> 09:37.890
So going to test if else, if we can't see the player, so not see the player.

09:37.890 --> 09:45.780
Remember that seeing the player switches in to this state and then once we can't see the player any

09:45.780 --> 09:49.770
more, we want to switch back into the patrol state.

09:50.040 --> 09:58.710
So next state, in this case, if you can't see the player, will equal new patrol.

09:59.460 --> 10:01.680
Again, we want to pass through these things.

10:01.680 --> 10:04.980
Let's just copy them from up there, bring them down here.

10:05.460 --> 10:14.880
And again, we're going to stage equals event, not exit like that.

10:14.880 --> 10:18.420
So that is our update.

10:18.420 --> 10:22.220
Let's just close the brackets off for the has path.

10:22.230 --> 10:29.310
Now, why am I checking for this has path if we've obviously set a destination up here?

10:29.310 --> 10:37.500
Well, with the nav mesh system, what happens is you can set a destination, but the nav mesh processing

10:37.500 --> 10:46.230
itself may not have yet taken place by the time you get from here to the next sort of lines.

10:46.230 --> 10:55.080
So you want to least wait one update until it comes back into here again to figure out if you have a

10:55.080 --> 10:59.160
path and then figure out the attacking and stuff.

10:59.160 --> 11:06.000
Otherwise you get really strange behaviour and it's just a thing that the nav mesh does and the way

11:06.000 --> 11:13.560
the nav mesh does it, you will have set up something for the nav mesh and told it to set a destination

11:13.560 --> 11:16.710
or work out a path or do something with the agent.

11:16.710 --> 11:22.230
And then you'll be trying to do stuff immediately after in your code and it won't work.

11:22.230 --> 11:28.260
And that's because it's got to run stuff in the background before it's in a state of sort of readiness.

11:28.410 --> 11:30.690
And this here is just testing for that.

11:30.990 --> 11:31.350
All right.

11:31.350 --> 11:33.690
So we've now done our update.

11:33.690 --> 11:43.380
Let's put in our exit and that will be public override, void, exit.

11:45.270 --> 11:55.020
And when we exit we're going to reset the trigger for our is running to just make sure that it's not

11:55.020 --> 11:56.760
hanging around on us.

11:56.790 --> 11:58.380
Then our base exit.

11:58.740 --> 12:03.870
So that's all done for the pursue.

12:07.210 --> 12:15.460
Right following that, our final state for now is a public class attack.

12:16.530 --> 12:17.550
State.

12:18.030 --> 12:21.120
And this is the attack that we're using up here.

12:21.120 --> 12:24.810
If you can see the player, we want to go into attack mode.

12:25.350 --> 12:25.950
Okay.

12:25.950 --> 12:28.110
So what are we going to have in here?

12:28.140 --> 12:37.410
Well, the NPC in this case actually has to follow the player or turn and look at the player.

12:37.650 --> 12:39.930
If you'd like it to as it's shooting.

12:39.930 --> 12:49.740
So we're going to put a float rotation speed value in here equals put to F so we can rotate around.

12:49.830 --> 12:55.810
We're going to grab the audio source that's attached to the NPC.

12:55.830 --> 13:02.460
Now, this is the case I mentioned in the previous lecture where I'm going to have a get component to

13:02.460 --> 13:08.700
get the audio source, whereas you probably would be better off passing it through rather than sort

13:08.700 --> 13:13.380
of getting it every time an attack situation comes up.

13:13.620 --> 13:15.420
But this is the way I've done it.

13:15.420 --> 13:17.990
So public attack.

13:18.000 --> 13:21.330
Now we need all of our bits and pieces that we had before.

13:21.330 --> 13:29.430
For these, let's go up to the pursue and just grab all of that because we're using that again, put

13:29.430 --> 13:37.050
it into our attack and then let's name it.

13:37.050 --> 13:42.510
So name equals state dot attack.

13:44.410 --> 13:54.370
And shoot is going to equal the PNP secret component.

13:55.900 --> 14:02.440
For the audio source that's attached to UG, which is a shooting sound.

14:03.400 --> 14:07.390
Okay, so that's all we're doing inside of that attack.

14:07.600 --> 14:08.190
All right.

14:08.200 --> 14:10.780
Next, we need the old use rules again.

14:10.780 --> 14:18.850
So public override, void, enter and.

14:19.630 --> 14:21.970
Public override.

14:21.970 --> 14:23.170
Void.

14:24.550 --> 14:25.840
Update.

14:27.370 --> 14:29.830
And the exit.

14:29.830 --> 14:36.250
So public override, void, exit.

14:37.690 --> 14:42.550
When we enter this state, first of all, we want to set up a shooting animation.

14:42.550 --> 14:45.370
So animate set trigger.

14:46.850 --> 14:48.320
Is shooting.

14:52.100 --> 15:04.370
Again Agent Dot is stopped is going to be sit in this case to true like we do in this case we want to

15:04.370 --> 15:09.890
stop the agent because they're attacking they've come out of the pursue.

15:09.890 --> 15:15.140
So they're actually the agent is walking along a path towards the player.

15:15.140 --> 15:22.700
But as soon as it sees the player, it has to stop that path.

15:22.940 --> 15:30.260
So by putting agent is stopped equals true will actually sort of stop it in its tracks and then it can

15:30.260 --> 15:37.310
start shooting and that will prevent it from following you while it is still shooting.

15:37.370 --> 15:47.570
Now we also need to start the shooting sound so we go shoot play and that will start it for us.

15:48.660 --> 15:55.620
Next, we're on to the update method and we'll have to do that in the next video because I'm well,

15:55.620 --> 15:59.250
I'm running out of time, so I will see you in there.
