WEBVTT

00:02.520 --> 00:08.160
In the previous lecture, we started with the code for fleeing, so we were able to drop a cylinder

00:08.160 --> 00:09.490
into the scene.

00:09.510 --> 00:15.360
What we're going to do now is continue with the code for that and write the code that is going to go

00:15.360 --> 00:18.900
inside of the A.I. control for our characters.

00:18.930 --> 00:22.830
First of all, I want you to find where you put all of your characters.

00:22.830 --> 00:24.330
That's if you rolled them up like I did.

00:24.330 --> 00:25.950
Now mine are under the city block.

00:26.010 --> 00:29.730
We need to select all of the characters.

00:29.730 --> 00:36.150
So shifts select and then make sure that we tag them with agent.

00:36.510 --> 00:46.020
So put a plus in there and agent because the code in the drop cylinder actually needs to grab hold of

00:46.020 --> 00:50.670
all of our characters so that they can send a command to them basically.

00:50.670 --> 00:58.350
So with that done again, make sure just select all of these and make sure I add that tag rather than

00:58.350 --> 00:59.280
just create it.

00:59.280 --> 01:03.210
Okay, so they're all there now we're going to go back into our AI controller.

01:03.240 --> 01:09.690
The first thing we want to do is create a reset method that we can run every time an agent gets to a

01:09.690 --> 01:10.710
goal location.

01:10.710 --> 01:18.480
So void reset agent and we're going to move some of the start code down into here.

01:18.480 --> 01:26.490
So the things that we want are our speed multiplier, which will be this line here, but we're going

01:26.490 --> 01:28.830
to separate it out by putting that float up at the top.

01:28.830 --> 01:36.990
So float speed molt and then we'll grab this line of code and actually these two lines of code as well.

01:36.990 --> 01:45.900
So just get those out of there and put them down into here and we'll set this to speed malt and we'll

01:45.900 --> 01:48.060
set this here to speed.

01:49.280 --> 01:49.910
Molt.

01:49.910 --> 01:53.090
And of course, we need to pass that through to their as well.

01:53.570 --> 01:56.550
Multiply it with the speed for our agent.

01:56.570 --> 01:57.080
Okay.

01:57.080 --> 02:04.520
So besides those things we're also going to set our triggers for is walking and we're going to grab

02:04.520 --> 02:08.810
that one and put that down in here.

02:09.230 --> 02:10.880
That's delete it out of there.

02:11.060 --> 02:16.710
And finally on Reset, we will go Agent Dot reset path.

02:16.730 --> 02:23.300
Now, this is a command that belongs to the nav mesh agents, which allows you to sort of reset the

02:23.300 --> 02:29.180
path, basically, which is to stop them going along their current path and sort of like clear wherever

02:29.180 --> 02:31.340
they're going out of their system.

02:31.370 --> 02:31.760
Okay.

02:31.760 --> 02:36.050
So now that we've got that, we can put that into our start.

02:36.050 --> 02:43.880
So just under here, we will run reset agent like that.

02:44.030 --> 02:49.400
Okay, now let's move down and put in our new method.

02:49.400 --> 02:53.700
That drop cylinder is going to call when a cylinder is dropped.

02:53.720 --> 02:54.080
Right.

02:54.080 --> 03:06.980
So under here, we will put in this public void, detect new obstacle and send in a vector three for

03:06.980 --> 03:09.050
the position of that obstacle.

03:10.220 --> 03:20.330
And then inside of here we'll go if vector three dot distance between the agent so position this dot

03:20.330 --> 03:21.890
transform.

03:23.710 --> 03:30.850
Dot position is less than our detection radius.

03:31.600 --> 03:35.980
Now, that's something we have to define at the top of the code, which we'll do in a moment.

03:36.160 --> 03:42.470
So if we're within the detection radius, then we're going to actually flee from the position.

03:42.490 --> 03:44.680
So vector three.

03:45.640 --> 03:51.400
Flee direction equals this dot.

03:52.410 --> 04:02.760
Transform top position minus position that's being passed through and we're going to normalize that,

04:03.120 --> 04:08.070
which will set it to a length of one so normalized.

04:08.940 --> 04:12.690
And then vector three new goal.

04:13.910 --> 04:24.700
Equals this dot transform position plus fli direction multiplied by fli radius.

04:24.710 --> 04:34.160
Now that's how far we want to actually flee away from the obstacle that we've detected.

