As I mentioned in the previous lecture, developing the eye for a crowd of NPCs is far simpler than
most people think.
This is because from simple rules, complex behaviour can develop and although we aren't programming
the so called brains of the NPCs, they still appear to move with some level of intelligence.
In this lecture, we will explore crowds further with a group of animated characters.
Okay, so to get started, you'll need to bring in the starter file attached to this lecture.
Now, when you bring it into unity, you will get a new scene called Crowd.
Double click on that and open up your scene, in which case you will see these characters all ready
to go for you.
Now, I've done a lot of the hard work as far as the animations go with these guys.
Let me just show you what they are.
So I'm just going to go into the controller for the crowd, and this is what the animation looks like.
It's very simple.
You've got Idol, you've got walking and got running, and you can transition between those different
states.
Now in here we have some parameters for is running, is walking and is idle.
So these are trigger values that are going to allow us to transition from one state to the other.
And we'll be using these values here to trigger the animations.
If you're not familiar with the animation system, don't worry too much.
This is going to do all of the work for you.
Just don't touch it and change what it's doing.
So we will just write code that will turn these things on and off to get our characters to move.
So let's go back into our scene or select this girl again.
Have a look in the inspector.
All the characters are going to have an animation agent already attached to them.
They have a speed set to.
The reason the speed is set to two is that it's sort of the best speed for the walking animation.
So when you have an animated character and it's an agent, you have to try and synchronize the speed
of the agent with the speed of the animation itself so that they kind of look fairly believable.
All right.
So at this point, let's just press play and have a look at what we get with these particular characters.
You'll see that they go straight into their idle state.
They're waiting to be given a goal to walk towards.
So let's get about doing that.
And we're going to use our eye control script that we used before.
So let's just open that up and we'll make a few modifications.
All right.
So are we going to, first of all, just get rid of the public off of our game object here and actually
make it into an array because we'll have multiple positions.
So let's go go locations that we'll call this.
Then we have our agent as we have before.
That's fine.
Now the agent here is going to get the mesh agent on the character.
We then want to also read in all of our goals.
So we'll go goal locations equals game object dot find objects with tag and we have to create some goals
and actually set them to the tag of goal like that.
So it'll populate our goal locations and then the set destination, it's going to get something from
the goal location.
So goal.
Locations and we will get a random value out of that.
So let me just put above this.
We'll have an INT for.
So I will call it the index into the goal locations and set this to random range between zero and goal
locations dot length.
And that will get us the index into that array there.
Then once we've got that, we want to get the position.
So transform it.
Position like that.
Right?
So save that.
Now let's go back into unity.
And what we're going to do is select all of our characters here.
And drag and drop the air controller onto them.
Okay.
Now we have to create some goal locations.
So let's come up into our scene and we'll scroll over a bit.
We're going to go into a hierarchy, right click 3D object, create a cube.
Now that cube doesn't need to be that big and we're going to get rid of the box collider on it so we
can actually scale it down.
It's just a location.
Let's see.
But we want it to be on the ground, so let's move that down.
So it's actually on the ground.
Okay.
And we're going to while we're here, tag it.
So go add tag add goal with lowercase save for the tag, click that cube and then set it to goal.
Now once we've got one goal location set, we can create another one.
So just duplicate that cube and put it over here.
All right, so we're ready to try this out.
Let me just check where my camera is pointing.
Yep, that's fine.
Now let's press play and have a look where they go so they're going to pick a random location.
Notice that they're sliding in the idle state towards it.
That's all right for now.
But you can see that these guys selected this location and these guys selected that location.
And if you add more goals, then they will just go to different locations randomly.
Okay.
So let's now add in our animation.
So at the top of our code, we're going to put animator.
Adam.
And then we'll get hold of that down here in the start.
So we go Adam equals this dot get component in brackets animator and then we can set the trigger so
Adam dot set trigger and then inside of here we want to put the string that represents the trigger that
we want to trigger, I guess.
Okay, so what is that?
Let's just quickly duck back into unity, grab one of our characters and go into our animator and have
a look.
So we're going to go from Idol to walking, which is this arrow here.
So this transition is if you have a look over the inspector is walking capital W it's what's written
over here.
This has to be exactly the same spelling and capitalization back in the script.
Okay, so let's go back to our script and in here we'll put is walking like that.
Okay, so save that and now we'll go back into our scene and press play and you'll be able to see the
characters running the walking animation towards their goal location.
So off they go and they're walking.
Now once they get there because there's a few of them, they're kind of fighting over whether they're
at the destination or not.
So they're kind of pushing each other out of the way, which is interesting kind of behavior.
Again, this is inherent behavior.
We didn't program them in to do this, but they're just doing that, which kind of looks quite neat.
So let's modify the code just a little bit more so that when they do reach their destination, they'll
actually go to another location.
So we're going to put if agent remaining distance is less than one, then they've reached their destination
and if they have, we will go agent set destination basically want to do this line here again.
Let's get another random location and go to it down there.
Okay, so save that.
Let's switch back into unity now with your cubes you've got for your goal locations, you can duplicate
them and move them to wherever you would like like that.
Okay.
And then we can actually get our group of people.
So shift select all of those and hit control.
D And while we've got that group, we might just move them out and then control D and move them out
and control D and move them out.
Now we have a much bigger crowd.
Let's press play and have a look at them wandering around in the scene.
So off they go.
They're going to find a random location to go to and when they've reached it, they will go to a different
random location.
So you can see them making a choice when they get there.
Let me come back in the next lecture.
We will continue this and have a look at some more sort of complex behaviors for the crowding.