In this lecture, we will examine flocking.
Flocking has to be one of my favorite techniques.
It involves simple rules that generate complex behaviors that are mesmerizing.
Flocking can be used to procedurally generate the movement of groups of individuals that like to stay
together while moving towards a common goal.
The flight of birds comes to mind, but this technique can be used pretty much anywhere.
You have a group that needs complex, moving behaviors that you don't want to program by hand.
In this workshop, we'll be creating flocking or schooling fish.
So in unity, we're going to create a school of flocking fish.
Now, what you'll need for this is to bring in the fish unity package that is attached to this lecture.
And what you'll get is this wonderful fish that I made.
Now, I'm not the best animator or model or in the world.
If you have a better fish than this, then feel free to use it.
If you press play at the moment after you've opened up the swimming scene, you're going to get my green
fish here, which is moving his tail and trying to swim.
Of course, he's not actually going anywhere at the moment, so we're going to use him as an agent or
a bot or whatever you like to call it.
Within our flocking system, the first thing we're going to need in the hierarchy is an empty object
called flock manager.
So that's going to keep track of all of the fish.
All right.
So once you've done that, we're now going to create a new script.
So in your assets, create C-sharp script, and we'll call that Flock manager as well.
You want to attach that to your flock manager object that's in the hierarchy.
Okay, so let's start editing that.
Let's open it up.
Okay.
So what we need at the top are some things to keep track of everything.
First of all, we need a public game object for our fish prefab so that we can create fish spontaneously,
basically, given the number of fish that we set it up to be so public int num fish, let's set that
as a default to 20, which is quite a nice number to start with.
Public game object array of all fish.
So once they're instantiated we're going to put them into an array so we can keep track of them and
control them.
And then we need a public vector three for our swim limits, and this will be the space that the fish
can actually swim within.
So think of it like a fish tank, for example, and the size of that fish tank.
So we will make it a new vector three and I'm going to make it five by five by five.
Again, that's something that you can update if you want to make a bigger or smaller, right?
So down in the start, we need to instantiate all of our fish.
So we go all fish equals new game object.
Now I'm fish.
And then four int i equals zero I is less than numb.
Fish eye plus plus.
And then inside of here we'll create a position to start our fish.
So vector three pause equals this dot transform position.
Now this is going to mean that the position of the fish are relative to where the flock manager is.
So the flock manager is going to be basically the origin of your fish tank, which is going to be five
by five by five.
So just keep that in mind.
Now, from that fish tank, we're then going to go plus a new vector three and we're going to do random
ranges around our swim limits of where we're going to position our fish when we first create them.
So this will be random dot range, minus swim limits, dot x and then we want positive swim limits,
dot x bracket, and we want to do that three more times or two more times.
I mean, so we'll copy that and put it here and here.
And these will be the Y and the Z positions for your fish.
So line that up.
So make this a Y value.
Then make this the Z value.
And just finish off our.
Line of code with semicolon.
And then once we've got that will go all fish position I in our array is going to equal a instantiation
instantiate fish prefab at the position we just calculated and we'll give it a Catalonian identity.
So just a zeroed rotation for that particular fish that's created.
Okay, so save that.
We'll go back into unity and test out the creation of these fish.
So the prefabs for the fish that you'll find, you've got a green fish down here and a pink fish.
So we're just working with the green fish at the moment.
In fact, they're both exactly the same.
They're colors just different.
And if you want to create different coloured fish other than green and pink, go ahead and make different
colour materials and put them on those models.
Alright, so we've got our flock manager here, this green fish, you can actually just delete it.
We don't need it in there because the flock manager is going to create them.
Now my flock manager is at some weird position that it got put into, so we might want to just zero
it in the world, 000 I'm going to double click on it so that my scene is focusing on it.
You might also want to go to your main camera and go game object, align with view.
So it's also looking towards that 000 position.
Then we're going to grab our green fish and put it into our flock manager over in the inspector, and
then we can press play and see what we get.
So there's our 20 fish.
If we come back to our scene, we'll be able to go in a bit further.
They're all within the swim limits and they're all not actually going anywhere at all because we haven't
programmed to the move, but they are animated.
So that's a start.
Okay.
Well, lay flat manager for a moment.
We'll come back into the project and we're going to create another script that we will put on our prefab
fish.
So we'll call it flock.
So you want to drop Flock script onto your green fish and pink fish.
So drag and drop that onto that prefab and drag and drop that onto that prefab and any others that you
might have created in different colors.
So let's open up our Flock script and in here we're going to come up to the top.
We're going to set a float for the speed.
Now we want to set this speed from our Flock Manager script.
So I'm actually going to create a singleton in there or a static pointer to it so we can use that for
each of our fish.
So back in Flock Manager up the top, let's create a public static flock manager call it if I'm in capital.
So flock manager And then in the start we'll go flock manager equals this to set it up.
Then we'll create a few more values that we can set via the inspector, which will be our minimum and
maximum feed speed for our fish.
So at the top here we'll put header and fish settings.
And then inside of here, we want a range.
So this is how you can program in different sort of UI elements for your inspector if you haven't done
this before.
So we're going to create a header which is a title.
We then want range, which will be a slider between zero and 5.0 F and then we also want a public float
min speed, which will be the value for this particular range.
And then we want this again and we'll have a max speed down here.
Okay.
So now if we just save this and quickly go back into unity, you'll find over in your flock manager
here that you've now got these minimum, maximum speeds that you can set for your fish.
So while you're here, you might just want to give them a bit of a value to start with because we'll
just test that.
So in the Flock script, we can actually use those values to set up our speed.
So we will go in this case, that speed is going to equal the flock manager dot.
F m dot min speed and flock manager dot fm dot max speed.
And we will actually use those values inside a random dot range like that.
So it's going to set the speed for the fish randomly from our static point that's back into our flock
manager for those two values that have been set.
Right.
So it actually see this speed in action.
Let's just go into our update for our flock and we'll go this dot transform, dot translate and capital
translate.
And we're going to go forward along the Z axis zero zero and it'll be speed multiplied by time, dot
delta, time to smooth out the movement.
Okay, so that's going to move it.
Let's put translate not transform in there.
So let's save it and we will now try this out.
So we should have fish moving forward at different speeds.
Let's press play and have a look.
And.
Yep.
Dip.
There they go.
So some fish are faster than other fish.
Okay, so that's the basic set up for all of our code as far as linking and creating fish and all that.
When we come back in the next video, we will start working with the flocking algorithm.