WEBVTT

00:02.380 --> 00:04.570
In this lecture, we will examine flocking.

00:04.780 --> 00:07.640
Flocking has to be one of my favorite techniques.

00:07.660 --> 00:12.220
It involves simple rules that generate complex behaviors that are mesmerizing.

00:12.580 --> 00:17.980
Flocking can be used to procedurally generate the movement of groups of individuals that like to stay

00:17.980 --> 00:20.680
together while moving towards a common goal.

00:20.710 --> 00:25.750
The flight of birds comes to mind, but this technique can be used pretty much anywhere.

00:25.750 --> 00:30.940
You have a group that needs complex, moving behaviors that you don't want to program by hand.

00:31.240 --> 00:36.070
In this workshop, we'll be creating flocking or schooling fish.

00:36.520 --> 00:41.620
So in unity, we're going to create a school of flocking fish.

00:41.740 --> 00:48.610
Now, what you'll need for this is to bring in the fish unity package that is attached to this lecture.

00:48.610 --> 00:52.720
And what you'll get is this wonderful fish that I made.

00:52.720 --> 00:56.350
Now, I'm not the best animator or model or in the world.

00:56.350 --> 00:59.560
If you have a better fish than this, then feel free to use it.

00:59.560 --> 01:06.520
If you press play at the moment after you've opened up the swimming scene, you're going to get my green

01:06.520 --> 01:11.050
fish here, which is moving his tail and trying to swim.

01:11.050 --> 01:17.650
Of course, he's not actually going anywhere at the moment, so we're going to use him as an agent or

01:17.650 --> 01:19.480
a bot or whatever you like to call it.

01:19.510 --> 01:26.500
Within our flocking system, the first thing we're going to need in the hierarchy is an empty object

01:26.500 --> 01:28.870
called flock manager.

01:28.870 --> 01:33.100
So that's going to keep track of all of the fish.

01:33.220 --> 01:33.640
All right.

01:33.640 --> 01:37.510
So once you've done that, we're now going to create a new script.

01:37.510 --> 01:43.120
So in your assets, create C-sharp script, and we'll call that Flock manager as well.

01:43.420 --> 01:50.050
You want to attach that to your flock manager object that's in the hierarchy.

01:50.560 --> 01:53.110
Okay, so let's start editing that.

01:53.350 --> 01:54.640
Let's open it up.

01:54.820 --> 01:55.420
Okay.

01:55.420 --> 01:59.530
So what we need at the top are some things to keep track of everything.

01:59.530 --> 02:09.370
First of all, we need a public game object for our fish prefab so that we can create fish spontaneously,

02:09.370 --> 02:17.680
basically, given the number of fish that we set it up to be so public int num fish, let's set that

02:17.680 --> 02:22.260
as a default to 20, which is quite a nice number to start with.

02:22.540 --> 02:27.220
Public game object array of all fish.

02:27.220 --> 02:31.930
So once they're instantiated we're going to put them into an array so we can keep track of them and

02:31.930 --> 02:32.800
control them.

02:32.830 --> 02:43.930
And then we need a public vector three for our swim limits, and this will be the space that the fish

02:43.930 --> 02:46.150
can actually swim within.

02:46.150 --> 02:50.860
So think of it like a fish tank, for example, and the size of that fish tank.

02:50.860 --> 02:58.420
So we will make it a new vector three and I'm going to make it five by five by five.

02:59.500 --> 03:04.540
Again, that's something that you can update if you want to make a bigger or smaller, right?

03:04.540 --> 03:07.630
So down in the start, we need to instantiate all of our fish.

03:07.630 --> 03:13.390
So we go all fish equals new game object.

03:14.530 --> 03:15.880
Now I'm fish.

03:17.370 --> 03:25.050
And then four int i equals zero I is less than numb.

03:25.050 --> 03:28.410
Fish eye plus plus.

03:28.920 --> 03:33.000
And then inside of here we'll create a position to start our fish.

