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 scene in it, which is this one here, you're going to find that you've
got a ground plan 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.
So he is not a nerve agent, but we're going to make the rubber character into a nerve agent and we're
going to also later on make the tree into an obstacle.
But first of all, we want the rubber 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 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 quarter on him and he has the drive script, which will allow him to move around.
Now, it 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 dry 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 rubber himself needs to be made into a nerve agent.
So selected, go over to the inspector, click on ADD component, and we want to go NAV Misch agent.
Now, if we have a look at the robot close up, you'll see that the nerve 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 that 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 break 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 remake
each time to reconstruct our nav mesh.
OK, so we're now ready to start programming the robot to chase after the cop.
So down in assets, us 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 nerve agent because we're going to access it inside of the
script.
OK, so now we open that up at the top.
We're going to do what we've done before with getting nerve agents.
But first of all, we need our AI library.
So using unity, dot engine dot AI and then down inside of a class, we're going to create a nerve agent
called Agent.
Then in the start, we'll go agent.
Equals this doget component, and we want to get the nerve agent that's attached to this game object
like that.
OK, so now we can write our SIKE method.
So Sike is probably the easiest because it's basically just following something else around on the nav
mesh.
OK, so we are not going to return anything from it.
So it'll be void seke.
We're going to give it a vector three, which is our position.
OK, so we'll call that here.
Location pass through and then all we do with our nerve 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 we're going to do with our target is in the update.
Send that through to seek so it'll be sick.
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.
OK, so save that right now.
Let's go back into unity and you want to go over to your roba 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.
OK, so now Pressplay and you will have a robot that is continually updating its position.
To follow the Corporan 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 wheel 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 lot on him, you don't really need it in order to stop him.
OK, so that seat, that's fairly straightforward.
So what Sike 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 weighs?
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 robot 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 robot, which will actually give us like this point here, and that will make
the robot flee in the opposite direction over there.
OK, so let's put this into practice.
Go back into your code and underneath Seke, we're going to add in void fli 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 fli victor equals location minus this transform
to position.
And then with that, Victor, we're going to set the destination to agent, not destination.
And that will then be the location of the agent, this transform position.
Minus the flea victor.
All right, so that's Fli now down here in the update, instead of Seke, 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 Pressplay.
And you can see that he's now flayed away now, just to see that a little bit more clearly, if we zoom
out and let's make the cup.
Try and go around.
The back here somewhere, so you can get the robot to move and flee in a different direction.
OK, so now he's made him go like that.
You can see he's going to keep fleeing.
Unfortunately, off the edge of our map, but you get the idea, so if we grab hold of that robber and
let's just put him over here and then press play and just go around this way, we can then manipulate
that he's going to flee from us.
I 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 flea and the sick.