In this section, we're working on creating some A.I. characters that are driven by Nev Meshes.
So far, we've got Choppa on the screen that will go between mouse clicks, but the animations aren't
sunk.
And there's also another little bit of a problem in our agent manager.
Let's fix those things up.
Let's open up our agent manager code.
Now, as I said, we've got a get component getting the AI controller inside of an update.
Now, this is very, very bad practice and I know I do it a lot.
And if you seen my other things, my especially my older stuff, I tend to have done this because I
didn't realize the ramifications of doing it.
Every update but get component is really slow.
So as far as optimization goes, you don't want to do this in here doing it in a start.
Fair enough.
Because it's done and then you don't have to do it again.
So that's what we're going to do.
We're going to move this sort of code up into here.
As far as we're concerned in our code, we only need the nav mesh agent to run set destination.
So we don't actually need the game object.
So instead of keeping the game objects up here in array, we could just keep an array of nav mesh agents.
So I'm going to go nav mesh agent.
Now I might actually need to include the top here to get hold of it.
So using unity engine I and then we can go now mesh agent array of agents down on the star game object
for an object with tag is getting a whole array of game objects.
So let's put those into their own array down here.
It's yeah, a little bit of double handling but anyway, not a lot we can do about at this point.
So game objects are going to go into a and then we go for each game object go in a and in here we will
just go nav mesh agent.
And you know what's going to be even better is if we make a list of nav mesh agents up here.
So list of nav mesh agents and that will going to equal a new list of nav mesh agent.
And then down in here we can just go agents dot add and it can be geo dot get component nav mesh agent.
And put it into the list, which makes it a lot easier.
Then down in here, we're going to go for each and we will get our agent out of our list.
So nav mesh agent a in agents and then we can just go a dot set destination hit point.
So we've got rid of our messy get component or our an optimized get component and putting that into
a list that we're keeping from our game objects and then running that there so that optimizes that code
so we can save that.
Now the other thing we have to do is come back and consider when is our agent moving and when isn't
it fair enough?
We can tell it to set destination here, which is going to set it on its way.
But how do we know if it's moving or not?
And when it is moving, we want to set its animation controller to run the walking animation as opposed
to the idle animation.
So the first thing we're going to need to do is get hold of the animator that's on our character.
So these characters come with an animator that I've already set up for you, so you don't worry about
dealing with an animation system as well as learning A.I. but we do have to get it in start.
So it's the same kind of thing, this dot get component and we want to get the animator like that.
Okay.
Now in the update, we can actually test where Chopper is in respect to its destination.
So we set the destination in the agent manager.
But in update we can also set the destination, but we can check how far it's still got to go.
So we don't want to set it in here because we're setting it outside.
But we will go something like if agent.
Dot remaining distance and we need to give it a distance of how far way it is from its goal.
Let's say it's two and you might come back and change that if you find it's going into idle too quickly.
Now to set the animation for idle it's anim set bool and the flag in there is called is moving.
We're going to set that to false in here then.
Else we will go Adam set ball is moving to true.
Okay.
So that should start your moving walking animation and then your idol depending on how far away you
are from your goal location.
So just save that.
Let me show you where I'm getting this is moving string from if you're not familiar with animation.
So let's just pop back into unity.
Once this is finished loading, we're going to select Ciampa.
Now Ciampa has an animator.
If we open up this animator by double clicking on it, it's going to show you a very simple graph,
which is your state machine, which is an animation, and he's got a chopper idle and a chopper walk
forward.
Now to transition from idle.
To walk forward.
You can see there's this arrow here.
If you select that arrow there and have a look over in the inspector, you'll see I've got an easy moving
flag on there for true.
And then I've got another one here for false to come back the other way.
If you're really interested in understanding how all these animators and this kind of thing works,
then do check out my animation course, which will cover all of these things in a lot of detail.
Right.
So with that explained, let's go back in to our scene and we're pretty much ready to try this out.
So let's press play.
Okay, so we'll click over here and off he goes.
He's walking and then he stops.
Now he's coming to a little bit of a slight skid when he stops.
That's that two meters that we've set, if you want that to be maybe a little bit more accurate.
So he keeps walking right up to the end.
You can put that value at one, like less than one or maybe even smaller than that.
Okay.
So now that you've done that, we've got our chopper.
We can actually duplicate our chopper.
So if you select it in the hierarchy and hit control D, just make sure that you move them apart and
you can kind of put them pretty much anywhere as long as they're above the nav mesh would be good.
And he's up in the air.
It doesn't really matter because they will come down onto the nav mesh because they need to be on the
nav mesh in order to orientate on it.
Okay.
So now let's press play with all those chompers on there.
And the nice thing about those colliders we set up earlier, the nav mesh colliders, is that it gives
a kind of a physics effect as far as it keeps them apart.
They won't overlap each other because of all those little green capsules that are on them, but they
also won't follow the exact same path either.
So they kind of get out of each other's roads and we can just keep clicking around here.
Now, if we click over here, they can't go over those rocks, they're just going to go through them.
Or some of them are that are not as bad sloped as these others, but they can't get over the top of
those.
So now you can put a whole bunch of these little guys on here and they're just going to go off and calculate
their paths like that.
And depending on which path they actually get through, you get some interesting kind of behaviors where
they kind of split up because they couldn't all sort of fit through that one area.
In the next video, we will come back and add in some different agent types.