In the previous lecture, we created our very simple flocking fish simulation here.
Now, if we in fact change the neighbour distance and put it down to like zero, notice the fish don't
flock, they just all go out.
Now, if I set it quickly back up to ten and then I go looking for my fish, you can see there's some
over here that have reef locked together and there might be others.
It depends how close they are.
There's a group down here.
I don't know if you can see those as well.
But the thing is with that is that their neighbor, the distance is going to be dependent on obviously
the distance between each group.
So while you've got a big group that can be going together, if they separate, then you can have little
groups because they're still within a certain distance of each other.
Now, a few things we've got here is we have a speed that is just out of control because it just keeps
averaging and goes up to the maximum speed here.
So we can actually, if we want, sort of more relaxed fish, kind of turn that down a bit.
We're also going to add a bit of variety to the speed in a moment.
So here we are and now our fish are going a lot slower.
So see if I can do that again for you.
It's just drop that neighbor distance, swim apart, turn it back up again.
Note they will still within ten.
Let them go out a bit, bring it back up again.
And now you can see a flock has formed over here, one over here, and I would assume one over here
because that's where they went.
Okay, so let's start this lecture by adding in a goal location that you can get the fish to swim towards
while they're flocking.
So, for example, let's say there was some food that came into their environment.
They would all want to go towards the food, but they'd also have to stay in a flocking formation.
So let's open up our flock manager to put the value for our goal location in here.
Okay, so let's create up the top a public vector three four goal pose and we'll equal vector 3.0.
And initially we will set it down in the start to goal.
Pause equals this dot transform dot.
Position.
So it'll be the position of the flock manager to begin with.
Then in the update, we'll actually move that around randomly.
So let's put if random range between zero and 100 is less than ten.
So sort of a 10% chance that we will then change it.
If you want it to change less often, you can increase this number here, like to a thousand or you
can make it smaller if you want it to happen often.
So we want goal pause equals and I'm going to use this value here that we're using for our fish, which
is to make sure that we keep the goal pause inside of the swim limits.
So let's copy that and put that down in here.
Okay, so that's our goal.
Pause now changing and we have to figure out how to then use this and integrate it into the code we've
got in the flock.
So let's just save that and switch into our flock code.
And in our calculation for the center of our group, we're going to add in a vector to indicate the
direction towards the food or the goal location.
So flock manager dot FM goal pose minus this dot transform dot position.
Okay.
So the other thing I also want to do in here is within the update for the fish is to randomly reset
the speed.
So this is actually setting the speed, but we want to randomly reset the speed before applying the
rules.
So I'm going to leave this as a challenge for you.
We've already randomly set the goal position in the flock manager, so using that kind of concept,
you can apply it in here with the setting of speed.
Okay.
So pause the video now and when we come back, I'll go through that with you.
Okay.
So in the update, we're going to go if random dot range between zero and 100 is a less than ten, same
sort of chance.
Then what we'll do is we'll reset the speed.
So we'll copy that and we'll put that down into that.
Okay.
So we'll save the flock.
Let's have a look at this changing goal.
So select your flock manager and I'm going to leave the neighbor distance at full.
Okay.
So you can actually see the whole flock moving together towards the goal, right?
So press play and we will see those fish.
Let's go in the scene where we can actually see it.
Right.
So we've got the goal position and you saw it just changing then and it's changing quite rapidly.
Let's come in a bit closer here because you can see that they are going to one area and they circle
it for a little bit of time and then they pop out to another area where the goal is and circle that.
So we have some really interesting and nice behaviors in here.
They're still going super fast, aren't they?
So I'm just going to come back into my flock script, want to make sure that this speed doesn't get
out of control where we're setting it down here so we could go something like under here if speed becomes
greater than the flock manager dot, FM dot.
Max speed.
Then we will just set speed to equal the max speed, and then you can actually control it right in there.
And if you also want to reduce the load on the system, as well as let the fish sort of swim in sort
of the forward direction for a little bit longer, rather than constantly calculating this, you can
also apply a randomness around the apply rule.
So they're not doing it every single update.
And the more and more fish you add, you'll notice there's quite a large load on them calculating their
current flocking rule.
So if we go something like this and then again you can play around with these positions here, we will
get the fish swimming for a little bit longer in a straight line and turning, which won't have them
sort of swimming around and round and round in circles, as you've seen before.
Okay, so save that.
Let's have a look at that behaviour now.
Okay.
So here they are.
See how they go straight line for just a little bit longer before they bother to turn towards their
neighbors.
And that speed's also going to really help with keeping them kind of under control.
And this looks a little bit more natural than than them whipping around in a circle.
And the goal position is still moving.
So you've got your basic rules.
And as you see, we're kind of tweaking the simulation along as we want to add sort of different functionality.
So never be afraid of just trying out different things.
Just because the flocking system has three sets of rules that belong to it doesn't mean you can't go
and add something else if you want slightly different behavior.
And it's just a matter of kind of experimenting to see, if you like, the effect that you get out of
it.
In the next video, we will come back and I will show you how to contain the fish within their swimming
limits so that if they're not flocking, then they don't just swan forever and ever in a day in the
one direction.
They actually hit the edge of the tank or pond or whatever it is, and they come back into the scene.