03:33.000 --> 03:39.310
So vector three pause equals this dot transform position.

03:39.330 --> 03:45.270
Now this is going to mean that the position of the fish are relative to where the flock manager is.

03:45.280 --> 03:51.570
So the flock manager is going to be basically the origin of your fish tank, which is going to be five

03:51.570 --> 03:52.860
by five by five.

03:52.860 --> 03:54.960
So just keep that in mind.

03:55.080 --> 04:04.380
Now, from that fish tank, we're then going to go plus a new vector three and we're going to do random

04:04.380 --> 04:09.860
ranges around our swim limits of where we're going to position our fish when we first create them.

04:09.870 --> 04:21.960
So this will be random dot range, minus swim limits, dot x and then we want positive swim limits,

04:22.830 --> 04:29.940
dot x bracket, and we want to do that three more times or two more times.

04:29.940 --> 04:34.560
I mean, so we'll copy that and put it here and here.

04:34.770 --> 04:40.260
And these will be the Y and the Z positions for your fish.

04:41.190 --> 04:42.510
So line that up.

04:42.510 --> 04:44.670
So make this a Y value.

04:46.290 --> 04:48.210
Then make this the Z value.

04:49.160 --> 04:50.930
And just finish off our.

04:52.490 --> 04:54.410
Line of code with semicolon.

04:54.470 --> 05:03.500
And then once we've got that will go all fish position I in our array is going to equal a instantiation

05:04.670 --> 05:13.910
instantiate fish prefab at the position we just calculated and we'll give it a Catalonian identity.

05:13.910 --> 05:18.860
So just a zeroed rotation for that particular fish that's created.

05:19.010 --> 05:20.570
Okay, so save that.

05:20.660 --> 05:25.100
We'll go back into unity and test out the creation of these fish.

05:25.100 --> 05:33.410
So the prefabs for the fish that you'll find, you've got a green fish down here and a pink fish.

05:33.410 --> 05:36.290
So we're just working with the green fish at the moment.

05:36.290 --> 05:38.420
In fact, they're both exactly the same.

05:38.420 --> 05:39.740
They're colors just different.

05:39.740 --> 05:44.180
And if you want to create different coloured fish other than green and pink, go ahead and make different

05:44.180 --> 05:46.940
colour materials and put them on those models.

05:47.090 --> 05:53.540
Alright, so we've got our flock manager here, this green fish, you can actually just delete it.

05:53.540 --> 05:56.570
We don't need it in there because the flock manager is going to create them.

05:56.570 --> 06:02.390
Now my flock manager is at some weird position that it got put into, so we might want to just zero

06:02.390 --> 06:10.520
it in the world, 000 I'm going to double click on it so that my scene is focusing on it.

06:10.550 --> 06:16.880
You might also want to go to your main camera and go game object, align with view.

06:16.880 --> 06:21.950
So it's also looking towards that 000 position.

06:21.950 --> 06:30.050
Then we're going to grab our green fish and put it into our flock manager over in the inspector, and

06:30.050 --> 06:33.650
then we can press play and see what we get.

06:34.760 --> 06:37.160
So there's our 20 fish.

06:37.460 --> 06:40.550
If we come back to our scene, we'll be able to go in a bit further.

06:40.550 --> 06:49.460
They're all within the swim limits and they're all not actually going anywhere at all because we haven't

06:49.460 --> 06:52.490
programmed to the move, but they are animated.

06:52.490 --> 06:54.890
So that's a start.

06:55.950 --> 06:56.400
Okay.

06:56.400 --> 06:58.080
Well, lay flat manager for a moment.

06:58.080 --> 07:04.860
We'll come back into the project and we're going to create another script that we will put on our prefab

07:04.860 --> 07:05.220
fish.

07:05.220 --> 07:06.900
So we'll call it flock.

07:07.230 --> 07:12.660
So you want to drop Flock script onto your green fish and pink fish.

