WEBVTT

00:02.480 --> 00:09.560
In this section, we're going to look at autonomously moving agents and the steering mechanics that

00:09.560 --> 00:12.430
are used to move them around the map.

00:12.440 --> 00:18.830
Now, there's a whole bunch of them that we're going to look at one at a time and then put them together

00:18.830 --> 00:20.720
to get different behaviors.

00:21.020 --> 00:27.980
So first of all, what I'd like you to do is create a new unity project and bring in the Stata file

00:27.980 --> 00:29.450
for this lecture.

00:29.450 --> 00:36.740
When you open up the steering seen in it, which is this one here, you're going to find that you've

00:36.740 --> 00:42.950
got a ground plane and there's a tree and there's two little agents in there.

00:42.950 --> 00:50.960
Now, if you press play, you'll find that the policeman agent is going to be able to freely move around

00:50.960 --> 00:52.730
using the arrow key.

00:52.730 --> 01:02.330
So he is not a nav mesh agent, but we're going to make the rubber character into a nav mesh agent.

01:02.600 --> 01:06.740
And we're going to also later on make the tree into an obstacle.

01:06.770 --> 01:16.820
But first of all, we want the robber to be able to chase the policeman and also flee from the policeman.

01:16.820 --> 01:19.850
So that's going to be our objective in this lecture.

01:19.850 --> 01:24.980
Now, if we have a look at the policeman or the cop, he's called in a hierarchy.

01:24.980 --> 01:25.700
Just click on that.

01:25.700 --> 01:33.620
You'll see he's got a capsule collider on him and he has the drive script which will allow him to move

01:33.620 --> 01:34.490
around.

01:34.610 --> 01:38.030
Now, I've made a little adjustment to this drive script.

01:38.030 --> 01:40.940
Let's open that up and have a bit of a look.

01:41.090 --> 01:47.660
It's slightly different from the Stock Standard Drive script that you will find in the Unity documentation

01:47.660 --> 01:49.850
for get access in that.

01:49.850 --> 01:56.600
I've also added in a public float of current speed and then I'm recording into this public float of

01:56.600 --> 02:02.540
current speed, the translations that's down here that we're calculating because we are going to use

02:02.540 --> 02:03.740
that later on.

02:03.950 --> 02:04.250
All right.

02:04.250 --> 02:06.320
So that's the difference with that.

02:06.320 --> 02:10.850
Now, the robber himself needs to be made into a nav mesh agent.

02:10.850 --> 02:12.380
So select it.

02:12.380 --> 02:19.160
Go over to the inspector, click on add component and we want to go nav mesh agent.

02:19.160 --> 02:26.270
Now if we have a look at the rubber close up, you'll see that the nav mesh Agent Collider is offset

02:26.720 --> 02:28.220
above the character.

02:28.220 --> 02:29.660
You want to move that down.

02:29.660 --> 02:36.440
So we go over to the inspector and in the base offset just put in one and that will move it down unit

02:36.440 --> 02:39.560
of one, which will then fit him quite nicely.

02:39.710 --> 02:47.630
Now to see the nav mesh itself, the ground and the tree, if we just select both of those which are

02:47.630 --> 02:49.700
inside of this environment here.

02:49.820 --> 02:57.710
So we've got a tree there and the ground there over in the inspector you'll see that they are both ticked

02:57.710 --> 03:00.680
to navigation static.

03:01.340 --> 03:07.730
Let's just bring this in so you can see navigation static which means when we bake our nav mesh, if

03:07.730 --> 03:13.520
you're a member, then we're going to get the tree excluded.

03:13.520 --> 03:19.370
So it's going to become an obstacle because it's too steep for the agents to climb.

03:19.370 --> 03:25.280
Now, if you add any more trees to this, which we will be doing a little later on, we have to re bake

03:25.280 --> 03:28.610
each time to reconstruct our nav mesh.

03:28.940 --> 03:36.890
Okay, so we're now ready to start programming the robber to chase after the cop.

03:37.830 --> 03:42.060
So down in assets, right click create a C-sharp script.

03:42.060 --> 03:50.280
And I'm just going to call this bot just to make it simple drag and drop that over into the hierarchy,

03:50.280 --> 03:52.230
on to the rubber.

03:52.230 --> 03:59.760
So it has to be on the same level as the mesh agent because we're going to access it inside of the script.

04:00.060 --> 04:02.430
So now we open that up at the top.

04:02.430 --> 04:05.280
We're going to do what we've done before with getting nav mesh agents.

04:05.280 --> 04:07.680
But first of all, we need our AI library.

04:07.680 --> 04:18.270
So using unity, dot engine, dot AI and then down inside of our class, we're going to create a nerve

04:18.450 --> 04:21.360
mesh agent called agent.

04:22.350 --> 04:25.440
Then in the start we'll go agent.

04:26.530 --> 04:28.960
Equals this dot get component.

04:29.650 --> 04:37.150
And we want to get the nav mesh agent that's attached to this game object like that.

04:37.150 --> 04:41.020
Okay, so now we can write our seek method.

04:41.020 --> 04:48.430
So Seek is probably the easiest because it's basically just following something else around on the nav

04:48.430 --> 04:49.000
mesh.

04:49.480 --> 04:51.610
So we're not going to return anything from it.

04:51.610 --> 04:56.440
So it'll be void seek, we're going to give it a vector three, which is our position.

04:57.040 --> 05:00.880
So we'll call that here location pass through.

05:01.120 --> 05:10.390
And then all we do with our nav mesh agent for that is go agent dot set destination and the destination

05:10.390 --> 05:11.770
is the location.

