WEBVTT

00:01.500 --> 00:07.320
The pursuit behavior is a little like Sikh, except instead of following a target, the agent predicts

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

00:11.250 --> 00:15.670
We see this occurring all the time in sports games in the case of football.

00:15.690 --> 00:21.420
The actual ball provides players with the predicted future position of their opponent.

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

00:27.960 --> 00:31.980
the target's position forward direction of travel and speed.

00:32.010 --> 00:38.490
We also need the pursuer speed and his distance to the target as they are going to depend on how quickly

00:38.490 --> 00:40.170
the robber can catch up.

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

00:46.530 --> 00:53.370
ahead of the target the pursuer should aim as the distance between the pursuer and target divided by

00:53.370 --> 00:56.550
the addition of the speeds of both agents.

00:56.850 --> 00:59.910
So let's go ahead and implement this in our bot script.

00:59.910 --> 01:06.720
So open up your bot code and we're going to put pursue under FLI just down here.

01:07.350 --> 01:11.010
So pursue will have its own method obviously.

01:11.010 --> 01:13.800
So avoid pursue.

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

01:23.730 --> 01:28.700
So vector three, target di.

01:28.740 --> 01:41.580
Ah, so the direction is target dot transform dot position minus this dot transform dot position.

01:42.980 --> 01:48.140
Now, once we've got that, we can actually go ahead and calculate the look ahead.

01:48.230 --> 01:54.020
So how far are we going to look ahead past the forward facing direction of the target?

01:54.020 --> 01:56.000
So look ahead.

01:56.270 --> 01:59.840
And this is where we're predicting the future location.

02:00.350 --> 02:05.330
So it'll be target di r dot magnitude.

02:06.440 --> 02:09.050
And we're going to divide that by the agent speed.

02:09.050 --> 02:19.010
So the agent speed plus the speed of the drive that's going on on the cup.

02:19.490 --> 02:28.190
To do that it will be the targets dot get component for the drive script.

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

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

02:41.300 --> 02:42.820
back in the drive script.

02:42.830 --> 02:45.860
Okay, so that's getting the look ahead.

02:45.860 --> 02:48.650
And then what we're going to do to that is actually seek.

02:48.650 --> 02:51.710
So we will seek the target.

02:52.850 --> 02:56.960
Dot transform dot position.

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

03:06.370 --> 03:07.210
vector.

03:07.780 --> 03:15.460
So the forward vector is the way that the target is facing and travelling form dot forward.

03:15.850 --> 03:19.780
And it has a length of one, so it's automatically normalized for us.

03:19.780 --> 03:25.360
So we multiplying that by look ahead gives us a vector that is the length of look ahead and we're therefore

03:25.360 --> 03:28.330
adding that on to the seek.

03:28.510 --> 03:30.490
Okay, so we've got that now.

03:30.490 --> 03:37.090
All we need to do with it is come down into update and just change this to pursue.

03:38.810 --> 03:39.890
So that we can run it.

03:40.020 --> 03:47.090
Okay, so let's save that and we'll go back into unity and let's press play.

03:47.450 --> 03:47.840
Okay.

03:47.840 --> 03:51.690
So you can see he's coming over to the cop.

03:51.720 --> 03:53.870
We move the cop.

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

04:05.730 --> 04:07.440
Then just blindly.

04:09.040 --> 04:09.700
Following.

04:09.700 --> 04:13.630
I think you can just kind of see it how far he's looking ahead.

04:14.850 --> 04:15.720
In that case.

04:15.720 --> 04:19.380
Now, the faster they're going, the more looking ahead that will happen.

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

04:26.100 --> 04:27.060
see it there.

04:27.780 --> 04:33.210
Let's just go back into our code and we can multiply this out further.

04:33.210 --> 04:35.880
So it's actually looking further than look ahead.

04:36.120 --> 04:41.520
Let's multiply that by five just to give it a really good, strong look ahead, I guess.

04:41.790 --> 04:43.680
Let's go back and press play.

04:43.710 --> 04:47.000
Now what you'll find is he will look ahead.

04:47.010 --> 04:52.110
Now, my bot's not even going at this point, but once you start adding.

04:53.420 --> 04:54.860
A speed.

04:55.770 --> 04:56.550
To look ahead.

04:56.550 --> 04:57.660
Let's get going.

04:58.940 --> 05:00.380
Turn around.

05:01.690 --> 05:02.710
And there you go.

05:02.710 --> 05:07.730
So then now the calculation for the look ahead has become quite large.

05:07.750 --> 05:11.110
So it's it's the speed that it was going in this direction.

05:11.110 --> 05:16.420
And the bots ended up over there somewhere right on the edge of the map.

05:16.840 --> 05:21.850
However, if this is happening, which it shouldn't, because you're not really meant to multiply that

05:21.850 --> 05:30.040
look ahead, you could fix it by putting in another condition to test if your cop, your target has

05:30.040 --> 05:34.740
stopped moving, in which case you would then go into a seek instead.

05:34.750 --> 05:39.850
So that's sort of one of the extra conditions that you could add into this pursue.