07:12.660 --> 07:19.740
So drag and drop that onto that prefab and drag and drop that onto that prefab and any others that you

07:19.740 --> 07:21.630
might have created in different colors.

07:21.780 --> 07:28.380
So let's open up our Flock script and in here we're going to come up to the top.

07:28.380 --> 07:31.170
We're going to set a float for the speed.

07:31.380 --> 07:36.930
Now we want to set this speed from our Flock Manager script.

07:36.930 --> 07:42.540
So I'm actually going to create a singleton in there or a static pointer to it so we can use that for

07:42.540 --> 07:43.740
each of our fish.

07:43.740 --> 07:53.280
So back in Flock Manager up the top, let's create a public static flock manager call it if I'm in capital.

07:53.280 --> 07:59.820
So flock manager And then in the start we'll go flock manager equals this to set it up.

08:00.960 --> 08:06.060
Then we'll create a few more values that we can set via the inspector, which will be our minimum and

08:06.060 --> 08:07.950
maximum feed speed for our fish.

08:07.950 --> 08:14.940
So at the top here we'll put header and fish settings.

08:17.670 --> 08:21.750
And then inside of here, we want a range.

08:22.530 --> 08:30.660
So this is how you can program in different sort of UI elements for your inspector if you haven't done

08:30.660 --> 08:31.260
this before.

08:31.260 --> 08:33.300
So we're going to create a header which is a title.

08:33.330 --> 08:46.230
We then want range, which will be a slider between zero and 5.0 F and then we also want a public float

08:46.230 --> 08:51.480
min speed, which will be the value for this particular range.

08:51.480 --> 08:57.180
And then we want this again and we'll have a max speed down here.

08:58.920 --> 08:59.370
Okay.

08:59.370 --> 09:08.130
So now if we just save this and quickly go back into unity, you'll find over in your flock manager

09:09.600 --> 09:14.400
here that you've now got these minimum, maximum speeds that you can set for your fish.

09:14.400 --> 09:18.720
So while you're here, you might just want to give them a bit of a value to start with because we'll

09:18.720 --> 09:19.800
just test that.

09:19.800 --> 09:25.380
So in the Flock script, we can actually use those values to set up our speed.

09:25.530 --> 09:33.600
So we will go in this case, that speed is going to equal the flock manager dot.

09:36.450 --> 09:48.480
F m dot min speed and flock manager dot fm dot max speed.

09:48.900 --> 09:55.980
And we will actually use those values inside a random dot range like that.

09:55.980 --> 10:02.670
So it's going to set the speed for the fish randomly from our static point that's back into our flock

10:02.670 --> 10:04.800
manager for those two values that have been set.

10:05.460 --> 10:05.820
Right.

10:05.820 --> 10:07.680
So it actually see this speed in action.

10:07.680 --> 10:17.220
Let's just go into our update for our flock and we'll go this dot transform, dot translate and capital

10:17.220 --> 10:18.330
translate.

10:18.510 --> 10:28.430
And we're going to go forward along the Z axis zero zero and it'll be speed multiplied by time, dot

10:28.590 --> 10:31.770
delta, time to smooth out the movement.

10:31.950 --> 10:34.770
Okay, so that's going to move it.

10:34.770 --> 10:38.250
Let's put translate not transform in there.

10:38.340 --> 10:41.940
So let's save it and we will now try this out.

10:41.940 --> 10:46.050
So we should have fish moving forward at different speeds.

10:46.170 --> 10:48.180
Let's press play and have a look.

10:49.260 --> 10:49.980
And.

10:50.160 --> 10:50.540
Yep.

10:51.960 --> 10:52.290
Dip.

10:52.320 --> 10:52.800
There they go.

10:52.800 --> 10:55.740
So some fish are faster than other fish.

10:56.320 --> 11:02.350
Okay, so that's the basic set up for all of our code as far as linking and creating fish and all that.

11:02.370 --> 11:09.300
When we come back in the next video, we will start working with the flocking algorithm.
