In the previous video, we finished off our flocking and added a little bit of randomness into the speed.
And also when the rules get applied in this video, we're going to set up the boundaries of the tank.
So we've actually got the boundaries of where the fish can swim defined back in our flock manager up
here in our swim limits.
So let's go into the flock and add that functionality in for each fish.
So at the top, we're going to create a bull called Turning and we're going to set that to false.
This bull is going to be turned on or set to true when a fish hits the outer boundary of where it can
swim, and then it's actually turning around when the fish is turning around from the outer edge, we
don't want it to go into flocking behavior because that will just confuse the direction that it's going
in.
All right.
So we're going to put the test for our boundary inside of the update.
So up the top here, let's add in a bounce.
B, now, a, bounds, if you're not familiar with that, is a unity construct, which is basically
a like a collision box, essentially, and it's the bounds of that particular rectangular prism.
So we're going to create a new bounds based on the size of the swimming space that we've set up.
So bounds and we have to give it a position.
So it's going to be at the flock manager dot f m.
Dot transform dot position and it's going to be your size of flock manager f m dot swim limits multiplied
by two.
So we're making it the dimensions actually twice the size of these swim limits to essentially keep the
fish within there when they actually spawn.
Okay.
So when we first create them, they're inside swim limits around the center.
But this is the box that's a little bit bigger.
So the swim limits essentially that we're setting are like a radius value from the center out to an
edge.
Okay.
So with that done, we're then going to go if not B, so they are bounced dot contains
transformed position, then we're going to say turning.
It's going to be set to true.
So bounce.
DOT contains tests for the Fishers location.
If the Fishers location is outside of the bounding box, then it will be a knot and that will set turning
to true, which means we're actually going to go through a turning.
Else we will set it back to false.
So turning equals false.
And this will occur when the fish enters the bounding box again, or while the fish is still in the
bounding box.
Now, if turning is true, we want to get the fish to actually turn around.
So if turning, then we're go to a vector.
Three direction equals flock manager dot f m dot transformed position.
Linus transform position.
So we're working out the vector that the fish has to go to get back to the center of the flock manager,
which is basically the origin of the flock manager.
So we're just turning around back to the center, which is going to get the fish back inside of that
bounding box.
And then we'll set the transform rotation for the fish based on a slurp like we're doing when we're
actually flocking.
Because again, we don't want to move the fish with any of this code.
We just want to change the direction it's facing because its movement happens when we do this transform
down here with the translate.
Okay, so we're slurping around transform rotation, turn in dot look rotation towards the direction
and then we're rotating based on our rotation speed which is in the flock manager dot FM dot rotation
speed multiplied by time, delta time.
Okay.
Now if we're turning, we're doing this.
If we're not turning, we want to do this other stuff down here, which is apply the rules and check
our speed.
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
and turn in the same breath.
Okay, so let's save this and we can go back into unity and have a look at our new flock and our turning.
So let's press play.
And I'm going to go back into the scene where we can actually see our fish.
We're going to select the flock manager.
Now, I will turn my neighbor distance right up and we can now watch the fish as.
They go about their flocking.
Now, if we turn that neighbor distance down to like one, then they're sort of independently going
to swim off.
But they should hit the limits.
And you're seeing it there when they hit the outside edge of that bounding box, like watching these
guys here and they turn around and come back into the center.
So this means that you're not going to lose a fish, essentially, and that they will stay in there
and not go off into infinity, basically.
Okay.
So that is a nice simple flocking algorithm.
And you, of course, can add in your pink fish if you want and make a very colorful looking tank.