WEBVTT

0
00:11.320 --> 00:17.750
Welcome back to the second part on Flocking. What you can see on the screen is the program we left off

1
00:17.750 --> 00:19.730
with last time.

2
00:19.730 --> 00:27.170
So our fish are flocking and they're flocking according to some settings we've got on our FlockManager.

3
00:27.830 --> 00:29.690
The minimum and maximum speeds are

4
00:29.780 --> 00:34.850
the initial speeds of the fish and they do influence but you can't change them once it's running

5
00:34.880 --> 00:37.580
and we'll work on that a little bit later,

6
00:37.760 --> 00:42.640
but as far as the Neighbour Distance goes, you can modify that at runtime.

7
00:42.640 --> 00:49.340
So if I turn the Neighbour Distance down, we should see that our flock will start to separate because there's

8
00:49.340 --> 00:51.160
no need to flock anymore

9
00:51.310 --> 00:53.390
but turn that back up again.

10
00:53.720 --> 00:58.000
It doesn't bring all the fish necessarily back together,

11
00:58.040 --> 01:05.300
just the ones within distance of each other and you can see in this example here that initially everybody

12
01:05.300 --> 01:11.280
was in the same flock, but I released them to move out in their own way

13
01:11.290 --> 01:16.420
and then I brought them back and said "Oh no, you actually are in a flock, you want to be a distance of

14
01:16.420 --> 01:21.920
10" and because they'd moved away from each other, they found the fish that were in a distance of 10 and now

15
01:21.940 --> 01:25.570
you've ended up with 2 little flocks within that.

16
01:25.600 --> 01:28.620
Now the Rotation Speed will also affect it.

17
01:29.430 --> 01:31.470
Actually, you just saw a nice emergent behavior

18
01:31.470 --> 01:37.440
there, where both the flocks joined together because they're not just in a stationary position and if you

19
01:37.440 --> 01:44.400
look over here on my main camera view of the game, this flock is slowly moving across the screen because

20
01:44.730 --> 01:49.810
they do have a particular heading, even if they don't really look like it here.

21
01:49.980 --> 01:56.870
If we slow down the Rotation Speed, we might get a better idea of which way they're trying to travel.

22
01:56.920 --> 02:03.640
So remember they're trying to go in a particular direction with the entire flock but they're also trying

23
02:03.640 --> 02:08.640
to stay together, so they're going to be circling around each other whilst they're moving along.

24
02:08.650 --> 02:10.530
Now this isn't the final product.

25
02:10.540 --> 02:17.140
This is just to play around with for now and it really does show you the effects of flocking from that

26
02:17.200 --> 02:23.170
little, tiny bit of code that we wrote with those 3 different averaged vectors in it.

27
02:23.200 --> 02:31.980
Right, so now that we've got this, let's go back and improve it. In the FlockManager, we'll add a 'goal position'.

28
02:31.980 --> 02:37.270
So at the top of the FlockManager, let's add in a

29
02:39.920 --> 02:48.810
'public Vector3 goalPos;' then to get that goalPos just to be somewhere to start with,

30
02:48.810 --> 02:54.730
down in the Start, we will put it where the FlockManager is.

31
02:54.730 --> 02:57.520
So in this case it will be:

32
02:57.520 --> 03:08.760
'goalPos = this.transform.position;'. Right, so that is creating a goal position for our fish to head

33
03:08.760 --> 03:10.180
towards.

34
03:10.200 --> 03:19.290
Save that. Now we need to modify the ApplyRules behavior to include that goal position.

35
03:19.320 --> 03:27.900
So go back to your Flock code and you want to find where you're working out where the center is, inside

36
03:27.900 --> 03:28.360
of this

37
03:28.380 --> 03:36.660
'if(groupSize'...so this is inside the ApplyRules and we're going to modify this vector towards the center

38
03:36.660 --> 03:43.220
of the group. We're going to make that center move towards the goalPos as well.

39
03:43.290 --> 03:50.520
So to the end of calculating the average of the position, we'll add a vector to the goalpos, so the

40
03:50.520 --> 03:57.030
Vector2, our goal is the goal position minus the current fish's position.

