WEBVTT

00:02.850 --> 00:05.970
In this lecture, we will look at the concept of fleeing.

00:06.180 --> 00:11.220
It's a common technique used in AI for getting an NPC to move away from a location.

00:11.250 --> 00:14.250
The technique can be used on any type of NPC.

00:14.280 --> 00:16.560
In this case, we will apply it to our crowd.

00:16.560 --> 00:19.290
We've been working on mathematically fleeing.

00:19.290 --> 00:20.460
Works like this.

00:20.670 --> 00:24.240
An object or location is introduced into an environment.

00:24.900 --> 00:29.120
The NPC determines where the object is in relation to their own position.

00:29.130 --> 00:33.210
They then calculate a new goal location that is away from the object.

00:33.480 --> 00:36.750
In this case, we will use the exact opposite direction.

00:37.260 --> 00:43.440
This can be calculated using the vector towards the object and taking that away from the NPC's position

00:44.100 --> 00:45.690
with the new goal location.

00:45.720 --> 00:51.180
The agent can turn around and start heading for it, thus being taken away from the object in question.

00:51.210 --> 00:56.410
Other factors we can consider are the radius of influence of the object and the cooldown period.

00:56.430 --> 01:02.190
The radius determines how close an NPC must be to an object to be affected by it, and the cooldown

01:02.190 --> 01:06.240
is how long the NPC flees for before returning to normal behavior.

01:06.720 --> 01:10.430
In the previous lectures we looked at creating our crowd.

01:10.440 --> 01:16.890
What we're going to do now is actually get them to flee from a potentially dangerous object.

01:16.890 --> 01:22.460
So I'm just going to turn a few things off that we've been using and turn some things on.

01:22.470 --> 01:28.680
So first of all, you're going to turn off the buildings and the shop, so I'm just going to take them

01:28.680 --> 01:33.660
out by unpicking them for enabled an inspector, the main camera.

01:33.660 --> 01:40.130
I will put that back on and my F P's controller, I will turn that one off.

01:40.140 --> 01:45.090
Now once you've done that, you'll want to come over to your navigation mesh.

01:45.630 --> 01:53.310
And in the scene view we're going to do a re bake because we want people to be able to walk everywhere.

01:53.490 --> 01:53.940
All right.

01:53.940 --> 01:58.860
So we've now got to find our cubes, which were our goals.

01:58.860 --> 02:05.760
Okay, so here's one here and here's one here, and there's another one just down here.

02:06.180 --> 02:08.070
Bring them up so that together.

02:09.370 --> 02:13.590
Now you can move them or you can create some more.

02:13.600 --> 02:21.400
So I'm just going to duplicate that one up in the corner and put one over there and then over here and

02:21.400 --> 02:27.220
over there, we just want to get the crowd sort of moving around this square.

02:27.400 --> 02:27.790
Okay.

02:27.790 --> 02:33.240
We're now going to add in our undesirable object that we want to flee from.

02:33.250 --> 02:33.640
Right?

02:33.640 --> 02:37.360
So I'm just going to roll up a few of these things.

02:37.360 --> 02:43.270
In fact, I might grab some of these people and actually make them children of this city.

02:43.270 --> 02:44.110
BLOCK objects.

02:44.140 --> 02:50.440
I can actually roll them up and just get rid of all of this extra mess that's happening inside of the

02:50.440 --> 02:54.070
hierarchy, which will allow you to see it a bit clearer.

02:54.100 --> 02:54.360
Okay.

02:54.370 --> 02:58.510
So let's put this girl up into the city block as well.

02:58.510 --> 02:59.260
Okay.

02:59.260 --> 03:02.110
So let's now right click in there.

03:04.180 --> 03:05.500
And 3D object.

03:05.500 --> 03:08.190
We're going to add in a cylinder.

03:08.200 --> 03:12.730
Now, the cylinder we want to move up so that we can see it.

03:12.850 --> 03:14.710
So hit the W key.

03:14.830 --> 03:16.450
Let's bring it up above the ground.

03:16.480 --> 03:21.070
Make sure that e's above the ground and then we're going to color that green.

03:21.070 --> 03:23.320
So I'm going to create a new material.

03:23.860 --> 03:27.250
Call it green, make it green.

03:27.250 --> 03:28.090
So select it.

03:28.090 --> 03:37.870
And then in the inspector, set the color for it, the albedo color to green, put that onto the cylinder.

03:37.900 --> 03:42.160
Now with that cylinder, it's going to get dropped when we click on the ground.

03:42.160 --> 03:48.790
So basically we don't want it there to start with and we're going to get it and then just put it into

03:48.790 --> 03:53.940
the assets folder, which will create a prefab of that cylinder for us.

03:53.950 --> 03:58.600
Then we can go ahead and delete it out of the hierarchy.

