WEBVTT

00:03.090 --> 00:07.720
I mentioned the flocking rules at the start of this section, but let's go over them again in detail.

00:07.740 --> 00:14.160
They are moved towards the average position of the group, align with the average heading of the group

00:14.160 --> 00:16.860
and avoid crowding other group members.

00:18.840 --> 00:23.730
Moving towards the average position of the group involves taking all the individual members positions,

00:23.730 --> 00:26.750
adding them together and dividing by the number in the group.

00:26.760 --> 00:32.910
We can then work out where an individual is in relation to the average location and have them turn towards

00:32.910 --> 00:36.020
it to align with the average heading of the group.

00:36.030 --> 00:40.790
We add all the headings of the individuals together and divide by the size of the group.

00:40.800 --> 00:45.270
This gives an average vector for individuals to try and align their movement.

00:46.660 --> 00:52.810
To avoid neighbors, an individual must be aware where the neighbors are and then turn away from them.

00:54.030 --> 00:59.490
Then to calculate a new heading and individual add together the vector for moving towards the center

00:59.490 --> 01:05.460
of a group, the vector to align with the group's heading and the vector to turn away from a neighbor.

01:07.540 --> 01:13.660
Before we start adding these rules into our Flock script, we're going to add a few more variables that

01:13.660 --> 01:18.100
you can change in the inspector to modify the behavior of the fish.

01:18.100 --> 01:22.630
So in our flock manager, we're going to add in two things.

01:22.630 --> 01:25.240
First of all, we want another range.

01:25.240 --> 01:28.840
So I'm just going to copy these and put them down here.

01:28.840 --> 01:37.780
Now, this range will be between one and ten and it's going to be called neighbor distance.

01:38.560 --> 01:45.880
And we want to use a neighbor distance because the fish are going to flock with their neighbors.

01:45.880 --> 01:51.850
That means you can set this, that they'll only abide by the rules with fish that are close to them

01:51.850 --> 01:53.800
and ignore fish that are far away.

01:53.800 --> 01:58.660
Because I don't actually want to be flocking with those particular fish that are in the distance.

01:58.660 --> 02:06.460
So we'll have a neighbor distance and then we'll copy this and we'll put this down here and we're going

02:06.460 --> 02:14.080
to add in a rotation speed as well as how fast the fish can turn around on the spot in order to keep

02:14.080 --> 02:15.520
up with the flock.

02:16.480 --> 02:16.640
Okay.

02:16.780 --> 02:22.900
And we'll set that between zero and five.

02:23.020 --> 02:28.300
Of course, zero is probably not a good value to have because fish aren't going to turn at all if they

02:28.300 --> 02:29.560
have a turn of zero.

02:29.560 --> 02:31.660
So let's set that to one instead.

02:31.780 --> 02:33.700
Okay, so let's save that.

02:34.540 --> 02:47.320
And back in our Flock script, we're going to add inside of the update and apply rules which we haven't

02:47.320 --> 02:47.980
written yet.

02:47.980 --> 02:54.820
But this method here is going to turn the fish around basically towards the direction it needs to be

02:54.820 --> 02:56.020
travelling in.

02:56.110 --> 02:59.620
And then we're just going to push the fish forward along here.

02:59.620 --> 03:03.760
Okay, so underneath update, we will write our apply rules.

03:03.760 --> 03:08.890
So this will be a void, apply rules.

03:10.480 --> 03:17.860
And then inside of here we're going to have game object array.

03:19.110 --> 03:20.090
Game objects.

03:20.100 --> 03:25.290
Now, that's going to be all of the fish that are back in the manager.

03:25.290 --> 03:37.530
So this will be joss equals flock manager, dot m dot all fish.

03:39.150 --> 03:49.650
Okay, so vector three to calculate the v center, which is the flock vector to the center of the group,

03:49.650 --> 03:53.160
and that's going to equal initially a vector 3.0.

03:54.300 --> 04:08.880
Then we want to vector three to avoid, let's call it V avoid vector to avoid equals vector 3.0 and

04:08.880 --> 04:11.850
then set that to zero.

04:11.850 --> 04:16.020
And then we're going to have float G speed.

04:16.020 --> 04:26.130
So this is our group speed, which equals 0.01 F by default, our float n distance which is our neighbor

04:26.130 --> 04:32.520
distance and then int group size or that will be the size of the fish that are within that neighbor

04:32.520 --> 04:32.850
distance.

04:32.850 --> 04:43.530
So that's the group, your current local group of fish right now we'll have a for each game object go

04:43.530 --> 04:45.000
in go's.

04:45.000 --> 04:52.710
So this is our game object in our go so it's basically for each fish in the fish array or in the school

04:52.710 --> 04:54.360
of fish that we're working with.

04:54.540 --> 05:02.760
If the current game object does not equal this game object, which is this current fish, because we

