WEBVTT

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

00:20.200 --> 00:22.930
are used to move them around the map.

00:23.140 --> 00:29.440
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:29.440 --> 00:31.150
to get different behaviors.

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

00:38.590 --> 00:39.870
for this lecture.

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

00:47.380 --> 00:53.560
got a ground plan and there's a tree and there's two little agents in there.

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

01:01.570 --> 01:03.040
using the arrow.

01:03.400 --> 01:14.050
So he is not a nerve agent, but we're going to make the rubber character into a nerve agent and we're

01:14.050 --> 01:17.150
going to also later on make the tree into an obstacle.

01:17.560 --> 01:27.450
But first of all, we want the rubber to be able to chase the policeman and also flee from the policeman.

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

01:30.580 --> 01:33.760
Now we have a look at the policeman or the cop.

01:34.120 --> 01:35.580
He's called in a hierarchy.

01:35.590 --> 01:36.340
Just click on that.

01:36.340 --> 01:44.890
You'll see he's got a quarter on him and he has the drive script, which will allow him to move around.

01:45.370 --> 01:48.460
Now, it made a little adjustment to this drive script.

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

01:51.770 --> 01:58.300
It's slightly different from the stock standard dry script that you will find in the unity documentation

01:58.300 --> 02:04.060
for get access in that I've also added in a public float of current speed.

02:04.060 --> 02:11.170
And then I'm recording into this public float of current speed, the translations that's down here that

02:11.170 --> 02:14.170
we're calculating because we are going to use that later on.

02:14.620 --> 02:14.880
All right.

02:14.890 --> 02:16.800
So that's the difference with that.

02:17.050 --> 02:21.460
Now, the rubber himself needs to be made into a nerve agent.

02:21.460 --> 02:29.570
So selected, go over to the inspector, click on ADD component, and we want to go NAV Misch agent.

02:29.980 --> 02:37.930
Now, if we have a look at the robot close up, you'll see that the nerve agent collider is offset above

02:37.930 --> 02:38.710
the character.

02:38.950 --> 02:40.270
You want to move that down.

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

02:47.050 --> 02:50.010
of one, which will then fit him quite nicely.

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

02:58.270 --> 03:00.120
inside of this environment here.

03:00.430 --> 03:08.350
So that tree there and the ground there over in the inspector, you'll see that they are both ticked

03:08.350 --> 03:11.110
to navigation static.

03:11.890 --> 03:18.190
Let's just bring this in so you can see navigation static, which means when we break our nav mesh,

03:18.190 --> 03:24.130
if you're a member, then we're going to get the tree excluded.

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

03:30.190 --> 03:35.920
Now, if you add any more trees to this, which we will be doing a little later on, we have to remake

03:35.920 --> 03:39.050
each time to reconstruct our nav mesh.

03:39.610 --> 03:47.320
OK, so we're now ready to start programming the robot to chase after the cop.

03:48.570 --> 03:55.290
So down in assets, us right click create a C sharp script, and I'm just going to call this bot just

03:55.290 --> 04:02.880
to make it simple, drag and drop that over into the hierarchy on to the rubber.

04:02.890 --> 04:09.750
So it has to be on the same level as the nerve agent because we're going to access it inside of the

04:09.750 --> 04:10.230
script.

04:10.440 --> 04:13.080
OK, so now we open that up at the top.

04:13.080 --> 04:15.920
We're going to do what we've done before with getting nerve agents.

04:15.930 --> 04:18.300
But first of all, we need our AI library.

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

04:30.540 --> 04:31.740
called Agent.

04:33.100 --> 04:35.830
Then in the start, we'll go agent.

04:37.090 --> 04:46.240
Equals this doget component, and we want to get the nerve agent that's attached to this game object

04:46.900 --> 04:47.560
like that.

04:47.860 --> 04:51.670
OK, so now we can write our SIKE method.

04:51.700 --> 04:59.050
So Sike is probably the easiest because it's basically just following something else around on the nav

04:59.050 --> 04:59.410
mesh.

04:59.860 --> 05:02.230
OK, so we are not going to return anything from it.

05:02.230 --> 05:04.090
So it'll be void seke.

05:04.090 --> 05:06.870
We're going to give it a vector three, which is our position.

05:07.450 --> 05:08.680
OK, so we'll call that here.

05:08.680 --> 05:18.790
Location pass through and then all we do with our nerve agent for that is go agent dot set destination.

