WEBVTT

00:13.520 --> 00:19.370
The pursuit behavior is a little like seek, except instead of following a target, the agent predicts

00:19.370 --> 00:22.850
where the target will be in the future in order to intercept it.

00:23.390 --> 00:26.170
We say this occurring all the time in sports games.

00:26.420 --> 00:32.540
In the case of football, the actual ball provides players with the predicted future position of their

00:32.570 --> 00:33.210
opponent.

00:33.950 --> 00:40.010
Now, in order to determine the location our pursuer the robber should steer towards, we need to get

00:40.010 --> 00:43.880
the target's position for direction of travel and its speed.

00:44.210 --> 00:50.150
We also need the pursuit of speed and his distance to the target as they are going to depend on how

00:50.150 --> 00:52.010
quickly the robber can catch up.

00:52.700 --> 00:58.580
We can then predict where the pursuer should head towards based on these values and calculate how far

00:58.580 --> 01:05.420
ahead of the target the pursuers should aim as the distance between the pursuer and target divided by

01:05.420 --> 01:08.360
the addition of the speeds of both agents.

01:08.960 --> 01:11.870
So let's go ahead and implement this in our bot script.

01:11.870 --> 01:17.660
So open up your BOT code and we're going to put pursue and flee.

01:17.660 --> 01:18.500
Just stand here.

01:19.040 --> 01:22.940
OK, so pursue will have its own method obviously.

01:22.940 --> 01:25.460
So avoid pursuit.

01:26.870 --> 01:35.150
And then inside of here, what we're going to do is first of all, work out the vector to the target.

01:35.720 --> 01:36.920
So vector three.

01:39.050 --> 01:51.530
Target, they are so the direction is target, dot, transform, dot position minus this dot transform

01:52.160 --> 01:53.300
the position.

01:55.070 --> 02:01.130
Now, once we've got that, we can actually go ahead and calculate the look ahead as to how far we're

02:01.130 --> 02:05.960
going to look ahead past the forward facing direction of the target.

02:05.990 --> 02:07.690
So look ahead.

02:08.300 --> 02:11.580
And this is where we're predicting the future location.

02:12.050 --> 02:20.990
OK, so it'll be target dot magnitude and we're going to divide that by the agents speed.

02:20.990 --> 02:33.290
So the agent speed plus the speed of the drive that's going on on the cop to do that, it will be the

02:33.290 --> 02:39.840
targets dot get component for the drive script.

02:40.640 --> 02:46.640
And remember, we created that current speed or I did and put it in there and I showed it to you earlier

02:47.030 --> 02:53.090
where we're updating the actual speed and we made it into a public value so that you could access it

02:53.360 --> 02:54.620
back in the drive script.

02:54.890 --> 02:57.860
OK, so that's getting the look ahead.

02:57.860 --> 03:00.340
And then what we're going to do to that is actually seek.

03:00.800 --> 03:03.470
So we will seek the target.

03:04.820 --> 03:08.690
Dot transform dot position.

03:10.370 --> 03:18.230
Plus, the look ahead, but the look ahead we're going to use as a multiplier against the targets forward

03:18.350 --> 03:18.980
Victor.

03:19.520 --> 03:28.430
OK, so the forward victor is the way that the target is facing and traveling form forward, and it

03:28.430 --> 03:29.330
has a length of one.

03:29.330 --> 03:31.730
So it's automatically normalized for us.

03:31.730 --> 03:37.310
So we multiply that by look ahead, gives us a vector that is the length of look ahead and we're therefore

03:37.310 --> 03:40.020
adding that on to the seat.

03:40.520 --> 03:48.080
OK, so we've got that now or we need to do with it is come down into update and just change this to

03:48.110 --> 03:48.830
pursue.

03:50.780 --> 03:51.850
So that we can run it.

03:51.890 --> 03:58.850
OK, so let's save that and we'll go back in to Unity and let's press play.

03:59.420 --> 04:03.620
OK, so you can see he's coming over to the cop.

04:03.660 --> 04:05.660
Remove the cop.

04:07.150 --> 04:15.520
And if we change directions, you'll see that he will then be predicting the future direction, rather.