41
03:57.210 --> 04:04.620
Save that and let's go back into Unity and run and have a look at what we've got. Now when we press Play,

42
04:04.620 --> 04:10.290
our fish have a goal position but they're not going to go to that position if they can't turn around, so the Rotation

43
04:10.290 --> 04:16.830
Speed on the FlockManager needs to be increased and you'll want to set the Neighbour Distance as well because

44
04:16.830 --> 04:23.340
remember if they're not part of a group, then they're not going to go anywhere except straight ahead.

45
04:23.340 --> 04:28.860
Now you can see that we've lost some fish up in the distance here because we didn't capture them within

46
04:28.860 --> 04:34.110
the Neighbor Distance in time and so they don't even consider themselves to have neighbours and they're

47
04:34.110 --> 04:39.750
just off into the distance and that's fine because that's the kind of emergent behavior that you're after and

48
04:39.750 --> 04:46.560
you can see I've got the FlockManager selected and you can see it's position if you double-click on it and

49
04:46.560 --> 04:54.270
in the Scene, it's position is shown just there and you can see them flocking around that actual position.

50
04:54.270 --> 04:58.330
Now what we want to do is make them flock around it as it moves.

51
04:58.330 --> 05:02.380
Now if I move it at the moment, they're still going to flock around

52
05:02.410 --> 05:02.940
goalPos.

53
05:02.940 --> 05:08.520
We're not updating goalPos to be updated with the center of the FlockManager,

54
05:08.650 --> 05:14.090
therefore you'll have to update that goalPos in the Update on FlockManager.

55
05:14.160 --> 05:15.810
So let's quickly do that.

56
05:16.020 --> 05:24.210
Open up the FlockManager code and we can grab this piece of code that sets the goalPos, copy it and

57
05:24.210 --> 05:34.440
paste it down into the Update. Just save that and go back into Unity and press Play and make sure (I really

58
05:34.440 --> 05:41.370
do need to set these rotations and distances to something other than 0 straight away).

59
05:41.370 --> 05:46.400
Ok, so here's our flock and they're moving around that center position

60
05:47.010 --> 05:54.600
and if I grab hold of it and move it over here, they start to move over to their goal and

61
05:54.760 --> 05:58.670
if I move it back here, they'll move back again.

62
06:07.590 --> 06:08.020
Cool.

63
06:08.110 --> 06:08.520
Ok.

64
06:08.530 --> 06:15.930
So let's (first of all, I'm going to set my Neighbour Distance and my Rotation Speed, so I don't keep having to change that

65
06:15.930 --> 06:17.830
every time I press play)

66
06:17.830 --> 06:21.160
and let's go and make some more modifications.

67
06:21.160 --> 06:23.690
First of all let's go into the FlockManager

68
06:24.130 --> 06:33.000
and what I want to do (instead of setting this goalPos here to the position of the FlockManager, which

69
06:33.000 --> 06:34.260
you can do)

70
06:34.450 --> 06:44.270
I wanted to randomly pick a new goal location which is inside of this range that we've set up for our

71
06:44.300 --> 06:53.430
swimLimits here. So we can do the same thing with goalPos as we have for 'pos'.

72
06:53.710 --> 07:02.720
So let's just grab this piece of code, copy it and just set it down here to our goalPos.

73
07:02.960 --> 07:13.400
Now, this is going to update goalPos every Update, which is probably not what you want (get rid of that

74
07:13.420 --> 07:14.200
semi-colon on the end)

75
07:14.250 --> 07:25.470
so we can do something like this to make it happen randomly...'if (Random.Range(0, 100)

76
07:25.890 --> 07:28.900
is < 10

77
07:29.040 --> 07:36.330
gives us a 10% chance that the next line is going to run and update our goal position.

78
07:36.750 --> 07:41.050
So save that and let's go back into Unity and press Play.

79
07:43.360 --> 07:51.620
Now we'll be able to see the goalPos changing and the fish moving around to us.

80
07:51.730 --> 07:57.400
There you go and you can see that there. We can also do a similar thing in the Flock script, where we're not

81
07:57.400 --> 08:06.700
applying the rules all the time and we're also at random moments resetting the speed. In Flock.cs,