05:19.790 --> 05:27.980
And the destination is the location now, the location in this case is going to be the cop, so we need

05:27.980 --> 05:31.880
to get hold of the cop so that we can follow it around.

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

05:39.360 --> 05:40.730
We'll call it there.

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

05:45.290 --> 05:48.570
Send that through to seek so it'll be sick.

05:49.430 --> 05:57.770
And in here we're going to pass through Target, dot, transform dot position because that will be our

05:57.770 --> 06:01.910
vector three value of the cop.

06:01.940 --> 06:04.730
OK, so save that right now.

06:04.730 --> 06:12.530
Let's go back into unity and you want to go over to your roba and find the script, the script.

06:12.800 --> 06:19.760
It's going to have the target exposed and that's where we want to drag and drop the cop onto that target.

06:20.540 --> 06:29.900
OK, so now Pressplay and you will have a robot that is continually updating its position.

06:30.890 --> 06:35.810
To follow the Corporan or seek the location of the cop.

06:36.780 --> 06:43.500
Fairly straightforward now when the robber actually gets to the cop, if we have a closer look, it's

06:43.500 --> 06:50.390
stopping on top of it to prevent that select the robber in the hierarchy over an inspector.

06:50.400 --> 06:57.570
You want to set its stopping distance and that's in the steering wheel stopping distance and set that

06:57.570 --> 07:03.790
to say, let's put it at two, which means it's going to stop, but not right on top of the cop.

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

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

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

07:22.430 --> 07:25.950
OK, so that seat, that's fairly straightforward.

07:26.270 --> 07:34.370
So what Sike is doing is it's calculating a vector from the robber to the cop.

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

07:40.490 --> 07:41.640
a path around there.

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

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

07:51.170 --> 07:52.860
that the cop has to travel.

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

07:58.580 --> 08:00.080
We want to go this way.

08:01.490 --> 08:03.610
But how do we figure out what this weighs?

08:03.620 --> 08:09.890
Well, this way is just this vector multiplied by minus one, which is going to flip it around.

08:09.890 --> 08:12.440
So it's facing in that direction.

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

08:20.390 --> 08:25.280
We need to give it an actual location where you want the robot to flee to.

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

08:32.580 --> 08:40.160
it to the position of the robot, which will actually give us like this point here, and that will make

08:40.160 --> 08:44.010
the robot flee in the opposite direction over there.

08:44.780 --> 08:46.400
OK, so let's put this into practice.

08:46.400 --> 08:55.430
Go back into your code and underneath Seke, we're going to add in void fli we're also going to flee

08:55.430 --> 08:56.630
to a vector.

08:56.930 --> 08:58.970
So vector.

08:59.990 --> 09:00.650
Three.

09:01.680 --> 09:08.460
We're going to give it the location that we're fleeing from, which, again, is our target, but this

09:08.460 --> 09:20.590
time we're going to, first of all, go vector three fli victor equals location minus this transform

09:21.090 --> 09:22.260
to position.

09:23.030 --> 09:29.180
And then with that, Victor, we're going to set the destination to agent, not destination.

09:30.580 --> 09:38.680
And that will then be the location of the agent, this transform position.

09:40.640 --> 09:43.970
Minus the flea victor.

09:46.350 --> 09:51.840
All right, so that's Fli now down here in the update, instead of Seke, we will just change that to

09:51.840 --> 09:53.790
flee so that we can see that running.

09:54.750 --> 10:00.510
Save it, let's go back into unity and we'll now Pressplay.

10:01.560 --> 10:08.310
And you can see that he's now flayed away now, just to see that a little bit more clearly, if we zoom

10:08.310 --> 10:11.940
out and let's make the cup.

10:13.300 --> 10:14.860
Try and go around.

10:16.170 --> 10:23.460
The back here somewhere, so you can get the robot to move and flee in a different direction.

10:23.850 --> 10:26.700
OK, so now he's made him go like that.

10:26.730 --> 10:28.680
You can see he's going to keep fleeing.

10:31.160 --> 10:38.120
Unfortunately, off the edge of our map, but you get the idea, so if we grab hold of that robber and

10:38.120 --> 10:46.400
let's just put him over here and then press play and just go around this way, we can then manipulate

10:46.400 --> 10:49.190
that he's going to flee from us.

10:54.310 --> 11:00.150
I like that, so as we try and get closer, he's just going to keep going, but the cop in this case

11:00.540 --> 11:08.100
is going much faster, so he will catch up, but that is the flea and the sick.
