WEBVTT

0
00:09.780 --> 00:12.180
In this lecture, we will examine 'Flocking'.

1
00:12.270 --> 00:15.060
Flocking has to be one of my favorite A.I. techniques.

2
00:15.070 --> 00:21.220
It involves simple rules that generate complex behaviors that are mesmerizing. Flocking can be used to

3
00:21.220 --> 00:26.680
procedurally generate the movement of groups of individuals that like to stay together, while moving

4
00:26.680 --> 00:28.190
towards a common goal.

5
00:28.210 --> 00:33.850
The flight of birds comes to mind but this technique can be used pretty much anywhere you have a group

6
00:33.850 --> 00:38.660
that needs complex moving behaviors that you don't want to program by hand.

7
00:38.710 --> 00:46.930
In this workshop, we'll be creating flocking (or schooling) fish. To follow along, download the 'AIfish' Unity

8
00:46.930 --> 00:49.500
package that comes with this lecture.

9
00:49.510 --> 00:54.570
Open up a new Unity project for 3D and bring that package in.

10
00:54.640 --> 01:03.740
When you add it into the Assets, you'll find that there is a green fish, a pink fish and a scene called Swimming. If you

11
01:03.750 --> 01:06.250
open up the Swimming scene itself,

12
01:06.250 --> 01:11.880
you'll get the green fish all by itself with the camera set up pointing at it

13
01:12.070 --> 01:17.730
and if you press Play, you'll find that that green fish has a swimming animation attached to it.

14
01:21.610 --> 01:25.950
Now, in order to create flocking you need more than 1 fish

15
01:25.960 --> 01:29.290
in this case because flocking is a group activity.

16
01:29.380 --> 01:32.240
So we're going to create a Flocking Manager

17
01:32.500 --> 01:38.660
and in the Hierarchy, just right-click in there and add an empty and call it FlockManager.

18
01:38.680 --> 01:45.070
This is not unlike the other Managers that we've had before for other agents, where we wanted to create

19
01:45.100 --> 01:51.160
multiple versions of them so that we can send messages and keep track of them.

20
01:51.190 --> 01:56.920
In this case, the fish need to know about each other, so the FlockManager is going to create a whole bunch

