Welcome back to part two of Creating a City Crowd.
Now, what I want to do in this one is to get the people moving up and down along a potential sidewalk
that goes from here and up to this corner and then back down again.
So we need to get three of our goal locations.
So I'm going to grab them one by one.
So there's one there.
So if I hit the key on that can actually move that goal location and I'm going to put it up here, then
I'll get the next one and I will put it down here in this corner.
And then the next one I will put up the other end of the street.
So up there.
Now the remaining ones, these three here, we can actually delete.
So we don't need those anymore.
Okay.
Now the people themselves, we want to sort of move them out of the road a little bit.
So let's just move these guys back here and anybody who's sort of within that darker gray square, just
move them out of the road because we're going to put some buildings into here and we actually don't
want them in the road.
So let me see if I can find each of these.
Just drag them out and then this one.
Drag her out there.
Okay.
So I think they're all outside of that realm.
Now, we going to put in some buildings.
So to do that over in the assets, if you come down and find the street folder resources folder, there's
a prefabs folder and in there there's some buildings and some shops.
Now the shops, if you grab that and drag that into your scene, you'll see that it's actually quite
a large model and it will fit in that gray square.
So let's just see if I can find that here.
And we just want to make sure it intersects with that nav mesh so that we can use it to block the path
of the agents.
Now, there's also a building here that you can drag in, which is absolutely enormous, and it sits
sort of inside of the street.
Like this.
Now it's really only for aesthetics.
You could, of course, just create all of this out of basic cubes.
You might want to bring that city down just a little bit more.
So let's see where those buildings have gone.
And they're just here.
Bring down the Y.
A little bit.
Okay.
So now what we will do is just select the buildings and the shops and you want to come over and make
sure that they are navigation static.
So drop down that box and you're looking to set them to navigation static.
Yes.
You want to change all of the children.
Then while we're here, we'll go to the navigation and then we'll click on Bake.
Now you'll be able to see immediately that our nav mesh gets baked around here.
So you can actually see there's a bit of a gutter around there.
Okay.
So at this point, if we press play, we should get the crowd walking along the sidewalk.
They're up to each goal.
So here they go.
Let's come out to the same view and have a look.
So they're going to walk to this goal.
They're going to go up that end, and then then they will pick another random goal and come back up
to this end.
Now they're going to select the shortest path.
So the shortest path from up here is going to be to come back again.
And you can see these guys are doing the same thing.
So they're coming back now.
Currently, they're walking all at the same speed, same animation.
So they're kind of all in step with each other, which doesn't make them look very natural.
So let's just fix their animation so that they start at random times.
So what we're going to do is to go into one of the agents and find their animation, which is this controller
here, and we want to select the walking animation inside of this looking animation.
Over in the inspector, you'll find that there's a cycle offset.
Now this can be set to a small offset, which will make them start their animations in slightly different
places.
So to do that, we need to create another parameter over on this side and the parameters you want to
hit the plus button and add a float.
We're going to call this our W offset, which stands for our walk off set.
Then the walking animation here we can come over and inside of this cycle offset, click on the parameter
box and then you can select W offset there.
Now you need to actually set this within the code as well to make it work.
So back to our assets, open up your script and what we're going to do underneath our is walking is
put an m dot set float w offset in quotes, same spelling and capitalization and we'll set it to a random
range between 0.0 F and 1.0 F, which will start the animations at slightly different locations.
Right?
So save that.
Let's go back into unity.
We'll press play and have a look at the effect that that has on the walking.
Okay.
So now they're not all in perfect step with each other, which looks a little bit more natural.
And we can also add in a speed multiplier so that they move at different speeds.
So back in the animator, we'll add in another float and we will call this one a speed malt like that.
Now we'll select the walk and then over an inspector.
There's a speed value here.
You can actually click on parameter and multiply by speed malt, which will speed up the animation and
how fast they're actually moving.
So with that, let's go back into the script and we'll just add this in here.
So we want to float speed multiplier equals random dot range 0.5, F and two and then we'll go.
Adam Dot set float speed malt.
Awesome.
Now we've sped up the actual animation.
We need to speed up the agent as well.
So agent speed multiply equal with SN.
So we're two places, we have to speed them up.
Now let's go ahead and add a dynamic obstacle and we'll make it a first person controller.
So attached to this lecture, there's a unity package called Characters Drag and drop that into your
assets.
It will create a standard assets folder.
Once it's loaded, you'll get a few warnings because it uses some old things from the previous standard
s package, which aren't actually used in here, but the scripts are still sort of integrated.
You can just select the console and clear it.
It's not going to affect the performance.
Okay, now we want to go into our standard assets folder.
Then we're going to go into characters first person prefabs and we want the controller.
So just drag that into the street and then you might just want to lift it up a little bit.
So that it's above the street level.
Okay.
So when we press play, you'll be able to move that around.
And if we go back to the same view so we can actually see it and put this over here.
You'll be able to look around with the mouse and then use the arrow keys to move.
Now, currently you can see that the people are just going to walk straight through the character.
They're not even going to be affected by bumping into it or anything like that.
So instead what we're going to do is make it into a nav mesh obstacle.
So come over into the controller in the inspector.
We're going to roll up all of this stuff and we're going to add a nav mesh obstacle to it.
Now it can have a size.
If you have a look in my scene, you'll see that it's put a box around there.
The controller is basically a capsule, so we can set that size to capsule, which will fit the current
size of the controller, as you can see there.
Now, of course, if it was a different size, you could resize it and recenter that actual capsule
over in the inspector.
So it fits a little bit better.
Right now, what you're going to find when we press play is that we have a nav mesh obstacle and you
can see that people are walking around it.
Now to have a look at what this looks like from the first person point of view, if you select the camera
that's in your scene just here, so this is the main camera and turn it off, it will then default to
the camera that is on the first person character.
Okay.
So now if we press play, let's bring across this game view, you'll be able to walk among the people.
But because you've got the capsule collider on there for the nav mesh, you'll actually be able to push
them out of the way.
So if we turn around and walk back into the crowd, you'll find that they go nicely around us.
And so you can create these kinds of dynamic obstacles.
Like, for example, you have a car that you drive down here or you can have a doorway that opens and
closes.
The nav mesh will recalculate the walking position of the characters so that they will basically avoid
you.
Okay, so that's a very simple way to create quite an effective crowd.