03:59.620 --> 04:04.840
Now we need to add in some code to drop the cylinder in place.

04:04.840 --> 04:13.870
So down in your assets, right click Create C-sharp script, let's call it drop cylinder and we're going

04:13.870 --> 04:16.650
to double click on that and edit it.

04:16.660 --> 04:17.050
Okay.

04:17.050 --> 04:21.580
So to begin with, we need to get the prefab for the object that we're dropping.

04:21.580 --> 04:24.850
So it'll be a public game object.

04:25.990 --> 04:26.950
Obstacle.

04:28.690 --> 04:35.860
And then we want a game object array of all the agents that are in the scene so that we can tell them

04:35.860 --> 04:39.430
the obstacle has been dropped in our start.

04:39.430 --> 04:46.600
We'll go agents equals game object dot find game objects with tag.

04:46.600 --> 04:54.400
So we have to tag all of our agents with the agent tag, which we'll do a little later on.

04:54.670 --> 05:01.870
And in the update we will test if we've clicked on the ground and if so, we will instantiate a green

05:01.870 --> 05:02.320
cylinder.

05:02.320 --> 05:06.670
So if input dot get mouse.

05:08.220 --> 05:09.480
Button down.

05:10.950 --> 05:13.050
Is the left mouse button.

05:13.350 --> 05:18.990
Then we're going to do a ray cast hit hitch info.

05:21.590 --> 05:22.470
Right.

05:23.420 --> 05:24.130
Right.

05:24.140 --> 05:33.050
Is going to equal the camera dot main screen point to ray input dot mouse position.

05:33.050 --> 05:41.180
So this line projects a ray from the click position into the environment and we're looking to hit something.

05:41.180 --> 05:44.420
So we do an IFF physics.

05:47.020 --> 05:48.070
Dot Ray.

05:48.070 --> 05:50.680
Cast Ray.

05:51.100 --> 05:51.970
Origin.

05:53.530 --> 05:53.960
Ray.

05:54.220 --> 05:55.180
Direction.

05:55.930 --> 05:56.780
Out.

05:57.310 --> 05:58.450
Hit info.

05:58.480 --> 06:00.520
Then we want to instantiate.

06:00.550 --> 06:04.150
So if we have hit something, we're going to instantiate our obstacle.

06:06.000 --> 06:10.590
At the hit info point

06:12.780 --> 06:16.200
obstacle transform.

06:17.460 --> 06:24.870
Dot rotation, which is just going to be the default rotation of the obstacle because INSTANTIATE needs

06:24.870 --> 06:31.860
a rotation, so we'll just put one in there for it and then we're going to send the message to each

06:31.860 --> 06:35.070
agent that we've actually dropped the cylinder.

06:35.070 --> 06:41.220
So this will be a for each game object A in agents.

06:42.720 --> 06:47.370
And for now we don't actually have any code that's going to do anything with that.

06:47.370 --> 06:48.780
So we'll just leave it empty.

06:49.110 --> 06:51.090
And so it doesn't actually give you any errors.

06:51.090 --> 06:55.230
Let's just comment it out for now, but leave it there.

06:55.230 --> 06:56.610
So save that.

06:56.610 --> 06:59.010
Now we're going to go back into unity.

06:59.010 --> 07:05.970
And I want you to drag and drop the drop cylinder script on to your camera.

07:05.970 --> 07:09.870
I mean, it can be on anything in the scene just as long as it's in there somewhere.

07:09.960 --> 07:13.800
So on my camera I've got drop cylinder down here.

07:13.800 --> 07:16.500
The obstacle has got cylinder in it.

07:16.500 --> 07:24.480
You need to drag and drop this prefab cylinder into that spot in order to get the cylinder to instantiate.

07:24.600 --> 07:30.270
The other thing is that this script that we just created uses the main camera and it won't be able to

07:30.270 --> 07:36.810
find the main camera unless the camera is tagged with main camera.

07:36.810 --> 07:42.120
So when it comes through initially this camera, I think it's going to have game controller on the tag

07:42.120 --> 07:43.080
that you've got.

07:43.080 --> 07:45.600
Make sure you set it to main controller.

07:45.660 --> 07:46.170
Okay.

07:46.170 --> 07:55.380
So now let's press play and we'll click on the surface here and we should get some cylinders.

07:55.380 --> 07:59.490
Now I'm getting an error about a tag not being defined for age.

07:59.490 --> 08:00.210
That's a fine.

08:00.210 --> 08:03.600
Just ignore that because we'll come back and put that in in a moment.

08:04.200 --> 08:04.530
Right.

08:04.530 --> 08:04.920
Okay.

08:04.920 --> 08:13.170
So we now need to work out how to get these agents to flee and we'll come back and work on that in the

08:13.170 --> 08:14.310
next lecture.