04:17.750 --> 04:19.160
Then just blindly.

04:21.020 --> 04:25.400
Following, I think you can just kind of see it how far he's looking ahead.

04:26.780 --> 04:31.190
In that case, now, the faster they're going, the more looking ahead that will happen.

04:31.730 --> 04:39.620
Now, you might have also noticed is that when the robber reaches the cop, he lands right on top of

04:39.620 --> 04:42.470
the cop, which is not really what you want.

04:42.470 --> 04:44.390
You want it to stop a little further away.

04:44.420 --> 04:47.420
Now, that's actually a dynamic of the nerve agent.

04:47.420 --> 04:53.330
And you can fix that if you select the robber and in the inspector, you go and modify the stopping

04:53.330 --> 04:53.800
distance.

04:53.810 --> 04:55.460
So I've currently got one.

04:55.850 --> 05:04.490
If I set that to two and then we press play, he will stop within a distance of two of the goal location.

05:05.540 --> 05:11.600
Now, if you want to force that looking ahead a bit more than it actually is, that you can't really

05:11.600 --> 05:12.410
see it there.

05:13.340 --> 05:18.610
Let's just go back into our code and we can multiply this out further.

05:18.620 --> 05:24.290
So it's actually looking further than lookahead let's multiply that by five just to give it a really

05:24.290 --> 05:26.880
good, strong look ahead, I guess.

05:27.200 --> 05:28.890
Let's go back and press play.

05:29.300 --> 05:32.410
Now, what you'll find is he will look ahead.

05:32.420 --> 05:37.280
Now, my boss not even going at this point, but once you start adding.

05:38.720 --> 05:40.040
A speed.

05:41.140 --> 05:42.810
The look ahead, let's get going.

05:44.370 --> 05:45.510
Turn around.

05:47.040 --> 05:52.950
And there you go, so then now the calculation for the look ahead has become quite large.

05:53.200 --> 06:00.060
So it's the speed that it was going in this direction and the bot's ended up over there somewhere right

06:00.060 --> 06:01.590
on the edge of the map.

06:02.370 --> 06:07.230
However, if this is happening, which it shouldn't, because you're not really meant to multiply that

06:07.230 --> 06:15.420
look ahead, you could fix it by putting in another condition to test if your cop, your target has

06:15.420 --> 06:20.140
stopped moving, in which case you would then go into a Sikh instead.

06:20.160 --> 06:25.110
So that's sort of one of the extra conditions that you could add into this pursue.

06:25.650 --> 06:33.690
And what we will also do is improve this by adding some other conditions that deal with whether you

06:33.690 --> 06:37.440
should be seeking or whether you should actually be looking ahead.

06:38.470 --> 06:43.750
First, of course, one condition we will consider is that if the target has stopped moving, then we

06:43.750 --> 06:48.260
will turn the pursuit into a seat and just head straight towards the target.

06:48.640 --> 06:54.190
The next condition we are going to consider is that if the robber is in front of the cop and pretty

06:54.190 --> 07:00.220
much already heading in the same direction, then the robber should turn around and just seek the cop.

07:00.700 --> 07:03.970
So how do we determine if the robber is in front of the cop?

07:04.240 --> 07:10.380
Well, we use angles and we use the angles between the various vectors that we've already been using.

07:10.870 --> 07:16.390
When the robber is in front of the cop, the angle between the robbers forward vector and the vector,

07:16.390 --> 07:19.480
the cop will be greater than 90 degrees.

07:20.410 --> 07:26.620
When the robber is behind the cop, the angle is going to be less than 90 degrees.

07:27.860 --> 07:35.120
Now, if the robber is in front of the cop, he might already have turned around to go towards the interception

07:35.120 --> 07:35.590
point.

07:36.200 --> 07:39.450
If this is the case, we don't want him to seek.

07:39.900 --> 07:47.420
Instead, we have to restrict our conditions for seeking even more by considering the angle between

07:47.420 --> 07:50.180
both of the four directions of the agents.

07:50.480 --> 07:56.420
And if this is quite small, then we will allow the pursuit to turn into a Sikh.