21
01:56.920 --> 02:05.530
of fish and then be able to tell all of the individuals about the other fish in the flock (or school

22
02:05.530 --> 02:06.940
I guess, but I'm calling it flock).

23
02:07.060 --> 02:14.060
Right, so we've got our FlockManager, which is an 'empty'. Create a C# script and call it FlockManager

24
02:16.310 --> 02:20.090
and then that script will be attached to our FlockManager.

25
02:20.950 --> 02:27.390
Now we're going to open up that script and add in this code. The code is quite simple.

26
02:27.400 --> 02:35.950
It creates an array of fish, instantiates all of those fish objects and puts them at a random location

27
02:36.010 --> 02:40.810
around the location of your FlockManager.

28
02:40.810 --> 02:41.020
Right.

29
02:41.020 --> 02:48.510
So first of all you'll need an exposed variable, public fishPrefab and we pass in our fish prefab

30
02:48.520 --> 02:55.140
here (which can be your green fish or the pink fish or whatever else). You can also do this with cubes, it won't

31
02:55.150 --> 02:56.610
make any difference

32
02:56.740 --> 03:03.220
and then a public numFish that we've got set to 20 (but this will allow you to change the number of

33
03:03.220 --> 03:07.270
fish you've got in a particular flock, through the Inspector).

34
03:07.600 --> 03:12.240
We're then going to create our array of allFish

35
03:12.370 --> 03:17.690
and so as we instantiate them, they're going to go into this allFish and then we have a Vector3

36
03:17.710 --> 03:19.480
called swimLimits.

37
03:19.480 --> 03:27.210
This is the range of locations that the fish can be generated within.

38
03:27.220 --> 03:35.020
So this is like a box that is around your flock manager and it's 5x5x5 which makes it

39
03:35.020 --> 03:38.340
10x10x10 because this is a radius value

40
03:38.650 --> 03:44.790
and then we'll use the swimLimits to generate the location of our fish within that box.

41
03:44.800 --> 03:48.060
So this is all happening down in Start.

42
03:48.160 --> 03:55.130
First of all, we create all of the fish and we make the array big enough to hold them.

43
03:55.180 --> 04:01.540
Then there's the 'for' loop, that loops around that number of times and each time creates a position to

44
04:01.540 --> 04:11.650
put the fish and this position is based on the position of the flock manager plus a random

45
04:11.680 --> 04:18.160
Vector3 value that is around those swimLimits (so between 'minus swimLimits' and 'positive swimLimits') which will

46
04:18.160 --> 04:21.260
give you the range of positions.

47
04:21.280 --> 04:26.980
Now if you want it to be a bigger box of positions, then you would just make these values bigger here or

48
04:26.980 --> 04:30.810
you could make them smaller if you want a tight-knit group.

49
04:30.910 --> 04:37.660
Then, once we've got this position that we've calculated, we then put the fish (we instantiate it from our prefab)

50
04:38.140 --> 04:43.930
at the position that we just calculated, using a neutral rotation.

51
04:43.930 --> 04:47.180
You could also set this to the fish prefab rotation

52
04:47.290 --> 04:51.790
if you've got them pointing in a particular direction, it doesn't really matter

53
04:52.090 --> 04:55.520
and that will fill up this array.

54
04:55.600 --> 05:04.090
So if you save that, flick back into Unity, select your FlockManager and you want to set the fish prefab.

55
05:04.390 --> 05:09.100
The fish prefab is not this green fish that's in the scene.

56
05:09.130 --> 05:09.450
Ok.

57
05:09.460 --> 05:12.530
So that's the one that was used to make the initial prefab.

58
05:12.550 --> 05:20.200
In fact just delete it, so that you aren't confused by it and then grab hold of your fish prefab

59
05:20.200 --> 05:26.440
(which is in your Assets) and drag-and-drop that into the FlockManager.

60
05:26.680 --> 05:31.270
We're going to create 20 fish and it will fill up this array here.

61
05:31.270 --> 05:35.910
So press Play and there's your 20 fish

62
05:36.200 --> 05:41.850
(and if I zoom out, you'll see them) within the confines of the swim limits that we've created.

63
05:41.850 --> 05:51.700
Now if you want more fish than that, let's say you wanted 50 fish...50 in here and press Play and you'll get 50 fish.

64
05:51.700 --> 05:57.690
You can see them, they've all appeared in the Hierarchy and they're all in the Inspector under the

65
05:57.730 --> 05:58.210
allFish array and that's

66
05:58.240 --> 06:03.670
so each fish can find every other fish that's in their flock.

67
06:06.220 --> 06:13.130
Ok, it's time to get the fish moving. To begin, we're going to set up some speed values for the fish and

68
06:13.130 --> 06:19.070
we'll do that in the FlockManager. So we'll make the FlockManager a place where we can set all the

69
06:19.070 --> 06:23.930
different variables for the particular fish that belong to that flock and then you can have

70
06:23.930 --> 06:28.010
multiple flocks for different types of fish in your scene.

71
06:28.010 --> 06:33.860
So I've been up the FlockManager script again. What you're going to do is add in some public values

72
06:34.220 --> 06:35.790
as I've got here.

73
06:35.840 --> 06:42.590
First of all, in the Inspector, I've just added a little 'Header' line, which sections it off and makes

74
06:42.590 --> 06:44.950
it easy to find when you're trying to set them.

75
06:44.960 --> 06:51.280
Then I've got a 'Range' here, between 0.0f and 5.0f which will give you a slider

76
06:51.380 --> 07:01.580
in the Inspector and then public minSpeed and public maxSpeed, with the same values for the range.

77
07:01.960 --> 07:08.790
Once you've added in those 5 lines, just save that and switch back over into Unity and click on the

78
07:08.840 --> 07:16.100
Flock Manager and you'll find in the Inspector that we've now got those settings. We've got 'Fish Settings',

79
07:16.190 --> 07:22.100
Min and Max Speed (which you can then change with the sliders as you like and you'll be able to do that

80
07:22.100 --> 07:29.150
in real-time as well and pass through those speed values to the fish). For the fish, we'll now create a

81
07:29.150 --> 07:33.380
new C# file and call it 'Flock'.

82
07:33.440 --> 07:39.800
This is the code that goes on to the fish prefab, so drag-and-drop that onto your green fish and while we're here,

83
07:40.020 --> 07:43.150
we'll drag-and-drop it onto the pink fish as well.

84
07:43.220 --> 07:50.000
Just select the green fish down in the Assets and check over in the Inspector to make sure that script is actually

85
07:50.000 --> 07:51.230
attached to it.

86
07:51.230 --> 07:58.670
Now open it up and the first thing we'll need is a link back to our manager, so we'll create a

87
07:59.120 --> 08:08.710
'public FlockManager myManager' and then we need a float for the speed.

88
08:08.740 --> 08:17.530
Now in the Start, we're going to set speed to equal Random.Range between the managers minimum speed setting

89
08:17.620 --> 08:20.710
and the managers maximum speed setting. In the Update,

90
08:20.740 --> 08:28.130
we'll put a Translate statement to drive the fish forward along their forward axis or their Z axis.

91
08:28.150 --> 08:32.340
So notice that the value of 'speed' is going into the Z axis.

92
08:32.340 --> 08:35.710
If your fish happen to be facing in a different direction...

93
08:35.710 --> 08:46.680
so if I just go back to my fish and grab a fish, so the blue axis here is facing forward and that's

94
08:46.680 --> 08:48.950
the one that it's going to be pushing it along.

95
08:49.080 --> 08:54.060
If your fish happens to have a different axis, then you'll have to adjust your code to be pushing it along

96
08:54.060 --> 08:55.180
that axis instead.

97
08:55.210 --> 09:03.430
Ok, so we'll delete that fish and go back to the code and just save that, then in your FlockManager

98
09:03.430 --> 09:12.900
script, if we open that up, after you've created a fish inside of that for-loop, you want to set

99
09:13.220 --> 09:17.690
it's FlockManager (or myManager) to this particular FlockManager.

100
09:17.700 --> 09:26.060
So add this code in and now both of those files (the Flock and the FlockManager) will be linked together.

101
09:26.290 --> 09:31.790
Back in the Editor, press Play and you'll see the results of the code.

102
09:31.790 --> 09:34.350
So you've got your fish that are now going forward.

103
09:34.370 --> 09:37.090
They have random speeds on them.

104
09:37.640 --> 09:40.910
Of course, they are going to go out of scene very quickly.

105
09:40.910 --> 09:47.310
You can see that they're moving at different speeds (if you can get in closer). Yep.

106
09:47.460 --> 09:50.500
So they've all got different speeds, but they're all moving along

107
09:50.550 --> 09:59.350
their Z axis. Ok, so now it's time to add in our 'flocking rules'.

108
09:59.490 --> 10:01.970
I mentioned the flocking rules at the start of this section

109
10:01.980 --> 10:04.210
but let's go over them again, in detail.

110
10:04.290 --> 10:10.860
They are: Move towards the average position of the group; Align with the average heading of the group and

111
10:10.860 --> 10:13.200
Avoid crowding other group members.

112
10:15.150 --> 10:20.010
Moving towards the average position of the group involves taking all the individual members positions,

113
10:20.100 --> 10:23.190
adding them together and dividing by the number in the group.

114
10:23.190 --> 10:29.190
We can then work out where an individual is, in relation to the average location and have them turn towards

115
10:29.190 --> 10:32.310
it. To align with the average heading of the group,

116
10:32.310 --> 10:37.220
we add all the headings of the individuals together and divide by the size of the group.

117
10:37.230 --> 10:44.230
This gives an average vector for individuals to try and align their movement. To avoid neighbours,

118
10:44.260 --> 10:46.780
an individual must be aware of where the neighbours

119
10:46.820 --> 10:47.300
are

120
10:47.350 --> 10:49.360
and then turn away from them.

121
10:50.440 --> 10:55.780
Then to calculate a new heading, an individual will add together the vector for moving towards the center

122
10:55.780 --> 11:01.530
of a group, the vector to align with the groups heading and the vector to turn away from a neighbour.

123
11:04.720 --> 11:10.810
Before we start editing the Flock code, we're going to just add a few more values into the

124
11:10.810 --> 11:16.180
FlockManager (ones that you can change at runtime, so that you can see different behaviors in the fish)

125
11:16.690 --> 11:22.090
and these settings for the behaviors will be available to all the fish in that particular

126
11:22.120 --> 11:23.170
flock.

127
11:23.200 --> 11:32.350
So I'm adding in here another Range for something called neighbourDistance and another Range for

128
11:32.350 --> 11:33.550
rotationSpeed.

129
11:33.550 --> 11:40.660
So once the fish start to get locations that they need to move towards, they're going to have to turn

130
11:40.660 --> 11:46.720
around at some point and we'll be using a slerp for that and that needs a rotation speed,

131
11:46.720 --> 11:48.250
so I've added that in there.

132
11:48.260 --> 11:57.580
Now, the other thing with this neighbourDistance is that you can have a flock of something and everything

133
11:57.580 --> 12:00.740
in that flock might be considered your neighbour

134
12:01.560 --> 12:09.600
or you might only want to consider the closest few (within one meter of you) as your

135
12:09.660 --> 12:16.890
actual neighbour, so you can therefore modify that and you get different behaviors out of the entire group

136
12:17.310 --> 12:24.320
when they're only considering those immediately around them as neighbours rather than everybody.

137
12:24.360 --> 12:28.490
Now for the changes to the Flock. Go to your Flock.cs code.

138
12:28.650 --> 12:36.710
We're going to create a new method called 'ApplyRules' and that method is getting called down here

139
12:36.750 --> 12:42.270
so there's no other changes to the top, just inside of the Update

140
12:42.270 --> 12:45.510
and then we add in ApplyRules.

141
12:45.510 --> 12:52.830
Now, this ApplyRules is doing those 3 rules that we just spoke about, with the different vector calculations.

142
12:52.830 --> 13:02.200
First of all it's creating a holder, to get hold of all of the fish in the current flock.

143
13:02.220 --> 13:08.060
So these are the fish that we want to try and find the average position, the average heading etc. and

144
13:08.070 --> 13:13.620
they're the fish that will be considered your neighbours (or at least they're the whole set and the neighbours

145
13:13.620 --> 13:17.240
will be part of that set, as you'll see in a minute).

146
13:17.260 --> 13:23.850
Now we're calculating the average center of the groups, so we're going to store that in 'vcentre'. Then we're

147
13:24.090 --> 13:28.350
calculating the average avoidance vector as well.

148
13:28.350 --> 13:32.090
So it's not just 1 neighbour that we want to avoid,

149
13:32.190 --> 13:33.840
it's EVERYBODY

150
13:33.840 --> 13:35.870
and so that's also an average.

151
13:35.880 --> 13:40.080
So you have to calculate all of the avoidance vectors and then average them out.

152
13:40.080 --> 13:42.030
So initially they're set to 0.

153
13:42.330 --> 13:44.510
Then we have this float called 'gSpeed'

154
13:44.520 --> 13:51.960
and this is a global speed of the group (an average speed that the group

155
13:51.960 --> 13:56.490
is moving in) and we'll use that in our calculations as well.

156
13:56.520 --> 14:00.690
Then we have a float 'nDistance', which is our neighbour distance.

157
14:00.750 --> 14:06.870
So we are going to have to ask each fish how far away they are and then work out if we consider them

158
14:06.870 --> 14:10.480
to be close enough to be in our group

159
14:10.570 --> 14:21.100
and so we have this 'groupSize' counter here, that will count how many of the fish in the flock are in

160
14:21.100 --> 14:29.890
our group, so the group is like this small section of the flock that is within that neighbourDistance

161
14:30.060 --> 14:30.760
value.

162
14:31.750 --> 14:38.560
Now with these values initialized to 0, essentially, we're going to loop through all of the fish

163
14:39.010 --> 14:40.850
in the flock.

164
14:40.930 --> 14:42.600
So we use a foreach loop.

165
14:42.610 --> 14:47.390
Then we're going to ask if the current fish (which is going to be this game object

166
14:47.410 --> 14:48.870
here: go)

167
14:49.000 --> 14:56.080
as long as that's not equal to the fish running this code at this time, then we're going to do the process

168
14:56.080 --> 15:00.280
because you don't need to find the average of yourself, you don't have to calculate the distance

169
15:00.280 --> 15:01.000
to yourself,

170
15:01.000 --> 15:06.640
so this just makes sure we don't calculate fish's data for itself.

171
15:06.640 --> 15:12.760
Then we work out the distance of the neighbour, so remember we're looping around this for-loop, picking out each fish

172
15:13.420 --> 15:21.640
and then we're working out the distance with a Vector3.Distance between the game object in our arrays

173
15:21.640 --> 15:29.800
position and our position in the world and then we're asking if that distance

174
15:29.800 --> 15:36.820
is 'less than or equal to' the neighbourDistance, which makes that a fish of interest (one that we're going

175
15:36.820 --> 15:45.990
to consider part of our close-knit group) then we will add that particular fish's position to the 'vcentre'.

176
15:46.060 --> 15:49.820
vcentre will be our average position of our group,

177
15:50.110 --> 15:54.680
so, therefore, we have to loop around and add all of the positions together

178
15:55.030 --> 15:57.370
and then we divide it by the group size.

179
15:57.430 --> 16:02.050
So at the same time we add it, we also increase the group size by 1.

180
16:02.080 --> 16:06.990
So after this loop has gone through, we can calculate the average.

181
16:07.120 --> 16:14.680
Next, we have an if-statement to test our neighbourDistance being less than a much smaller value, which

182
16:14.680 --> 16:17.870
I've hard-coded here as 1.0

183
16:17.980 --> 16:25.270
and we could, in fact, put this in the FlockManager as another distance value but this is how close

184
16:25.630 --> 16:32.090
we're allowed to be to another fish before we should be considering avoiding it.

185
16:32.110 --> 16:40.840
Now if, in this case, the distance is less than 1.0f, then the avoid average vector is going to be added to

186
16:40.870 --> 16:48.250
and so it has it's own value + the vector away from that fish.

187
16:48.250 --> 16:53.620
So the vector away from something (and we looked at this in a crowd simulation we wanted to move away)

188
16:53.920 --> 16:58.730
was our current position minus the position we want to get away from

189
16:58.960 --> 17:03.150
and so that adds up as we loop through here as well.

190
17:03.160 --> 17:09.860
Now the global speed is adding together all the speeds of the particular fish from the flock.

191
17:09.910 --> 17:16.450
So we go ahead and grab hold of the Flock code that's attached to the fish that we're looking at at

192
17:16.450 --> 17:23.620
the moment and then we grab its speed and add it to this gSpeed value.

193
17:23.650 --> 17:29.500
Now by the time the code finishes this foreach loop, we've looped through every fish in the flock and

194
17:29.500 --> 17:38.770
we've examined it. If it's considered to be a close neighbour, then we're finding its position and also

195
17:38.890 --> 17:42.570
its avoidance vector as well and adding all of that up.

196
17:42.620 --> 17:47.170
Oh and of course the speed. Now underneath the foreach loop,

197
17:47.260 --> 17:55.360
we have an if-statement. The if-statement is asking if the group size is > 0. If the fish

198
17:55.750 --> 18:01.920
is moving along and there is no one around it and the group size becomes 0,

199
18:02.260 --> 18:06.250
it's not going to apply these rules. It's just going to keep moving in a straight line.

200
18:06.280 --> 18:14.230
So if this fish IS influenced by another fish (or another group of fish), we then find the average to the

201
18:14.230 --> 18:14.700
center.

202
18:14.710 --> 18:20.130
So that's the 'vcentre' vector that we calculated when we added them all together,

203
18:20.290 --> 18:28.540
now we divide by the group size. The speed itself of the fish is set to the global speed,

204
18:28.540 --> 18:30.700
divided by the group size.

205
18:30.850 --> 18:37.750
With that information, we can work out the direction that the fish needs to travel in (or the direction

206
18:37.750 --> 18:40.840
the fish wants to travel in).

207
18:40.900 --> 18:51.200
So its 'direction' is equal to the 'vcentre array' + 'vavoid array' - our current position.

208
18:51.200 --> 18:56.730
Now if that means that direction is 0,

209
18:56.770 --> 18:58.700
we're already facing in the right direction.

210
18:58.810 --> 19:02.480
If it's not equal to zero then we need to rotate

211
19:02.500 --> 19:09.310
and so we do a transform.rotation and we use our Slerp that we've used before for other NPCs and

212
19:09.310 --> 19:14.040
we do a little rotation according to our rotation speed

213
19:14.170 --> 19:19.860
and what this will do is slowly turn the fish towards the direction it needs to. It's not going to snap

214
19:19.870 --> 19:21.830
right around to the perfect direction

215
19:22.000 --> 19:27.970
and it's this slerp (and slerping a little bit by little bit) and constantly updating your position and

216
19:27.970 --> 19:36.120
checking where you are with respect to all of the other fish, that it causes this flocking to occur,

217
19:36.190 --> 19:43.150
as you're about to see. Time on this video is about to run out, so quickly we'll just press Play (make

218
19:43.150 --> 19:49.030
sure that in your FlockManager you do give a Neighbour Distance and a Rotation Speed, otherwise

219
19:49.030 --> 19:51.500
you're not going to get any rotation at all

220
19:51.640 --> 19:54.560
and then you'll start to see the flocking occurring.

221
19:54.580 --> 19:57.440
We'll investigate this more in the next video.