82
08:06.760 --> 08:13.580
in the Update, add these 2 lines here and you can play around with the percentage chance of the

83
08:13.580 --> 08:21.850
Random.Range lines executing and you can set speed here, using the same settings that you've got up in Start

84
08:21.850 --> 08:29.560
so that you can reset the speed of the fish and then you can also only apply the rules every now and

85
08:29.560 --> 08:29.770
then.

86
08:29.770 --> 08:37.580
So this is like every 20% (or applying the rules 20 out of 100 times).

87
08:37.990 --> 08:45.850
What this also does is to take away a lot of the load from constantly calculating the rules over and

88
08:45.850 --> 08:48.660
over and over again, for every single fish

89
08:48.880 --> 08:51.190
because as your flocks get much larger,

90
08:51.190 --> 08:57.430
it will be just way too much processing them to have them do the rules and they don't need to check

91
08:57.730 --> 08:58.720
all of the time,

92
08:58.720 --> 09:00.460
so this does it randomly.

93
09:00.520 --> 09:08.830
You could, of course, have an InvokeRepeating type method that you include in here, that you could apply

94
09:08.830 --> 09:16.660
the rules every 1 second or 2 seconds. I like random because it gives you unexpected results.

95
09:16.660 --> 09:24.580
Ok, so if you add that in (above the Translate) and save, go back into Unity and press Play.

96
09:26.280 --> 09:32.910
You're going to find that with the FlockManager, you can now increase the maximum speed and speed up

97
09:32.910 --> 09:34.320
the fish.

98
09:34.320 --> 09:40.190
You may also need to increase your Neighbour Distance because they might just disappear.

99
09:40.230 --> 09:41.460
Let's just slow them down.

100
09:41.460 --> 09:46.670
The other thing that will keep them together into a nice tight-knit group is the Rotation Speed

101
09:46.960 --> 09:49.840
The faster that they can turn around,

102
09:49.860 --> 09:57.180
then the more compact the flock will be, as you can see there

103
09:57.340 --> 10:04.510
and then if you wanted them to lose interest in each other, then you could turn the Neighbour Distance down so

104
10:04.510 --> 10:11.440
that they then start to form little tiny flocks within the bigger flock but they're all going for the

105
10:11.440 --> 10:17.020
same direction and we could see before, there's a few. There's one fish over there that's heading off

106
10:17.530 --> 10:21.590
into the distance, so he has fallen outside of our logic...

107
10:21.760 --> 10:28.030
and there's another one over there that's off as well and you will lose members of your flock because

108
10:28.330 --> 10:31.210
at some point the rules just won't apply to them.

109
10:31.360 --> 10:37.180
If you do end up with game objects that swim off into the distance, you will have to have some way of

110
10:37.690 --> 10:45.970
deleting them once they've gone. So you might have an OnBecameInvisible on the actual model itself

111
10:45.970 --> 10:48.660
so that it will automatically destroy itself,

112
10:48.700 --> 10:53.820
otherwise, you can control them by keeping them within a certain region.

113
10:53.920 --> 11:02.560
Now, we've already created a region on our flock because we've got that structure for the swim limits.

114
11:02.560 --> 11:09.850
So this vector here at the top is where we're creating our fish but we could also use it to contain them

115
11:10.510 --> 11:14.640
and that means that the fish have to leave

116
11:14.650 --> 11:20.620
the logic of the rules and have a little bit of separate logic which will turn them around if they've

117
11:20.620 --> 11:22.170
gone too far.

118
11:22.180 --> 11:32.320
So back in Flock.cs, first of all, at the top we'll create a boolean value and call it 'turning' and set

119
11:32.320 --> 11:34.560
it to 'false'.

120
11:34.570 --> 11:37.230
Now we will turn this to 'true'

121
11:37.420 --> 11:44.680
when the fish reaches the swim limits. Before we start modifying the fish's speed or applying the rules

122
11:44.680 --> 11:51.190
and moving it around, inside of the Update method we're going to turn the fish around if it has reached

123
11:51.430 --> 11:59.660
that boundary, so just as the first thing inside of Update, we'll add this code here.

124
11:59.770 --> 12:07.990
We're going to create a new 'Bounds'. So a 'Bounds' is like a rectangular prism that you specify around a certain

