I mentioned the flocking rules at the start of this section, but let's go over them again in detail.
They are moved towards the average position of the group, align with the average heading of the group
and avoid crowding other group members.
Moving towards the average position of the group involves taking all the individual members positions,
adding them together and dividing by the number in the group.
We can then work out where an individual is in relation to the average location and have them turn towards
it to align with the average heading of the group.
We add all the headings of the individuals together and divide by the size of the group.
This gives an average vector for individuals to try and align their movement.
To avoid neighbors, an individual must be aware where the neighbors are and then turn away from them.
Then to calculate a new heading and individual add together the vector for moving towards the center
of a group, the vector to align with the group's heading and the vector to turn away from a neighbor.
Before we start adding these rules into our Flock script, we're going to add a few more variables that
you can change in the inspector to modify the behavior of the fish.
So in our flock manager, we're going to add in two things.
First of all, we want another range.
So I'm just going to copy these and put them down here.
Now, this range will be between one and ten and it's going to be called neighbor distance.
And we want to use a neighbor distance because the fish are going to flock with their neighbors.
That means you can set this, that they'll only abide by the rules with fish that are close to them
and ignore fish that are far away.
Because I don't actually want to be flocking with those particular fish that are in the distance.
So we'll have a neighbor distance and then we'll copy this and we'll put this down here and we're going
to add in a rotation speed as well as how fast the fish can turn around on the spot in order to keep
up with the flock.
Okay.
And we'll set that between zero and five.
Of course, zero is probably not a good value to have because fish aren't going to turn at all if they
have a turn of zero.
So let's set that to one instead.
Okay, so let's save that.
And back in our Flock script, we're going to add inside of the update and apply rules which we haven't
written yet.
But this method here is going to turn the fish around basically towards the direction it needs to be
travelling in.
And then we're just going to push the fish forward along here.
Okay, so underneath update, we will write our apply rules.
So this will be a void, apply rules.
And then inside of here we're going to have game object array.
Game objects.
Now, that's going to be all of the fish that are back in the manager.
So this will be joss equals flock manager, dot m dot all fish.
Okay, so vector three to calculate the v center, which is the flock vector to the center of the group,
and that's going to equal initially a vector 3.0.
Then we want to vector three to avoid, let's call it V avoid vector to avoid equals vector 3.0 and
then set that to zero.
And then we're going to have float G speed.
So this is our group speed, which equals 0.01 F by default, our float n distance which is our neighbor
distance and then int group size or that will be the size of the fish that are within that neighbor
distance.
So that's the group, your current local group of fish right now we'll have a for each game object go
in go's.
So this is our game object in our go so it's basically for each fish in the fish array or in the school
of fish that we're working with.
If the current game object does not equal this game object, which is this current fish, because we
don't want to compare this fish with itself.
They were going to calculate the distance to the next fish.
So distance and distance equals vector three point distance between go dot transform position and this
dot transform dot position.
And then if that fish you've just checked on out of the whole school is within the neighbour distance,
then we will consider it a neighbor and it's going to influence the fishers current trajectory.
So if NW distance is less than or equal to flock manager dot m dot neighbor distance.
Then the vector to the center of the group is going to equal plus equals go transform position.
So we're going to add all of the positions of any fish that are in our group together and our group
size.
We will increase by one.
So plus, plus now if n distance is less than 0.10 or sorry, 1.0 f if we're nice and close, then we
actually want to avoid that particular fish.
So the avoid will equal v avoid plus the vector away from that particular fish.
It'll be this dot transform dot position minus go dot transform position.
Okay.
So then we'll have floc another floc equals the go get component.
Flock.
So this is getting the flock script on the current comparison.
Fish And what are we going to do with that is get it speed so that we can get an average of the speed
of the group.
So group speed equals G speed plus another flock.
Dot spade.
Okay.
Right.
Moving down after all of that, we then going to test for our group size.
So if group size is greater than zero, the V center is going to equal the V center divided by group
sizes.
This is where we're actually averaging it at the top.
We're adding them all together.
And then after we finished adding them up, which is our v avoid now v center, we're going to average
them down here because it's only after we've added them all up and counted them.
Do we know how many neighbors we have in the group?
So speed is going to equal G speed divided by group size and then we want a vector.
Three direction is going to equal our v center plus lv avoid minus transform dot position.
So this will be our new direction that we want our fish to head in if direction does not equal zero.
So.
Vector 3.0.
Then we're going to transform dot rotation equals Catalonian dot slurp transform dot rotation and this
saturnian slurp is going to turn the fish slowly towards the direction that it needs to go in.
That's why we created that rotation speed earlier.
Okay, so Catalonian look, rotation direction.
And then flock manager, dot flock manager f m dot rotation speed multiplied by time dot delta time.
Okay, so that's the code for the flock, so save that.
Okay, so let's have a look what this is going to do.
So we're going to go into our flock manager and make sure that we've got some values for everything.
So our rotation speed, let's click on that to set it to one our neighbor distance.
Let's make that.
But now let's turn it right up and let's put rotation speed to sort of halfway along.
Okay, let's press play and have a look at our flock.
So there they are there.
Let me go to the scene so I can actually zoom in and you'll be able to see the behavior of their fish.
So they're actually really nicely flocking.
Now, when we come back in the next lecture, we'll actually add a little bit of variety into here so
that we can set a goal location and also to stop the fish speeding up, as you can see there, that
they're just getting faster and faster.
So I'll see you in the next lecture.