WEBVTT

00:06.620 --> 00:13.640
In the previous lecture, we created our very simple flocking fish simulation here.

00:13.730 --> 00:21.230
Now, if we in fact change the neighbour distance and put it down to like zero, notice the fish don't

00:21.230 --> 00:23.030
flock, they just all go out.

00:23.060 --> 00:29.750
Now, if I set it quickly back up to ten and then I go looking for my fish, you can see there's some

00:29.750 --> 00:36.090
over here that have reef locked together and there might be others.

00:36.110 --> 00:37.400
It depends how close they are.

00:37.400 --> 00:38.480
There's a group down here.

00:38.480 --> 00:41.930
I don't know if you can see those as well.

00:41.930 --> 00:49.430
But the thing is with that is that their neighbor, the distance is going to be dependent on obviously

00:49.430 --> 00:50.690
the distance between each group.

00:50.690 --> 00:55.970
So while you've got a big group that can be going together, if they separate, then you can have little

00:55.970 --> 00:59.390
groups because they're still within a certain distance of each other.

00:59.540 --> 01:06.230
Now, a few things we've got here is we have a speed that is just out of control because it just keeps

01:06.230 --> 01:09.830
averaging and goes up to the maximum speed here.

01:09.830 --> 01:14.990
So we can actually, if we want, sort of more relaxed fish, kind of turn that down a bit.

01:14.990 --> 01:19.190
We're also going to add a bit of variety to the speed in a moment.

01:19.580 --> 01:25.730
So here we are and now our fish are going a lot slower.

01:26.960 --> 01:29.300
So see if I can do that again for you.

01:29.300 --> 01:34.250
It's just drop that neighbor distance, swim apart, turn it back up again.

01:34.820 --> 01:36.500
Note they will still within ten.

01:36.500 --> 01:39.230
Let them go out a bit, bring it back up again.

01:39.230 --> 01:44.300
And now you can see a flock has formed over here, one over here, and I would assume one over here

01:44.300 --> 01:46.820
because that's where they went.

01:47.300 --> 01:55.880
Okay, so let's start this lecture by adding in a goal location that you can get the fish to swim towards

01:55.880 --> 01:57.920
while they're flocking.

01:57.920 --> 02:02.930
So, for example, let's say there was some food that came into their environment.

02:02.930 --> 02:09.170
They would all want to go towards the food, but they'd also have to stay in a flocking formation.

02:09.170 --> 02:16.880
So let's open up our flock manager to put the value for our goal location in here.

02:16.910 --> 02:30.230
Okay, so let's create up the top a public vector three four goal pose and we'll equal vector 3.0.

02:30.800 --> 02:36.200
And initially we will set it down in the start to goal.

02:36.470 --> 02:42.170
Pause equals this dot transform dot.

02:42.940 --> 02:43.570
Position.

02:43.570 --> 02:47.110
So it'll be the position of the flock manager to begin with.

02:47.200 --> 02:51.970
Then in the update, we'll actually move that around randomly.

02:51.970 --> 03:01.660
So let's put if random range between zero and 100 is less than ten.

03:01.960 --> 03:06.130
So sort of a 10% chance that we will then change it.

03:06.130 --> 03:12.940
If you want it to change less often, you can increase this number here, like to a thousand or you

03:12.940 --> 03:15.280
can make it smaller if you want it to happen often.

03:15.280 --> 03:22.150
So we want goal pause equals and I'm going to use this value here that we're using for our fish, which

03:22.150 --> 03:27.250
is to make sure that we keep the goal pause inside of the swim limits.

03:27.250 --> 03:30.430
So let's copy that and put that down in here.

03:30.530 --> 03:31.990
Okay, so that's our goal.

03:31.990 --> 03:40.870
Pause now changing and we have to figure out how to then use this and integrate it into the code we've

03:40.870 --> 03:41.590
got in the flock.

03:41.590 --> 03:45.520
So let's just save that and switch into our flock code.

03:46.060 --> 03:55.000
And in our calculation for the center of our group, we're going to add in a vector to indicate the

03:55.000 --> 03:58.360
direction towards the food or the goal location.

03:58.360 --> 04:08.530
So flock manager dot FM goal pose minus this dot transform dot position.

04:08.680 --> 04:09.430
Okay.

04:09.430 --> 04:22.510
So the other thing I also want to do in here is within the update for the fish is to randomly reset

04:22.510 --> 04:23.320
the speed.

04:23.320 --> 04:28.510
So this is actually setting the speed, but we want to randomly reset the speed before applying the

04:28.510 --> 04:29.140
rules.

04:29.140 --> 04:31.120
So I'm going to leave this as a challenge for you.

04:31.120 --> 04:37.360
We've already randomly set the goal position in the flock manager, so using that kind of concept,

