WEBVTT

00:07.010 --> 00:12.200
In this section, we're working on creating some A.I. characters that are driven by Nev Meshes.

00:12.200 --> 00:17.300
So far, we've got Choppa on the screen that will go between mouse clicks, but the animations aren't

00:17.300 --> 00:18.020
sunk.

00:18.170 --> 00:21.850
And there's also another little bit of a problem in our agent manager.

00:21.860 --> 00:23.450
Let's fix those things up.

00:23.600 --> 00:25.600
Let's open up our agent manager code.

00:25.610 --> 00:33.290
Now, as I said, we've got a get component getting the AI controller inside of an update.

00:33.290 --> 00:37.070
Now, this is very, very bad practice and I know I do it a lot.

00:37.070 --> 00:41.960
And if you seen my other things, my especially my older stuff, I tend to have done this because I

00:41.960 --> 00:44.510
didn't realize the ramifications of doing it.

00:44.510 --> 00:47.570
Every update but get component is really slow.

00:47.570 --> 00:54.050
So as far as optimization goes, you don't want to do this in here doing it in a start.

00:54.050 --> 00:54.710
Fair enough.

00:54.710 --> 00:56.900
Because it's done and then you don't have to do it again.

00:56.900 --> 00:57.860
So that's what we're going to do.

00:57.860 --> 01:00.440
We're going to move this sort of code up into here.

01:00.980 --> 01:09.140
As far as we're concerned in our code, we only need the nav mesh agent to run set destination.

01:09.890 --> 01:11.990
So we don't actually need the game object.

01:11.990 --> 01:19.130
So instead of keeping the game objects up here in array, we could just keep an array of nav mesh agents.

01:19.220 --> 01:24.350
So I'm going to go nav mesh agent.

01:24.350 --> 01:28.250
Now I might actually need to include the top here to get hold of it.

01:28.250 --> 01:39.050
So using unity engine I and then we can go now mesh agent array of agents down on the star game object

01:39.050 --> 01:42.320
for an object with tag is getting a whole array of game objects.

01:42.320 --> 01:45.080
So let's put those into their own array down here.

01:47.120 --> 01:52.490
It's yeah, a little bit of double handling but anyway, not a lot we can do about at this point.

01:52.490 --> 02:04.130
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

02:04.130 --> 02:08.570
just go nav mesh agent.

02:08.810 --> 02:12.890
And you know what's going to be even better is if we make a list of nav mesh agents up here.

02:13.040 --> 02:23.480
So list of nav mesh agents and that will going to equal a new list of nav mesh agent.

02:23.480 --> 02:37.220
And then down in here we can just go agents dot add and it can be geo dot get component nav mesh agent.

02:38.610 --> 02:41.430
And put it into the list, which makes it a lot easier.

02:41.460 --> 02:49.740
Then down in here, we're going to go for each and we will get our agent out of our list.

02:49.740 --> 03:03.510
So nav mesh agent a in agents and then we can just go a dot set destination hit point.

03:03.660 --> 03:10.830
So we've got rid of our messy get component or our an optimized get component and putting that into

03:10.830 --> 03:17.340
a list that we're keeping from our game objects and then running that there so that optimizes that code

03:17.340 --> 03:18.800
so we can save that.

03:18.810 --> 03:25.440
Now the other thing we have to do is come back and consider when is our agent moving and when isn't

03:25.440 --> 03:26.550
it fair enough?

03:26.550 --> 03:31.800
We can tell it to set destination here, which is going to set it on its way.

03:31.800 --> 03:35.400
But how do we know if it's moving or not?

03:35.400 --> 03:42.330
And when it is moving, we want to set its animation controller to run the walking animation as opposed

03:42.330 --> 03:44.100
to the idle animation.

03:44.100 --> 03:49.260
So the first thing we're going to need to do is get hold of the animator that's on our character.

03:49.260 --> 03:54.360
So these characters come with an animator that I've already set up for you, so you don't worry about

03:54.360 --> 04:01.260
dealing with an animation system as well as learning A.I. but we do have to get it in start.

04:01.260 --> 04:08.970
So it's the same kind of thing, this dot get component and we want to get the animator like that.

04:09.030 --> 04:09.600
Okay.

04:09.600 --> 04:18.180
Now in the update, we can actually test where Chopper is in respect to its destination.

04:18.180 --> 04:20.940
So we set the destination in the agent manager.

04:20.940 --> 04:27.810
But in update we can also set the destination, but we can check how far it's still got to go.

04:27.810 --> 04:30.900
So we don't want to set it in here because we're setting it outside.

04:30.900 --> 04:35.100
But we will go something like if agent.

04:36.270 --> 04:42.510
Dot remaining distance and we need to give it a distance of how far way it is from its goal.