05:11.770 --> 05:16.420
Now the location in this case is going to be the cop.

05:16.630 --> 05:21.280
So we need to get hold of the cop so that we can follow it around.

05:21.280 --> 05:28.720
So back up the top of our class, we're going to create a public game object for our target.

05:28.720 --> 05:30.250
We'll call it there.

05:30.640 --> 05:34.630
And then what we're going to do with our target is in the update.

05:34.630 --> 05:38.170
Send that through to seek so it'll be seek.

05:38.680 --> 05:47.590
And in here we're going to pass through target dot transform dot position because that will be our vector

05:47.590 --> 05:51.340
three value of the cop.

05:51.340 --> 06:00.280
Okay, so save that right now let's go back into unity and you want to go over to your robber and find

06:00.280 --> 06:00.970
the script.

06:00.970 --> 06:06.700
The script, it's going to have the target exposed and that's where we want to drag and drop the cop

06:07.000 --> 06:09.340
onto that target.

06:10.180 --> 06:21.100
So now press play and you will have a robber that is continually updating its position to follow the

06:21.100 --> 06:25.330
cop around or seek the location of the cop.

06:26.140 --> 06:27.310
Fairly straightforward.

06:27.310 --> 06:34.180
Now, when the robber actually gets to the cop, if we have a closer look, it's stopping on top of

06:34.180 --> 06:34.510
it.

06:35.080 --> 06:39.760
To prevent that, select the robber in the hierarchy over an inspector.

06:39.760 --> 06:44.830
You want to set its stopping distance and that's in the steering.

06:44.830 --> 06:50.680
If we've got a stopping distance and set that to say, let's put it at two, which means it's going

06:50.680 --> 06:53.170
to stop, but not right on top of the cop.

06:53.170 --> 07:00.760
So now just press play and you can watch him come over and they're going to stop face to face like that.

07:00.940 --> 07:04.480
And then he's not going to actually go on top of the cop.

07:05.570 --> 07:11.120
And even though the cop does have a collider on him, you don't really need it in order to stop him.

07:11.790 --> 07:13.340
Okay, so that's Sikh.

07:13.370 --> 07:15.490
That's fairly straightforward.

07:15.500 --> 07:23.960
So what Sikh is doing is it's calculating a vector from the robber to the cop.

07:25.250 --> 07:29.870
In the most simplest case, that is, if there's no obstacles in there, otherwise it's got to calculate

07:29.870 --> 07:31.010
a path around there.

07:31.010 --> 07:33.440
But let's just say we've got our vector going this way.

07:33.440 --> 07:40.490
So the vector is going to be the cop minus the robot, which will give us this vector here for the direction

07:40.490 --> 07:42.470
that the cop has to travel.

07:42.500 --> 07:47.810
Now, if we're going to flee, then we want to do the opposite.

07:47.810 --> 07:49.670
We want to go this way.

07:50.770 --> 07:52.980
But how do we figure out what this way is?

07:52.990 --> 07:59.290
Well, this way is just this vector multiplied by minus one, which is going to flip it around.

07:59.290 --> 08:02.050
So it's facing in that direction.

08:02.050 --> 08:09.750
But we can't give the set destination just a vector value.

08:09.760 --> 08:14.860
We need to give it an actual location where you want the robber to flee to.

08:15.070 --> 08:21.970
So instead of taking just this vector that we're going to calculate out to here, we're going to add

08:21.970 --> 08:29.230
it to the position of the rubber, which will actually give us like this point here, and that will

08:29.230 --> 08:33.610
make the rubber flee in the opposite direction over there.

08:34.160 --> 08:35.750
Okay, so let's put this into practice.

08:35.770 --> 08:39.990
Go back into your code and underneath seek.

08:40.000 --> 08:42.970
We're going to add in void flee.

08:43.690 --> 08:46.410
We're also going to flee to a vector.

08:46.420 --> 08:56.380
So vector three, we're going to give it the location that we're fleeing from, which again, is our

08:56.380 --> 08:57.340
target.

08:57.340 --> 09:01.390
But this time we're going to, first of all, go vector three.

09:01.510 --> 09:12.040
Flee vector equals location minus this dot transform dot position.

09:12.550 --> 09:19.000
And then with that vector, we're going to set the destination to agent set destination.

09:20.080 --> 09:28.510
And that will then be the location of the agent this dot transform position.

09:30.150 --> 09:33.750
Minus the flea vector.

09:35.980 --> 09:36.550
All right.

09:36.550 --> 09:39.040
So that's flee now down here in the update.

09:39.040 --> 09:43.570
Instead of seek, we will just change that to flee so that we can see that running.

09:44.260 --> 09:45.270
Save it.

09:45.280 --> 09:50.320
Let's go back into unity and we'll now press play.

09:51.070 --> 09:54.730
And you can see that he's now fled away.

09:54.760 --> 10:03.550
Now, just to see that a little bit more clearly, if we zoom out and let's make the cop try and go

10:03.550 --> 10:04.780
around.

10:05.790 --> 10:13.350
The back here somewhere so he can get the rubber to move and flee in a different direction.

10:13.800 --> 10:16.290
So now he's made him go like that.

10:16.290 --> 10:18.510
You can see he's going to keep fleeing.

10:24.590 --> 10:25.010
Like that.

10:25.010 --> 10:28.610
So as we try and get closer, he's just going to keep going.

10:28.610 --> 10:33.860
But the cop in this case is going much faster, so he will catch up.

10:34.220 --> 10:38.540
But that is the flee and the seek.

10:39.860 --> 10:43.340
In the next video, we'll come back and look at pursuit.
