WEBVTT

00:02.240 --> 00:08.030
As I mentioned in the previous lecture, developing the eye for a crowd of NPCs is far simpler than

00:08.030 --> 00:09.230
most people think.

00:09.260 --> 00:14.960
This is because from simple rules, complex behaviour can develop and although we aren't programming

00:14.960 --> 00:21.080
the so called brains of the NPCs, they still appear to move with some level of intelligence.

00:21.380 --> 00:26.480
In this lecture, we will explore crowds further with a group of animated characters.

00:27.050 --> 00:31.990
Okay, so to get started, you'll need to bring in the starter file attached to this lecture.

00:32.000 --> 00:36.560
Now, when you bring it into unity, you will get a new scene called Crowd.

00:36.590 --> 00:44.030
Double click on that and open up your scene, in which case you will see these characters all ready

00:44.030 --> 00:45.440
to go for you.

00:45.470 --> 00:50.800
Now, I've done a lot of the hard work as far as the animations go with these guys.

00:50.810 --> 00:53.140
Let me just show you what they are.

00:53.150 --> 01:00.470
So I'm just going to go into the controller for the crowd, and this is what the animation looks like.

01:00.470 --> 01:01.310
It's very simple.

01:01.310 --> 01:07.790
You've got Idol, you've got walking and got running, and you can transition between those different

01:07.790 --> 01:08.770
states.

01:08.780 --> 01:14.450
Now in here we have some parameters for is running, is walking and is idle.

01:14.450 --> 01:21.170
So these are trigger values that are going to allow us to transition from one state to the other.

01:21.170 --> 01:26.390
And we'll be using these values here to trigger the animations.

01:26.390 --> 01:30.950
If you're not familiar with the animation system, don't worry too much.

01:30.950 --> 01:33.590
This is going to do all of the work for you.

01:33.590 --> 01:37.580
Just don't touch it and change what it's doing.

01:37.760 --> 01:43.000
So we will just write code that will turn these things on and off to get our characters to move.

01:43.010 --> 01:47.600
So let's go back into our scene or select this girl again.

01:48.020 --> 01:49.070
Have a look in the inspector.

01:49.070 --> 01:52.880
All the characters are going to have an animation agent already attached to them.

01:52.880 --> 01:55.490
They have a speed set to.

01:55.520 --> 02:01.790
The reason the speed is set to two is that it's sort of the best speed for the walking animation.

02:01.790 --> 02:09.200
So when you have an animated character and it's an agent, you have to try and synchronize the speed

02:09.200 --> 02:16.640
of the agent with the speed of the animation itself so that they kind of look fairly believable.

02:16.970 --> 02:17.480
All right.

02:17.480 --> 02:24.290
So at this point, let's just press play and have a look at what we get with these particular characters.

02:24.290 --> 02:28.040
You'll see that they go straight into their idle state.

02:28.160 --> 02:32.210
They're waiting to be given a goal to walk towards.

02:32.210 --> 02:35.180
So let's get about doing that.

02:35.210 --> 02:40.730
And we're going to use our eye control script that we used before.

02:40.880 --> 02:44.960
So let's just open that up and we'll make a few modifications.

02:45.890 --> 02:46.220
All right.

02:46.220 --> 02:52.220
So are we going to, first of all, just get rid of the public off of our game object here and actually

02:52.220 --> 02:56.540
make it into an array because we'll have multiple positions.

02:56.540 --> 03:00.170
So let's go go locations that we'll call this.

03:00.200 --> 03:02.930
Then we have our agent as we have before.

03:02.930 --> 03:03.680
That's fine.

03:03.680 --> 03:09.530
Now the agent here is going to get the mesh agent on the character.

03:09.620 --> 03:13.820
We then want to also read in all of our goals.

03:13.820 --> 03:24.770
So we'll go goal locations equals game object dot find objects with tag and we have to create some goals

03:24.770 --> 03:28.860
and actually set them to the tag of goal like that.

03:28.860 --> 03:36.680
So it'll populate our goal locations and then the set destination, it's going to get something from

03:36.680 --> 03:37.790
the goal location.

03:37.790 --> 03:38.540
So goal.

03:41.280 --> 03:45.270
Locations and we will get a random value out of that.

03:45.270 --> 03:47.730
So let me just put above this.

03:47.730 --> 03:50.760
We'll have an INT for.

03:53.000 --> 04:03.110
So I will call it the index into the goal locations and set this to random range between zero and goal

04:03.350 --> 04:06.860
locations dot length.

04:10.060 --> 04:13.900
And that will get us the index into that array there.

04:13.920 --> 04:16.330
Then once we've got that, we want to get the position.

04:16.330 --> 04:18.900
So transform it.

04:20.110 --> 04:22.020
Position like that.

04:22.030 --> 04:22.450
Right?

04:22.450 --> 04:23.830
So save that.

04:23.950 --> 04:26.380
Now let's go back into unity.

04:26.380 --> 04:30.760
And what we're going to do is select all of our characters here.

04:32.690 --> 04:36.050
And drag and drop the air controller onto them.

04:37.850 --> 04:38.610
Okay.

04:38.630 --> 04:41.600
Now we have to create some goal locations.

04:41.600 --> 04:46.570
So let's come up into our scene and we'll scroll over a bit.

