WEBVTT

00:02.690 --> 00:09.680
In the previous video, we finished off our flocking and added a little bit of randomness into the speed.

00:09.680 --> 00:18.230
And also when the rules get applied in this video, we're going to set up the boundaries of the tank.

00:18.230 --> 00:25.040
So we've actually got the boundaries of where the fish can swim defined back in our flock manager up

00:25.040 --> 00:27.860
here in our swim limits.

00:27.860 --> 00:33.080
So let's go into the flock and add that functionality in for each fish.

00:33.080 --> 00:40.430
So at the top, we're going to create a bull called Turning and we're going to set that to false.

00:40.670 --> 00:48.830
This bull is going to be turned on or set to true when a fish hits the outer boundary of where it can

00:48.830 --> 00:54.800
swim, and then it's actually turning around when the fish is turning around from the outer edge, we

00:54.800 --> 01:01.580
don't want it to go into flocking behavior because that will just confuse the direction that it's going

01:01.580 --> 01:02.090
in.

01:02.270 --> 01:02.660
All right.

01:02.660 --> 01:08.300
So we're going to put the test for our boundary inside of the update.

01:08.300 --> 01:13.070
So up the top here, let's add in a bounce.

01:13.970 --> 01:19.730
B, now, a, bounds, if you're not familiar with that, is a unity construct, which is basically

01:19.730 --> 01:26.300
a like a collision box, essentially, and it's the bounds of that particular rectangular prism.

01:26.300 --> 01:32.270
So we're going to create a new bounds based on the size of the swimming space that we've set up.

01:32.270 --> 01:36.200
So bounds and we have to give it a position.

01:36.200 --> 01:41.660
So it's going to be at the flock manager dot f m.

01:43.110 --> 01:59.910
Dot transform dot position and it's going to be your size of flock manager f m dot swim limits multiplied

01:59.910 --> 02:01.800
by two.

02:02.130 --> 02:10.980
So we're making it the dimensions actually twice the size of these swim limits to essentially keep the

02:10.980 --> 02:13.230
fish within there when they actually spawn.

02:13.230 --> 02:13.490
Okay.

02:13.560 --> 02:17.670
So when we first create them, they're inside swim limits around the center.

02:17.670 --> 02:19.860
But this is the box that's a little bit bigger.

02:19.860 --> 02:27.060
So the swim limits essentially that we're setting are like a radius value from the center out to an

02:27.060 --> 02:27.720
edge.

02:27.960 --> 02:28.410
Okay.

02:28.410 --> 02:36.690
So with that done, we're then going to go if not B, so they are bounced dot contains

02:38.700 --> 02:43.710
transformed position, then we're going to say turning.

02:44.040 --> 02:45.810
It's going to be set to true.

02:47.010 --> 02:48.810
So bounce.

02:49.540 --> 02:52.630
DOT contains tests for the Fishers location.

02:52.630 --> 02:59.170
If the Fishers location is outside of the bounding box, then it will be a knot and that will set turning

02:59.170 --> 03:02.530
to true, which means we're actually going to go through a turning.

03:02.860 --> 03:05.320
Else we will set it back to false.

03:05.320 --> 03:08.100
So turning equals false.

03:08.110 --> 03:15.640
And this will occur when the fish enters the bounding box again, or while the fish is still in the

03:15.640 --> 03:16.660
bounding box.

03:16.900 --> 03:22.330
Now, if turning is true, we want to get the fish to actually turn around.

03:22.330 --> 03:28.240
So if turning, then we're go to a vector.

03:28.690 --> 03:41.290
Three direction equals flock manager dot f m dot transformed position.

03:43.550 --> 03:46.790
Linus transform position.

03:46.790 --> 03:53.450
So we're working out the vector that the fish has to go to get back to the center of the flock manager,

03:53.990 --> 03:56.780
which is basically the origin of the flock manager.

03:56.780 --> 04:01.280
So we're just turning around back to the center, which is going to get the fish back inside of that

04:01.280 --> 04:02.330
bounding box.

04:02.780 --> 04:10.220
And then we'll set the transform rotation for the fish based on a slurp like we're doing when we're

04:10.220 --> 04:11.330
actually flocking.

04:11.330 --> 04:14.900
Because again, we don't want to move the fish with any of this code.

04:14.900 --> 04:21.530
We just want to change the direction it's facing because its movement happens when we do this transform

04:21.530 --> 04:23.390
down here with the translate.

04:23.540 --> 04:36.200
Okay, so we're slurping around transform rotation, turn in dot look rotation towards the direction

04:36.410 --> 04:48.530
and then we're rotating based on our rotation speed which is in the flock manager dot FM dot rotation

04:48.530 --> 04:52.370
speed multiplied by time, delta time.

04:52.670 --> 04:53.510
Okay.

04:53.660 --> 04:56.780
Now if we're turning, we're doing this.

04:56.780 --> 05:02.270
If we're not turning, we want to do this other stuff down here, which is apply the rules and check

05:02.270 --> 05:02.840
our speed.

05:02.840 --> 05:11.540
So we'll put an else and then all of that can go inside of that else so that we don't apply the rules

05:11.540 --> 05:13.790
and turn in the same breath.

05:13.790 --> 05:23.990
Okay, so let's save this and we can go back into unity and have a look at our new flock and our turning.

05:23.990 --> 05:26.030
So let's press play.

05:26.180 --> 05:30.230
And I'm going to go back into the scene where we can actually see our fish.

05:30.470 --> 05:32.600
We're going to select the flock manager.

05:32.600 --> 05:41.990
Now, I will turn my neighbor distance right up and we can now watch the fish as.

05:42.780 --> 05:44.390
They go about their flocking.

05:44.400 --> 05:49.920
Now, if we turn that neighbor distance down to like one, then they're sort of independently going

05:49.920 --> 05:50.880
to swim off.

05:50.880 --> 05:52.710
But they should hit the limits.

05:52.710 --> 05:57.630
And you're seeing it there when they hit the outside edge of that bounding box, like watching these

05:57.630 --> 06:02.790
guys here and they turn around and come back into the center.

06:03.240 --> 06:11.040
So this means that you're not going to lose a fish, essentially, and that they will stay in there

06:11.040 --> 06:13.760
and not go off into infinity, basically.

06:13.770 --> 06:14.250
Okay.

06:14.250 --> 06:18.840
So that is a nice simple flocking algorithm.

06:18.840 --> 06:25.950
And you, of course, can add in your pink fish if you want and make a very colorful looking tank.