04:37.390 --> 04:42.340
you can apply it in here with the setting of speed.

04:42.370 --> 04:42.820
Okay.

04:42.820 --> 04:47.980
So pause the video now and when we come back, I'll go through that with you.

04:49.120 --> 04:49.630
Okay.

04:49.630 --> 05:01.570
So in the update, we're going to go if random dot range between zero and 100 is a less than ten, same

05:01.570 --> 05:02.710
sort of chance.

05:03.070 --> 05:06.820
Then what we'll do is we'll reset the speed.

05:06.820 --> 05:10.420
So we'll copy that and we'll put that down into that.

05:10.420 --> 05:10.780
Okay.

05:10.780 --> 05:11.830
So we'll save the flock.

05:11.830 --> 05:15.460
Let's have a look at this changing goal.

05:15.640 --> 05:24.100
So select your flock manager and I'm going to leave the neighbor distance at full.

05:24.460 --> 05:24.620
Okay.

05:24.730 --> 05:29.320
So you can actually see the whole flock moving together towards the goal, right?

05:29.320 --> 05:33.700
So press play and we will see those fish.

05:33.700 --> 05:35.860
Let's go in the scene where we can actually see it.

05:36.370 --> 05:36.760
Right.

05:36.760 --> 05:46.180
So we've got the goal position and you saw it just changing then and it's changing quite rapidly.

05:46.180 --> 05:52.660
Let's come in a bit closer here because you can see that they are going to one area and they circle

05:52.660 --> 05:59.350
it for a little bit of time and then they pop out to another area where the goal is and circle that.

05:59.440 --> 06:03.670
So we have some really interesting and nice behaviors in here.

06:08.490 --> 06:11.070
They're still going super fast, aren't they?

06:12.150 --> 06:17.970
So I'm just going to come back into my flock script, want to make sure that this speed doesn't get

06:17.970 --> 06:26.580
out of control where we're setting it down here so we could go something like under here if speed becomes

06:26.580 --> 06:32.280
greater than the flock manager dot, FM dot.

06:34.080 --> 06:35.820
Max speed.

06:35.850 --> 06:44.810
Then we will just set speed to equal the max speed, and then you can actually control it right in there.

06:44.820 --> 06:53.490
And if you also want to reduce the load on the system, as well as let the fish sort of swim in sort

06:53.490 --> 06:58.140
of the forward direction for a little bit longer, rather than constantly calculating this, you can

06:58.140 --> 07:03.330
also apply a randomness around the apply rule.

07:03.330 --> 07:06.300
So they're not doing it every single update.

07:06.300 --> 07:13.710
And the more and more fish you add, you'll notice there's quite a large load on them calculating their

07:13.920 --> 07:15.210
current flocking rule.

07:15.210 --> 07:23.070
So if we go something like this and then again you can play around with these positions here, we will

07:23.070 --> 07:29.580
get the fish swimming for a little bit longer in a straight line and turning, which won't have them

07:29.580 --> 07:33.150
sort of swimming around and round and round in circles, as you've seen before.

07:33.440 --> 07:34.860
Okay, so save that.

07:34.860 --> 07:37.290
Let's have a look at that behaviour now.

07:38.400 --> 07:38.790
Okay.

07:38.790 --> 07:39.930
So here they are.

07:39.960 --> 07:45.570
See how they go straight line for just a little bit longer before they bother to turn towards their

07:45.570 --> 07:46.410
neighbors.

07:46.500 --> 07:52.050
And that speed's also going to really help with keeping them kind of under control.

07:52.050 --> 07:56.370
And this looks a little bit more natural than than them whipping around in a circle.

07:56.370 --> 07:59.940
And the goal position is still moving.

08:02.480 --> 08:04.520
So you've got your basic rules.

08:04.520 --> 08:10.670
And as you see, we're kind of tweaking the simulation along as we want to add sort of different functionality.

08:10.670 --> 08:14.630
So never be afraid of just trying out different things.

08:14.630 --> 08:21.200
Just because the flocking system has three sets of rules that belong to it doesn't mean you can't go

08:21.200 --> 08:25.070
and add something else if you want slightly different behavior.

08:25.070 --> 08:31.100
And it's just a matter of kind of experimenting to see, if you like, the effect that you get out of

08:31.100 --> 08:31.550
it.

08:32.600 --> 08:40.430
In the next video, we will come back and I will show you how to contain the fish within their swimming

08:40.430 --> 08:46.790
limits so that if they're not flocking, then they don't just swan forever and ever in a day in the

08:46.790 --> 08:47.300
one direction.

08:47.300 --> 08:52.730
They actually hit the edge of the tank or pond or whatever it is, and they come back into the scene.