05:40.180 --> 05:48.520
And what we'll also do is improve this by adding some other conditions that deal with whether you should

05:48.520 --> 05:52.240
be seeking or whether you should actually be looking ahead.

05:52.980 --> 05:53.430
First.

05:53.430 --> 05:59.070
Of course, one condition we will consider is that if the target has stopped moving, then we will turn

05:59.070 --> 06:03.060
the pursuit into a seek and just head straight towards the target.

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

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

06:15.150 --> 06:18.690
So how do we determine if the robber is in front of the cop?

06:18.720 --> 06:25.170
Well, we use angles and we use the angles between the various vectors that we've already been using.

06:25.320 --> 06:30.990
When the robber is in front of the cop, the angle between the robbers forward vector and the vector,

06:30.990 --> 06:37.720
the cop will be greater than 90 degrees when the robber is behind the cop.

06:37.740 --> 06:41.430
The angle is going to be less than 90 degrees.

06:42.310 --> 06:49.720
Now, if the robber is in front of the cop, he might already have turned around to go towards the interception

06:49.720 --> 06:50.380
point.

06:50.740 --> 06:54.340
If this is the case, we don't want him to seek.

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

07:02.050 --> 07:04.960
both of the forward directions of the agents.

07:04.960 --> 07:11.260
And if this is quite small, then we will allow the pursuit to turn into a seek.

07:12.380 --> 07:18.060
So let's go ahead and add in these conditions into the pursue method.

07:18.080 --> 07:27.080
So the first one we have is if the target has stopped moving, so we can test that if the speed which

07:27.080 --> 07:29.360
is this here, let me just copy that.

07:31.020 --> 07:43.380
If the speed is less than 0.01 F and I'm saying that rather than saying equals zero, because sometimes

07:43.380 --> 07:48.630
you can get a floating error with zero values and it might not actually be equal to zero.

07:48.660 --> 07:51.030
It might be a very, very small number.

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

07:58.260 --> 08:01.860
then what we're going to do down in here is seek.

08:01.860 --> 08:05.220
So we're just going to seek the target.

08:05.760 --> 08:09.780
So the target dot transform position.

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

08:17.500 --> 08:23.200
Now, the other things that we want to test for is the relative heading of the agent.

08:23.200 --> 08:33.040
So if we have a small, small angle between the forward directions of the agents, then we're just going

08:33.040 --> 08:39.100
to get our agent to follow or seek the target instead of looking ahead.

08:39.100 --> 08:42.400
So we need to calculate that particular angle.

08:42.400 --> 08:50.710
So let's go float relative heading, we'll call it equals vector three dot angle.

08:50.920 --> 08:59.680
And that angle is going to be between this dot transformed forward and we want to get the forward vector

08:59.680 --> 09:02.170
of the cop or the target.

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

09:10.960 --> 09:19.240
that vector, which is the forward direction of the target into the space of the agent.

09:19.330 --> 09:27.610
What that essentially means is you're kind of making that agent the origin point of its own little set

09:27.610 --> 09:34.210
of Cartesian coordinates and then projecting the target's forward vector into that.

09:34.510 --> 09:36.580
So it's actually quite easy to do.

09:36.610 --> 09:38.170
You get the transform.

09:38.170 --> 09:44.320
So this dot trends form dot and it has its own method transform vector.

09:44.470 --> 09:48.400
And the vector that you want to transform is the target.

09:49.260 --> 09:53.400
Dot transform dot forward like that.

09:53.400 --> 09:57.330
So you just putting them into the same space, same relative space.

09:57.330 --> 10:00.750
So that's your angle between there forward directions.

10:00.750 --> 10:07.920
Now we also want to get the angle between the forward direction of the agent and the direction to the

10:07.920 --> 10:08.550
target.

10:08.850 --> 10:17.070
So that will be float to target equals vector three dot angle.

10:17.340 --> 10:20.640
And again, it's this dot transformed forward.

10:20.820 --> 10:27.300
And again, we need to do a this dot transform dot transform vector.

10:27.420 --> 10:36.990
And the vector in this case is the target deer, which we calculated just before.

10:37.110 --> 10:37.300
Okay.

10:37.320 --> 10:41.670
So now we have both of these angles, we can put those in our ith statement as well.

10:41.670 --> 10:50.880
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

10:50.880 --> 11:01.560
relative heading is less than 20 degrees and we will or that with the speed of the target.

11:01.710 --> 11:07.560
So these are our conditions in which we will actually perform a seek instead.

11:07.620 --> 11:16.950
Okay, so let's save that and we'll go back into Unity Press play and try it out.

11:22.560 --> 11:28.890
Now because of the actual speeds and angles of these guys, it's really difficult to see whether he's

11:28.890 --> 11:33.630
seeking or whether in fact, he is looking ahead.

11:35.810 --> 11:38.570
But he is, trust me on it.

11:39.020 --> 11:44.990
And it does make all the difference that you have these calculations correct when you move forward to

11:44.990 --> 11:48.650
other behaviors which actually are based on the pursue.