04:34.160 --> 04:39.530
So let's create these two objects at the top of our code.

04:39.530 --> 04:52.100
So we want a float for detection radius which will equal, let's say, five, and a float for FLI radius,

04:52.100 --> 04:54.500
which will equal ten.

04:54.980 --> 04:58.550
Okay, so let's continue writing this code down here.

04:58.550 --> 05:08.810
So we've created a new goal location to go to, which is from the position of our player or our agent,

05:08.810 --> 05:13.940
plus the fli direction multiplied by a certain radius to push it out.

05:14.210 --> 05:21.050
So nav mesh path equals new nav mesh path.

05:21.410 --> 05:24.470
Agent dot calculate path.

05:25.770 --> 05:27.510
New goal.

05:29.350 --> 05:30.730
Return the path.

05:32.350 --> 05:46.960
And then we're going to say if path dot status does not equal nav mesh path status dot path in valid,

05:47.860 --> 05:51.570
then we'll actually set the character along that path.

05:51.580 --> 05:52.000
Right.

05:52.000 --> 05:58.120
So just going back to this code for a minute, this is our nav mesh functionality that's built in.

05:58.120 --> 06:03.910
So we're going to rather than setting the destination for an agent like we've done before down here,

06:03.910 --> 06:06.160
we're going agent set destination.

06:06.160 --> 06:14.950
We're first of all seeing if we can calculate a valid path from where we are to where we want to be,

06:15.190 --> 06:20.920
because that new location might actually be off the nav mesh and might not be possible.

06:20.920 --> 06:26.660
So therefore we don't want to set the agent going there if they can't get to their OC.

06:26.740 --> 06:34.570
So if they can get to there, the path is not invalid for our agent, then we'll go agent set destination

06:34.570 --> 06:38.830
and we'll set it to path dot corners.

06:39.990 --> 06:48.420
Path, dot corners, dot length minus one and then annum.

06:48.930 --> 06:59.070
Dot set trigger is running so this will set our animation to and is running the agent dot speed will

06:59.070 --> 07:06.840
equal ten at this point and the agent dot angular speed, which is how fast it turns around.

07:06.840 --> 07:10.680
We'll set that to 500 now because we're setting that there.

07:10.680 --> 07:15.150
We'll have to set it to a smaller value when we reset it.

07:15.150 --> 07:20.490
Because when you go back to walking between the gold boxes, you don't actually want to turn around

07:20.490 --> 07:22.170
really fast like you're fleeing from them.

07:22.170 --> 07:22.950
So up here will go.

07:22.950 --> 07:30.210
Agent dot angular speed equals 120.

07:30.360 --> 07:34.050
Okay, let me just come back down to this set destination.

07:34.050 --> 07:41.520
So when we get the path through here, what it does is it gets a whole bunch of waypoints and sticks

07:41.520 --> 07:47.250
them into a path list or a array called corners.

07:47.520 --> 07:54.510
And these corners are our basic waypoints from our current position to the last position or the goal

07:54.510 --> 07:55.110
position.

07:55.110 --> 08:02.340
So the goal position in this list is going to be at the end of the list, which is why we put this in

08:02.340 --> 08:05.310
here in the square brackets to index it.

08:05.970 --> 08:06.330
All right.

08:06.330 --> 08:07.770
So let's save that now.

08:07.770 --> 08:14.760
We'll go back into drop cylinder because we need to make sure that we run that detection for each agent

08:14.760 --> 08:17.370
down in our update where we put that down in here.

08:17.370 --> 08:19.440
So this is our for loop going through each agent.

08:19.440 --> 08:21.510
We've tagged all of our agent.

08:21.510 --> 08:29.550
So down here we're going to go a dot get component I control.

08:31.490 --> 08:36.050
Dot detect new obstacle.

08:38.210 --> 08:45.520
And we're going to pass through the hit info point like that.

08:45.890 --> 08:52.040
So this will send through the position of the cylinder to the agents who will then flee from it.

08:52.040 --> 08:53.090
So we'll save that.

08:53.090 --> 08:56.570
Let's go back into unity and we'll try this out.

08:56.570 --> 08:59.930
So we'll press play and everyone is wandering around.

08:59.930 --> 09:02.030
Let's drop in a cylinder.

09:02.690 --> 09:03.220
Okay.

09:03.230 --> 09:06.020
Nobody seems to be very fazed by the cylinder, though.

09:06.020 --> 09:08.380
There are some people running around.