04:46.580 --> 04:52.180
We're going to go into a hierarchy, right click 3D object, create a cube.

04:52.190 --> 04:57.980
Now that cube doesn't need to be that big and we're going to get rid of the box collider on it so we

04:57.980 --> 05:00.080
can actually scale it down.

05:00.080 --> 05:01.910
It's just a location.

05:03.450 --> 05:03.930
Let's see.

05:03.930 --> 05:06.300
But we want it to be on the ground, so let's move that down.

05:06.300 --> 05:08.580
So it's actually on the ground.

05:12.410 --> 05:13.080
Okay.

05:13.100 --> 05:16.640
And we're going to while we're here, tag it.

05:17.000 --> 05:28.430
So go add tag add goal with lowercase save for the tag, click that cube and then set it to goal.

05:28.490 --> 05:32.900
Now once we've got one goal location set, we can create another one.

05:32.900 --> 05:37.250
So just duplicate that cube and put it over here.

05:39.370 --> 05:41.750
All right, so we're ready to try this out.

05:41.770 --> 05:43.990
Let me just check where my camera is pointing.

05:43.990 --> 05:45.200
Yep, that's fine.

05:45.220 --> 05:52.360
Now let's press play and have a look where they go so they're going to pick a random location.

05:52.390 --> 05:56.220
Notice that they're sliding in the idle state towards it.

05:56.230 --> 05:57.730
That's all right for now.

05:57.940 --> 06:04.600
But you can see that these guys selected this location and these guys selected that location.

06:04.630 --> 06:12.010
And if you add more goals, then they will just go to different locations randomly.

06:12.790 --> 06:13.150
Okay.

06:13.150 --> 06:15.730
So let's now add in our animation.

06:15.730 --> 06:18.760
So at the top of our code, we're going to put animator.

06:20.380 --> 06:21.160
Adam.

06:21.850 --> 06:25.030
And then we'll get hold of that down here in the start.

06:25.030 --> 06:35.560
So we go Adam equals this dot get component in brackets animator and then we can set the trigger so

06:35.560 --> 06:45.010
Adam dot set trigger and then inside of here we want to put the string that represents the trigger that

06:45.010 --> 06:47.560
we want to trigger, I guess.

06:47.590 --> 06:48.580
Okay, so what is that?

06:48.580 --> 06:55.390
Let's just quickly duck back into unity, grab one of our characters and go into our animator and have

06:55.390 --> 06:55.900
a look.

06:55.900 --> 07:00.130
So we're going to go from Idol to walking, which is this arrow here.

07:00.130 --> 07:07.840
So this transition is if you have a look over the inspector is walking capital W it's what's written

07:07.840 --> 07:08.200
over here.

07:08.200 --> 07:13.180
This has to be exactly the same spelling and capitalization back in the script.

07:13.300 --> 07:21.820
Okay, so let's go back to our script and in here we'll put is walking like that.

07:21.820 --> 07:30.580
Okay, so save that and now we'll go back into our scene and press play and you'll be able to see the

07:30.580 --> 07:35.890
characters running the walking animation towards their goal location.

07:35.890 --> 07:38.260
So off they go and they're walking.

07:38.440 --> 07:43.900
Now once they get there because there's a few of them, they're kind of fighting over whether they're

07:43.900 --> 07:45.040
at the destination or not.

07:45.040 --> 07:49.540
So they're kind of pushing each other out of the way, which is interesting kind of behavior.

07:49.540 --> 07:50.770
Again, this is inherent behavior.

07:50.770 --> 07:56.080
We didn't program them in to do this, but they're just doing that, which kind of looks quite neat.

07:57.520 --> 08:02.860
So let's modify the code just a little bit more so that when they do reach their destination, they'll

08:02.860 --> 08:05.020
actually go to another location.

08:05.020 --> 08:14.440
So we're going to put if agent remaining distance is less than one, then they've reached their destination

08:14.680 --> 08:20.650
and if they have, we will go agent set destination basically want to do this line here again.

08:20.650 --> 08:25.630
Let's get another random location and go to it down there.

08:25.780 --> 08:28.060
Okay, so save that.

08:28.060 --> 08:35.920
Let's switch back into unity now with your cubes you've got for your goal locations, you can duplicate

08:35.920 --> 08:42.580
them and move them to wherever you would like like that.

08:42.790 --> 08:43.480
Okay.

08:43.480 --> 08:46.870
And then we can actually get our group of people.

08:46.870 --> 08:49.900
So shift select all of those and hit control.

08:49.900 --> 08:59.290
D And while we've got that group, we might just move them out and then control D and move them out

08:59.290 --> 09:01.330
and control D and move them out.

09:01.360 --> 09:03.160
Now we have a much bigger crowd.

09:03.190 --> 09:09.250
Let's press play and have a look at them wandering around in the scene.

09:09.550 --> 09:10.390
So off they go.

09:10.420 --> 09:17.860
They're going to find a random location to go to and when they've reached it, they will go to a different

09:18.130 --> 09:19.210
random location.

09:19.210 --> 09:22.390
So you can see them making a choice when they get there.

09:23.590 --> 09:25.210
Let me come back in the next lecture.

09:25.210 --> 09:30.340
We will continue this and have a look at some more sort of complex behaviors for the crowding.
