The pursuit behavior is a little like seek, except instead of following a target, the agent predicts
where the target will be in the future in order to intercept it.
We say this occurring all the time in sports games.
In the case of football, the actual ball provides players with the predicted future position of their
opponent.
Now, in order to determine the location our pursuer the robber should steer towards, we need to get
the target's position for direction of travel and its speed.
We also need the pursuit of speed and his distance to the target as they are going to depend on how
quickly the robber can catch up.
We can then predict where the pursuer should head towards based on these values and calculate how far
ahead of the target the pursuers should aim as the distance between the pursuer and target divided by
the addition of the speeds of both agents.
So let's go ahead and implement this in our bot script.
So open up your BOT code and we're going to put pursue and flee.
Just stand here.
OK, so pursue will have its own method obviously.
So avoid pursuit.
And then inside of here, what we're going to do is first of all, work out the vector to the target.
So vector three.
Target, they are so the direction is target, dot, transform, dot position minus this dot transform
the position.
Now, once we've got that, we can actually go ahead and calculate the look ahead as to how far we're
going to look ahead past the forward facing direction of the target.
So look ahead.
And this is where we're predicting the future location.
OK, so it'll be target dot magnitude and we're going to divide that by the agents speed.
So the agent speed plus the speed of the drive that's going on on the cop to do that, it will be the
targets dot get component for the drive script.
And remember, we created that current speed or I did and put it in there and I showed it to you earlier
where we're updating the actual speed and we made it into a public value so that you could access it
back in the drive script.
OK, so that's getting the look ahead.
And then what we're going to do to that is actually seek.
So we will seek the target.
Dot transform dot position.
Plus, the look ahead, but the look ahead we're going to use as a multiplier against the targets forward
Victor.
OK, so the forward victor is the way that the target is facing and traveling form forward, and it
has a length of one.
So it's automatically normalized for us.
So we multiply that by look ahead, gives us a vector that is the length of look ahead and we're therefore
adding that on to the seat.
OK, so we've got that now or we need to do with it is come down into update and just change this to
pursue.
So that we can run it.
OK, so let's save that and we'll go back in to Unity and let's press play.
OK, so you can see he's coming over to the cop.
Remove the cop.
And if we change directions, you'll see that he will then be predicting the future direction, rather.
Then just blindly.
Following, I think you can just kind of see it how far he's looking ahead.
In that case, now, the faster they're going, the more looking ahead that will happen.
Now, you might have also noticed is that when the robber reaches the cop, he lands right on top of
the cop, which is not really what you want.
You want it to stop a little further away.
Now, that's actually a dynamic of the nerve agent.
And you can fix that if you select the robber and in the inspector, you go and modify the stopping
distance.
So I've currently got one.
If I set that to two and then we press play, he will stop within a distance of two of the goal location.
Now, if you want to force that looking ahead a bit more than it actually is, that you can't really
see it there.
Let's just go back into our code and we can multiply this out further.
So it's actually looking further than lookahead let's multiply that by five just to give it a really
good, strong look ahead, I guess.
Let's go back and press play.
Now, what you'll find is he will look ahead.
Now, my boss not even going at this point, but once you start adding.
A speed.
The look ahead, let's get going.
Turn around.
And there you go, so then now the calculation for the look ahead has become quite large.
So it's the speed that it was going in this direction and the bot's ended up over there somewhere right
on the edge of the map.
However, if this is happening, which it shouldn't, because you're not really meant to multiply that
look ahead, you could fix it by putting in another condition to test if your cop, your target has
stopped moving, in which case you would then go into a Sikh instead.
So that's sort of one of the extra conditions that you could add into this pursue.
And what we will also do is improve this by adding some other conditions that deal with whether you
should be seeking or whether you should actually be looking ahead.
First, of course, one condition we will consider is that if the target has stopped moving, then we
will turn the pursuit into a seat and just head straight towards the target.
The next condition we are going to consider is that if the robber is in front of the cop and pretty
much already heading in the same direction, then the robber should turn around and just seek the cop.
So how do we determine if the robber is in front of the cop?
Well, we use angles and we use the angles between the various vectors that we've already been using.
When the robber is in front of the cop, the angle between the robbers forward vector and the vector,
the cop will be greater than 90 degrees.
When the robber is behind the cop, the angle is going to be less than 90 degrees.
Now, if the robber is in front of the cop, he might already have turned around to go towards the interception
point.
If this is the case, we don't want him to seek.
Instead, we have to restrict our conditions for seeking even more by considering the angle between
both of the four directions of the agents.
And if this is quite small, then we will allow the pursuit to turn into a Sikh.
So let's go ahead and add in these conditions into the Persue method, so the first one we have is if
the target has stopped moving so we can test that if the speed, which is this here.
Let me just copy that.
If the speed is less than zero point zero one F, and I'm saying that rather than saying equals zero,
because sometimes you can get a floating arrow with zero values and it might not actually be equal to
zero.
It might be a very, very small number.
So by doing this, you kind of covering yourself that if we're pretty much standing still our target,
then what we're going to do down in here is seek.
So we're just going to seek the target.
So the target dot transform position.
And then we're also going to return rather than continuing in this method.
Now the other things that we want to test for is the relative heading of the agent.
So if we have a small, small angle between the forward directions of the agents, then we're just going
to get our agent to follow or seek the target instead of looking ahead.
So we need to calculate that particular angle.
So let's go float relative heading or call it equals vector three dot angle.
And that angle is going to be between this dot transformed forward and we want to get the forward vector
of the cop or the target.
Now we have to get the forward vector relative to the actual agent, which means we need to translate
that vector, which is the forward direction of the target into the space of the agent.
What that essentially means is you're kind of making that agent the origin point of its own little set
of Cartesian coordinates and then projecting the targets forward vector into that.
OK, so it's actually quite easy to do.
You get the transform.
So this doctrine's form dot and it has its own method, transform vector and the vector that you want
to transform is the target.
Dot transform, dot forward.
Like that, so you just putting them into the same space, same relative space, so that's your angle
between their forward directions.
Now, we also want to get the angle between the forward direction of the agent and the direction to
the target.
OK, so that'll be float to target equals vector three dot angle.
And again, it's this dot transformed or forward.
And again, we need to do it.
This dot, transform, dot, transform vector.
And the vector in this case is the target desire which we calculated just before.
OK, so now we have both of these angles.
We can put those in our IF statement as well.
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
relative hitting is less than 20 degrees and we will or that with the speed of the target.
So these are our conditions in which we will actually perform a Sikh instead.
OK, so let's save that and go back into Unity Pressplay and try it out.
Now, because of the actual speeds and angles of these guys, it's really difficult to see whether he's
seeking or whether, in fact, he is looking ahead.
But he is just me on it, and it does make all the difference that you have these calculations correct
when you move forward to other behaviors which actually are based on the pursue.