05:02.760 --> 05:05.550
don't want to compare this fish with itself.

05:07.070 --> 05:11.330
They were going to calculate the distance to the next fish.

05:11.330 --> 05:24.320
So distance and distance equals vector three point distance between go dot transform position and this

05:24.320 --> 05:28.250
dot transform dot position.

05:30.440 --> 05:37.250
And then if that fish you've just checked on out of the whole school is within the neighbour distance,

05:37.250 --> 05:43.430
then we will consider it a neighbor and it's going to influence the fishers current trajectory.

05:43.430 --> 05:55.940
So if NW distance is less than or equal to flock manager dot m dot neighbor distance.

05:57.760 --> 06:07.660
Then the vector to the center of the group is going to equal plus equals go transform position.

06:07.660 --> 06:14.650
So we're going to add all of the positions of any fish that are in our group together and our group

06:14.650 --> 06:15.490
size.

06:15.490 --> 06:18.010
We will increase by one.

06:18.010 --> 06:31.840
So plus, plus now if n distance is less than 0.10 or sorry, 1.0 f if we're nice and close, then we

06:31.840 --> 06:34.210
actually want to avoid that particular fish.

06:34.210 --> 06:44.070
So the avoid will equal v avoid plus the vector away from that particular fish.

06:44.090 --> 06:52.870
It'll be this dot transform dot position minus go dot transform position.

06:53.840 --> 06:54.440
Okay.

06:54.440 --> 07:03.260
So then we'll have floc another floc equals the go get component.

07:05.000 --> 07:05.540
Flock.

07:05.540 --> 07:09.500
So this is getting the flock script on the current comparison.

07:09.500 --> 07:16.400
Fish And what are we going to do with that is get it speed so that we can get an average of the speed

07:16.400 --> 07:16.910
of the group.

07:16.910 --> 07:23.630
So group speed equals G speed plus another flock.

07:24.280 --> 07:26.020
Dot spade.

07:26.890 --> 07:27.690
Okay.

07:27.700 --> 07:28.260
Right.

07:28.270 --> 07:34.840
Moving down after all of that, we then going to test for our group size.

07:34.850 --> 07:47.530
So if group size is greater than zero, the V center is going to equal the V center divided by group

07:47.530 --> 07:47.980
sizes.

07:47.980 --> 07:50.830
This is where we're actually averaging it at the top.

07:50.830 --> 07:52.360
We're adding them all together.

07:52.360 --> 07:57.670
And then after we finished adding them up, which is our v avoid now v center, we're going to average

07:57.670 --> 08:01.180
them down here because it's only after we've added them all up and counted them.

08:01.180 --> 08:04.540
Do we know how many neighbors we have in the group?

08:04.540 --> 08:13.240
So speed is going to equal G speed divided by group size and then we want a vector.

08:13.240 --> 08:27.550
Three direction is going to equal our v center plus lv avoid minus transform dot position.

08:28.060 --> 08:35.470
So this will be our new direction that we want our fish to head in if direction does not equal zero.

08:35.470 --> 08:35.740
So.

08:35.740 --> 08:38.110
Vector 3.0.

08:40.210 --> 08:55.360
Then we're going to transform dot rotation equals Catalonian dot slurp transform dot rotation and this

08:55.420 --> 09:01.340
saturnian slurp is going to turn the fish slowly towards the direction that it needs to go in.

09:01.360 --> 09:04.180
That's why we created that rotation speed earlier.

09:04.810 --> 09:11.170
Okay, so Catalonian look, rotation direction.

09:12.750 --> 09:29.010
And then flock manager, dot flock manager f m dot rotation speed multiplied by time dot delta time.

09:29.890 --> 09:36.180
Okay, so that's the code for the flock, so save that.

09:37.410 --> 09:41.280
Okay, so let's have a look what this is going to do.

09:41.280 --> 09:47.760
So we're going to go into our flock manager and make sure that we've got some values for everything.

09:47.760 --> 09:53.100
So our rotation speed, let's click on that to set it to one our neighbor distance.

09:53.100 --> 09:54.810
Let's make that.

09:56.390 --> 10:01.970
But now let's turn it right up and let's put rotation speed to sort of halfway along.

10:02.000 --> 10:07.370
Okay, let's press play and have a look at our flock.

10:07.370 --> 10:08.950
So there they are there.

10:08.950 --> 10:14.800
Let me go to the scene so I can actually zoom in and you'll be able to see the behavior of their fish.

10:14.830 --> 10:18.590
So they're actually really nicely flocking.

10:20.110 --> 10:27.310
Now, when we come back in the next lecture, we'll actually add a little bit of variety into here so

10:27.310 --> 10:33.130
that we can set a goal location and also to stop the fish speeding up, as you can see there, that

10:33.130 --> 10:35.470
they're just getting faster and faster.

10:35.470 --> 10:38.080
So I'll see you in the next lecture.
