In this section, we're going to look at autonomously moving agents and the steering mechanics that
are used to move them around the map.
Now, there's a whole bunch of them that we're going to look at one at a time and then put them together
to get different behaviors.
So first of all, what I'd like you to do is create a new unity project and bring in the Stata file
for this lecture.
When you open up the steering seen in it, which is this one here, you're going to find that you've
got a ground plane and there's a tree and there's two little agents in there.
Now, if you press play, you'll find that the policeman agent is going to be able to freely move around
using the arrow key.
So he is not a nav mesh agent, but we're going to make the rubber character into a nav mesh agent.
And we're going to also later on make the tree into an obstacle.
But first of all, we want the robber to be able to chase the policeman and also flee from the policeman.
So that's going to be our objective in this lecture.
Now, if we have a look at the policeman or the cop, he's called in a hierarchy.
Just click on that.
You'll see he's got a capsule collider on him and he has the drive script which will allow him to move
around.
Now, I've made a little adjustment to this drive script.
Let's open that up and have a bit of a look.
It's slightly different from the Stock Standard Drive script that you will find in the Unity documentation
for get access in that.
I've also added in a public float of current speed and then I'm recording into this public float of
current speed, the translations that's down here that we're calculating because we are going to use
that later on.
All right.
So that's the difference with that.
Now, the robber himself needs to be made into a nav mesh agent.
So select it.
Go over to the inspector, click on add component and we want to go nav mesh agent.
Now if we have a look at the rubber close up, you'll see that the nav mesh Agent Collider is offset
above the character.
You want to move that down.
So we go over to the inspector and in the base offset just put in one and that will move it down unit
of one, which will then fit him quite nicely.
Now to see the nav mesh itself, the ground and the tree, if we just select both of those which are
inside of this environment here.
So we've got a tree there and the ground there over in the inspector you'll see that they are both ticked
to navigation static.
Let's just bring this in so you can see navigation static which means when we bake our nav mesh, if
you're a member, then we're going to get the tree excluded.
So it's going to become an obstacle because it's too steep for the agents to climb.
Now, if you add any more trees to this, which we will be doing a little later on, we have to re bake
each time to reconstruct our nav mesh.
Okay, so we're now ready to start programming the robber to chase after the cop.
So down in assets, right click create a C-sharp script.
And I'm just going to call this bot just to make it simple drag and drop that over into the hierarchy,
on to the rubber.
So it has to be on the same level as the mesh agent because we're going to access it inside of the script.
So now we open that up at the top.
We're going to do what we've done before with getting nav mesh agents.
But first of all, we need our AI library.
So using unity, dot engine, dot AI and then down inside of our class, we're going to create a nerve
mesh agent called agent.
Then in the start we'll go agent.
Equals this dot get component.
And we want to get the nav mesh agent that's attached to this game object like that.
Okay, so now we can write our seek method.
So Seek is probably the easiest because it's basically just following something else around on the nav
mesh.
So we're not going to return anything from it.
So it'll be void seek, we're going to give it a vector three, which is our position.
So we'll call that here location pass through.
And then all we do with our nav mesh agent for that is go agent dot set destination and the destination
is the location.
Now the location in this case is going to be the cop.
So we need to get hold of the cop so that we can follow it around.
So back up the top of our class, we're going to create a public game object for our target.
We'll call it there.
And then what we're going to do with our target is in the update.
Send that through to seek so it'll be seek.
And in here we're going to pass through target dot transform dot position because that will be our vector
three value of the cop.
Okay, so save that right now let's go back into unity and you want to go over to your robber and find
the script.
The script, it's going to have the target exposed and that's where we want to drag and drop the cop
onto that target.
So now press play and you will have a robber that is continually updating its position to follow the
cop around or seek the location of the cop.
Fairly straightforward.
Now, when the robber actually gets to the cop, if we have a closer look, it's stopping on top of
it.
To prevent that, select the robber in the hierarchy over an inspector.
You want to set its stopping distance and that's in the steering.
If we've got a stopping distance and set that to say, let's put it at two, which means it's going
to stop, but not right on top of the cop.
So now just press play and you can watch him come over and they're going to stop face to face like that.
And then he's not going to actually go on top of the cop.
And even though the cop does have a collider on him, you don't really need it in order to stop him.
Okay, so that's Sikh.
That's fairly straightforward.
So what Sikh is doing is it's calculating a vector from the robber to the cop.
In the most simplest case, that is, if there's no obstacles in there, otherwise it's got to calculate
a path around there.
But let's just say we've got our vector going this way.
So the vector is going to be the cop minus the robot, which will give us this vector here for the direction
that the cop has to travel.
Now, if we're going to flee, then we want to do the opposite.
We want to go this way.
But how do we figure out what this way is?
Well, this way is just this vector multiplied by minus one, which is going to flip it around.
So it's facing in that direction.
But we can't give the set destination just a vector value.
We need to give it an actual location where you want the robber to flee to.
So instead of taking just this vector that we're going to calculate out to here, we're going to add
it to the position of the rubber, which will actually give us like this point here, and that will
make the rubber flee in the opposite direction over there.
Okay, so let's put this into practice.
Go back into your code and underneath seek.
We're going to add in void flee.
We're also going to flee to a vector.
So vector three, we're going to give it the location that we're fleeing from, which again, is our
target.
But this time we're going to, first of all, go vector three.
Flee vector equals location minus this dot transform dot position.
And then with that vector, we're going to set the destination to agent set destination.
And that will then be the location of the agent this dot transform position.
Minus the flea vector.
All right.
So that's flee now down here in the update.
Instead of seek, we will just change that to flee so that we can see that running.
Save it.
Let's go back into unity and we'll now press play.
And you can see that he's now fled away.
Now, just to see that a little bit more clearly, if we zoom out and let's make the cop try and go
around.
The back here somewhere so he can get the rubber to move and flee in a different direction.
So now he's made him go like that.
You can see he's going to keep fleeing.
Like that.
So as we try and get closer, he's just going to keep going.
But the cop in this case is going much faster, so he will catch up.
But that is the flee and the seek.
In the next video, we'll come back and look at pursuit.