125
12:07.990 --> 12:09.160
center point.

126
12:09.190 --> 12:13.900
In this case, it's the myManager transform position

127
12:13.900 --> 12:20.680
and for a certain size. So it's 'myManager.swimLimits * 2' because our swimLimits are essentially

128
12:20.680 --> 12:28.450
like a radius type (a half-size value) that we've put in there. This will create a nice big box (although it doesn't

129
12:28.450 --> 12:34.190
create a big box, it just takes a structure that's holding the size of a box that would be around the manager)

130
12:34.510 --> 12:43.180
and we can then use it down here in this if-statement, we can test if our fish (which is transform.position

131
12:43.240 --> 12:44.090
in this case)

132
12:44.120 --> 12:52.850
if our fish's position is inside of the bounds. This is a really nice method, this 'Bounds.contains'

133
12:52.860 --> 13:00.730
and if it's NOT contained in there, then start turning,

134
13:00.820 --> 13:03.480
otherwise, we're not turning.

135
13:03.480 --> 13:11.200
So we're not in turning mode and we can go back and do our rules. We'll then test (before we do anything

136
13:11.290 --> 13:18.340
else) if we're turning or not. If we ARE turning, we're going to calculate a new direction to turn toward.

137
13:18.350 --> 13:24.360
So rather than the ApplyRules (which gives our fish another direction) we'll create another one

138
13:24.370 --> 13:32.830
and in this case, we're just going to turn from our current position to the center of the cube, so we're

139
13:32.830 --> 13:40.780
working out the direction to get from where we are (which is on the 'out of bounds') back to the center

140
13:40.990 --> 13:45.220
and then we're using that direction in another slerp.

141
13:45.220 --> 13:52.930
So again we're slerping through our rotation to look in that particular direction, using our rotational

142
13:52.930 --> 13:53.920
speed.

143
13:53.920 --> 14:00.550
Now if we're NOT turning, then we want to do these other things about applying the rules, so we'll just

144
14:00.550 --> 14:02.270
put an 'else' in here

145
14:02.500 --> 14:10.350
and open that up. Put these things about speed and applying the rules inside

146
14:10.360 --> 14:15.170
there, so that they only occur when we're not turning.

147
14:15.170 --> 14:16.190
Right, so save that

148
14:16.210 --> 14:19.420
and let's go back and run it.

149
14:21.820 --> 14:22.080
All right.

150
14:22.090 --> 14:28.260
So I'm going to go to my FlockManager and I'm going to turn down the Neighbour Distance, so that the fish

151
14:28.260 --> 14:31.000
will go and do their own thing.

152
14:31.140 --> 14:36.220
Let's see if we can get them turning around...ok.

153
14:36.240 --> 14:37.240
So you can see them there.

154
14:37.240 --> 14:43.590
They're reaching the outer bounds over here and they're turning around and they'll do it whenever they

155
14:43.590 --> 14:47.100
reach any of the outer bounds...that fish did and that fish did.

156
14:47.100 --> 14:49.220
So they stay within

157
14:49.250 --> 14:50.850
what is like a fish tank

158
14:50.880 --> 14:57.000
and you can make that as big as you like, knowing that your fish are going to stay put and you're not

159
14:57.000 --> 14:58.990
going to lose any.

160
14:59.160 --> 15:07.710
Within that environment, you can also have little tiny flocks and so let's increase our Neighbour Distance

161
15:07.710 --> 15:14.040
and our Rotation Speed and our Max Speed

162
15:14.210 --> 15:20.360
and so here's a bit of a flock going on but there's other fish who've swum off over here and they'll

163
15:20.360 --> 15:25.040
hit the edge of the tank and then turn around and come back to the center. While they're on their way

164
15:25.040 --> 15:27.110
back to the center of the

165
15:27.110 --> 15:33.560
FlockManager, they may encounter other fish within the correct Neighbour Distance and so they'll start automatically

166
15:33.560 --> 15:35.560
flocking with them.

167
15:35.750 --> 15:41.700
Now this works really nicely until you put obstacles inside of your tank,

168
15:41.750 --> 15:45.700
so in the next lecture, we'll have a look at how to deal with that.