Okay.
In this video, I'm going to give you a little bit of a challenge.
What I would like you to do is to keep the pursuit and the hiding mechanics like we've got them, but
include now a range.
And if the cop is outside of a distance of ten from the robber, then the robber should just wander
around.
So let me show you that in action.
Okay.
So he's now going to hide.
And you can see him over in this view over here.
He's just hidden around the back.
And here he comes around there.
And if I can just you can see he's going into Wanda.
Now, what I'm going to do is just try and get him to follow me out here and I'll stay close to him.
So here he comes, following.
I don't want to turn around and spook him.
Okay?
And now I'm just going to go really fast to get out of a distance of ten, at which point he's just
going to wander around and do his own thing.
So he just goes into a wander.
But then if I come back close in again like that, he's going to decide he needs to go and hide from
me, which he's just done.
There.
And then if I turn around when he comes back out to have a look.
No, he's just going to go off and wander because that distance is ten, obviously, around that area.
So see if you can pull that together.
Pause the video now.
And when we come back, I will show you how I implemented it.
All right.
How did you go with that?
Well, the first thing that you're going to need is to determine if the cop is out of range.
And for that, I'm going to create another method.
So it's going to return a bull bull target in range.
Well, we'll call it.
Now, you could actually pass that through a particular range if you wanted to set that up so you could
have a variable and use it for other conditions down in your update.
But we're not doing that.
All we're going to go is if vector three dot distance.
Between this dot transform dot position and the target dot transform dot position is less than ten.
Then we will return.
True.
Otherwise we will return.
False.
Okay.
So that's our target in range.
Now we just have to implement that down in our update.
So where are we going to put it?
Well, if the target is out of range, then it's really doesn't depend on whether you can see something
or whether the target can see you.
We're just having the simple out of range, so it really needs to be the first thing in your if statement.
So we're going to have, if not target in range, then what we can do in here is wander and I'm not
going to set a cooldown for that particular behavior because I want them to immediately stop wandering.
If the agent gets into range and they can go and then hide and then do all these other things, now
we need to put an else down here.
So we're checking if we're out of range.
Then we want the wandering to happen pretty much no matter what, which is why it is up at the top.
And because we've lost it with these other things, they're not going to try and overwrite it if it's
already happening or if this is a true condition.
And that's pretty much it.
So very simple.
We've only added like one, two, three, four, five, six, seven, eight, nine, about ten lines
in here just to get that done.
And the majority of it was just in our condition that we put up here.
So I'm just going to save that, go back to Unity and we can press play and try that out with our little
agent, which we're looking at to start with.
And so if we now go out here, then he's just going to go off and wander around.
And if we come back into range, he's going to go on hide, which you can't see because he's over here
now.
So we've got that complex behavior and now you might like to actually duplicate your robot and create
a few versions of it so you can actually see what it's like if you've got lots of agents.
So we're just going to select him and control D to make a copy, hit the W key to make sure you can
just move them out.
I don't know how many we want.
As many as you like.
Really?
Okay.
And now let's press play.
And they're all going to go and hide.
What?
Actually, the ones at the far end have gone and wandered.
Some of them are hiding.
So if we take the cop over to these guys, we can actually make them hide from him.
They go and hide behind that tree.
See if I can force them to all come around the other side and hide from him.
So he comes a heap.
They're hiding the cops behind the house.
And he obviously has no colliders.
He can go through the house.
So.
Let's just turn this around, because most of the action's all happening over in that back area over
there.
And just put the cop where these guys are going to go.
Hide behind that tree over there.
And if I turn around, all those guys will go back and hide over there behind the house on the other
side.
And then others are just going to wander.
And we have some nice, complex behavior.
Now, if I get my cop and bring him back around the front and.
Scared him totally out of range of everybody, then they should just all go wandering off on their own.
As you can see, they're doing their happily wandering around and if they ever wander over near the
cop that they are like that they discover that he's suddenly exist.
Maybe if we go like this, then they'll start to sneak up on him and just put him over here.
So when they wander within range, which of course they won't, because I'm wanting them to right now.
Here we go.
So a few of them have now snuck up behind him.
And here come a few others sneaking up over here.
But if I turn around, I'm going to force them all to run away and go and hide from me.
Because I've noticed them.
I like how they all had to have a look around the corner there to see if he was still there.
Okay.
So that's another complex behavior that we've now added in there to our sneaky sort of pursue.
We've got wandering and you can put a vade in there if you want to put that instead of hiding, some
could evade or some of the time you can go and hide.
Or if you're not close enough to a particular hiding position, which is something else you could program
in, you could then go into the evade mode.
So if you can't find anywhere to hide, just start to evade many possibilities now that you have that
simple set of tools for these agents.