07:57.910 --> 08:05.770
So let's go ahead and add in these conditions into the Persue method, so the first one we have is if

08:05.770 --> 08:13.130
the target has stopped moving so we can test that if the speed, which is this here.

08:13.130 --> 08:14.470
Let me just copy that.

08:16.420 --> 08:27.670
If the speed is less than zero point zero one F, and I'm saying that rather than saying equals zero,

08:28.000 --> 08:33.370
because sometimes you can get a floating arrow with zero values and it might not actually be equal to

08:33.370 --> 08:33.830
zero.

08:34.030 --> 08:36.350
It might be a very, very small number.

08:36.520 --> 08:43.660
So by doing this, you kind of covering yourself that if we're pretty much standing still our target,

08:43.660 --> 08:47.270
then what we're going to do down in here is seek.

08:47.290 --> 08:50.340
So we're just going to seek the target.

08:51.280 --> 08:54.970
So the target dot transform position.

08:57.530 --> 09:02.450
And then we're also going to return rather than continuing in this method.

09:02.990 --> 09:08.570
Now the other things that we want to test for is the relative heading of the agent.

09:08.620 --> 09:18.440
So if we have a small, small angle between the forward directions of the agents, then we're just going

09:18.440 --> 09:24.500
to get our agent to follow or seek the target instead of looking ahead.

09:24.530 --> 09:27.790
So we need to calculate that particular angle.

09:27.800 --> 09:35.900
So let's go float relative heading or call it equals vector three dot angle.

09:36.440 --> 09:45.050
And that angle is going to be between this dot transformed forward and we want to get the forward vector

09:45.080 --> 09:47.360
of the cop or the target.

09:48.050 --> 09:56.150
Now we have to get the forward vector relative to the actual agent, which means we need to translate

09:56.480 --> 10:04.410
that vector, which is the forward direction of the target into the space of the agent.

10:04.880 --> 10:12.980
What that essentially means is you're kind of making that agent the origin point of its own little set

10:12.980 --> 10:19.430
of Cartesian coordinates and then projecting the targets forward vector into that.

10:19.710 --> 10:21.860
OK, so it's actually quite easy to do.

10:21.980 --> 10:23.570
You get the transform.

10:23.570 --> 10:31.490
So this doctrine's form dot and it has its own method, transform vector and the vector that you want

10:31.490 --> 10:33.590
to transform is the target.

10:34.680 --> 10:37.230
Dot transform, dot forward.

10:38.220 --> 10:44.310
Like that, so you just putting them into the same space, same relative space, so that's your angle

10:44.520 --> 10:46.150
between their forward directions.

10:46.170 --> 10:53.190
Now, we also want to get the angle between the forward direction of the agent and the direction to

10:53.190 --> 10:53.890
the target.

10:53.940 --> 11:02.260
OK, so that'll be float to target equals vector three dot angle.

11:02.790 --> 11:05.850
And again, it's this dot transformed or forward.

11:06.360 --> 11:08.600
And again, we need to do it.

11:08.610 --> 11:12.540
This dot, transform, dot, transform vector.

11:12.960 --> 11:22.170
And the vector in this case is the target desire which we calculated just before.

11:22.500 --> 11:24.740
OK, so now we have both of these angles.

11:24.760 --> 11:27.060
We can put those in our IF statement as well.

11:27.070 --> 11:36.210
So let's put them in here and it's going to be to target and we'll see if it's greater than 90 and our

11:36.210 --> 11:46.770
relative hitting is less than 20 degrees and we will or that with the speed of the target.

11:47.250 --> 11:52.740
So these are our conditions in which we will actually perform a Sikh instead.

11:53.140 --> 12:02.160
OK, so let's save that and go back into Unity Pressplay and try it out.

12:08.080 --> 12:14.230
Now, because of the actual speeds and angles of these guys, it's really difficult to see whether he's

12:14.230 --> 12:18.820
seeking or whether, in fact, he is looking ahead.

12:21.260 --> 12:29.180
But he is just me on it, and it does make all the difference that you have these calculations correct

12:29.180 --> 12:33.860
when you move forward to other behaviors which actually are based on the pursue.