09:08.390 --> 09:08.620
Okay.

09:08.630 --> 09:12.320
So that girl, they ran around and away from the cylinder.

09:12.500 --> 09:15.080
Okay, so let's stop playing this.

09:15.080 --> 09:19.490
What I'd like to do, first of all, is actually move into a better position up here.

09:19.490 --> 09:25.880
So let's just come around into that and then we'll grab our camera and align to view so we can actually

09:25.880 --> 09:28.010
drop our cylinders into a better place.

09:28.130 --> 09:30.620
So align with view of the camera.

09:30.620 --> 09:36.470
Now let's go back into our control and what we're going to do up in here for our detection radius is

09:36.470 --> 09:45.230
just set that to 20 to increase the distance of seeing the cylinder because if you don't see the cylinder,

09:45.230 --> 09:48.470
then you can just sort of keep walking towards it, I guess.

09:48.500 --> 09:53.390
Okay, so we're saving that and we're going to run this again and have a look.

09:53.690 --> 09:54.080
Okay?

09:54.080 --> 09:59.210
So once the crowd's kind of all wandering around, we can drop a cylinder like here.

09:59.210 --> 10:06.680
And you can see they all fled from it over here and they're all fleeing from it and going absolutely

10:06.980 --> 10:07.850
crazy.

10:08.180 --> 10:13.760
Now we need to stop them from running once they're set running currently they go to running and then

10:13.760 --> 10:15.560
they just keep running and running and running.

10:15.560 --> 10:17.090
So this is a challenge for you.

10:17.120 --> 10:20.150
Can you figure out how we need to modify the code?

10:20.150 --> 10:21.200
And let me give you a hint.

10:21.200 --> 10:26.120
You only have to insert a new line of code in the update method to fix this.

10:26.120 --> 10:27.200
So pause video.

10:27.200 --> 10:28.580
Now go to give that a go.

10:28.580 --> 10:30.800
And when we come back, I'll show you how to do it.

10:31.760 --> 10:32.390
All right.

10:32.420 --> 10:33.440
How did you go with that?

10:33.440 --> 10:40.100
Well, if we go back into our AI controller, you'll remember that we put this reset agent in here.

10:40.100 --> 10:45.140
We actually need to run that whenever we reach a goal location.

10:45.140 --> 10:50.420
So the goal location is being checked down here in the update.

10:50.420 --> 10:56.720
So what we want to do after we've reached our location before we get a new goal location is to actually

10:56.720 --> 11:04.430
run reset agent, which will set the animations back to walk and also set the speeds and everything

11:04.430 --> 11:05.750
back to normal.

11:05.750 --> 11:08.960
So sort of calm walking around rather than fleeing.

11:08.960 --> 11:13.310
Okay, so if we just save that quickly, go back into our scene.

11:13.310 --> 11:21.710
Now, what I'm going to do is just to test this and it's going to grab all of these people and control

11:21.710 --> 11:25.370
d them to make a whole double set of everybody.

11:25.370 --> 11:27.200
So we'll get a lot of people running around.

11:27.230 --> 11:29.090
Let's now press play.

11:29.090 --> 11:29.390
All right.

11:29.390 --> 11:34.130
So they're all going a little bit too fast to really notice the effect here.

11:34.130 --> 11:36.350
And they're actually going super fast to begin with.

11:36.350 --> 11:37.700
They're not running away.

11:37.730 --> 11:43.970
They're actually running this code here for our speed multiplier, which has got this to value in here.

11:43.970 --> 11:47.600
So I'm actually going to slow these guys down so that we can really see the effect.

11:47.600 --> 11:51.350
And let's put this at 1.5 F in there.

11:51.350 --> 11:53.090
Okay, so we'll save that.

11:53.090 --> 12:01.430
Now, before I play this, I'm also going to grab hold of one of these goal locations and duplicate

12:01.430 --> 12:08.630
it and just put some out here so that we get more people running around into the center of the scene.

12:08.720 --> 12:16.100
Okay, so let's press play and we'll have a look at them as they come out into the center here.

12:16.340 --> 12:22.340
And once they get moving around, we can actually start to drop some cylinders in amongst a whole bunch

12:22.340 --> 12:22.550
of them.

12:22.550 --> 12:26.900
So like down here see them all fleeing away from it.

12:27.800 --> 12:28.910
So that's fleeing.

12:28.910 --> 12:33.710
And I hope you enjoyed playing around with this crowd and this technique.