04:42.510 --> 04:49.440
Let's say it's two and you might come back and change that if you find it's going into idle too quickly.

04:49.440 --> 04:59.340
Now to set the animation for idle it's anim set bool and the flag in there is called is moving.

05:00.120 --> 05:04.110
We're going to set that to false in here then.

05:05.920 --> 05:15.640
Else we will go Adam set ball is moving to true.

05:16.320 --> 05:16.720
Okay.

05:16.720 --> 05:22.840
So that should start your moving walking animation and then your idol depending on how far away you

05:22.840 --> 05:25.180
are from your goal location.

05:25.180 --> 05:26.550
So just save that.

05:26.560 --> 05:32.170
Let me show you where I'm getting this is moving string from if you're not familiar with animation.

05:32.170 --> 05:34.210
So let's just pop back into unity.

05:34.480 --> 05:39.160
Once this is finished loading, we're going to select Ciampa.

05:39.190 --> 05:41.050
Now Ciampa has an animator.

05:41.050 --> 05:47.290
If we open up this animator by double clicking on it, it's going to show you a very simple graph,

05:47.290 --> 05:53.980
which is your state machine, which is an animation, and he's got a chopper idle and a chopper walk

05:53.980 --> 05:54.520
forward.

05:54.520 --> 05:56.740
Now to transition from idle.

05:56.740 --> 05:57.730
To walk forward.

05:57.730 --> 05:59.080
You can see there's this arrow here.

05:59.080 --> 06:04.450
If you select that arrow there and have a look over in the inspector, you'll see I've got an easy moving

06:04.450 --> 06:06.970
flag on there for true.

06:07.060 --> 06:11.650
And then I've got another one here for false to come back the other way.

06:11.830 --> 06:17.440
If you're really interested in understanding how all these animators and this kind of thing works,

06:17.440 --> 06:22.900
then do check out my animation course, which will cover all of these things in a lot of detail.

06:22.990 --> 06:23.470
Right.

06:23.470 --> 06:29.710
So with that explained, let's go back in to our scene and we're pretty much ready to try this out.

06:29.710 --> 06:31.420
So let's press play.

06:32.980 --> 06:36.010
Okay, so we'll click over here and off he goes.

06:36.010 --> 06:38.110
He's walking and then he stops.

06:38.200 --> 06:41.920
Now he's coming to a little bit of a slight skid when he stops.

06:41.920 --> 06:47.470
That's that two meters that we've set, if you want that to be maybe a little bit more accurate.

06:47.470 --> 06:49.330
So he keeps walking right up to the end.

06:49.330 --> 06:55.210
You can put that value at one, like less than one or maybe even smaller than that.

06:58.890 --> 06:59.400
Okay.

06:59.400 --> 07:02.760
So now that you've done that, we've got our chopper.

07:02.760 --> 07:05.390
We can actually duplicate our chopper.

07:05.400 --> 07:13.080
So if you select it in the hierarchy and hit control D, just make sure that you move them apart and

07:13.080 --> 07:17.310
you can kind of put them pretty much anywhere as long as they're above the nav mesh would be good.

07:18.160 --> 07:19.260
And he's up in the air.

07:19.260 --> 07:25.350
It doesn't really matter because they will come down onto the nav mesh because they need to be on the

07:25.350 --> 07:28.410
nav mesh in order to orientate on it.

07:29.400 --> 07:29.790
Okay.

07:29.790 --> 07:33.000
So now let's press play with all those chompers on there.

07:33.210 --> 07:41.490
And the nice thing about those colliders we set up earlier, the nav mesh colliders, is that it gives

07:41.490 --> 07:44.190
a kind of a physics effect as far as it keeps them apart.

07:44.190 --> 07:49.920
They won't overlap each other because of all those little green capsules that are on them, but they

07:49.920 --> 07:52.380
also won't follow the exact same path either.

07:52.380 --> 07:56.730
So they kind of get out of each other's roads and we can just keep clicking around here.

07:56.730 --> 08:02.940
Now, if we click over here, they can't go over those rocks, they're just going to go through them.

08:02.940 --> 08:07.620
Or some of them are that are not as bad sloped as these others, but they can't get over the top of

08:07.620 --> 08:08.310
those.

08:09.660 --> 08:15.510
So now you can put a whole bunch of these little guys on here and they're just going to go off and calculate

08:15.510 --> 08:17.550
their paths like that.

08:19.780 --> 08:23.890
And depending on which path they actually get through, you get some interesting kind of behaviors where

08:23.890 --> 08:28.180
they kind of split up because they couldn't all sort of fit through that one area.

08:29.050 --> 08:33.460
In the next video, we will come back and add in some different agent types.
