WEBVTT

00:06.500 --> 00:10.760
In the previous video we wrote The Idol and the Patrol States.

00:10.760 --> 00:18.020
We now need to create the A.I. class, which we will put on our guard here so that he will be able to

00:18.020 --> 00:19.640
transition through states.

00:19.640 --> 00:28.730
So let's right click and create a C-sharp script called a AI and we'll open that up.

00:29.150 --> 00:33.930
Are you going to be quite surprised just how short this code is?

00:33.940 --> 00:40.190
So this is the code that we will attach to our NPC, which is UG in this case.

00:40.550 --> 00:44.420
First of all, we need to make sure we grab hold of the nav mesh stuff.

00:44.420 --> 00:49.850
So using unity engine AI at the top.

00:49.910 --> 00:56.810
And then we also want to get all the bits and pieces that we need to send through to our states.

00:56.810 --> 00:59.720
So we need our nav mesh agent.

01:00.650 --> 01:02.960
We need the animator.

01:03.560 --> 01:12.730
We will a little later on need the player as well because we need to get your NPC to chase the player.

01:12.740 --> 01:15.590
So let's just put a public.

01:16.950 --> 01:26.280
Transform player in there and we will create our state, which is the current state.

01:26.970 --> 01:28.500
So that's all we need there.

01:28.500 --> 01:33.630
We just have to remember it to give the player to this code in the inspector.

01:33.870 --> 01:37.620
Right in the start we will grab hold of our agents.

01:39.210 --> 01:40.260
NAV mesh agent.

01:40.260 --> 01:44.790
So agent equals this dot get component.

01:47.000 --> 01:49.520
NAV mesh agent.

01:51.110 --> 01:56.000
And the animator, they get component.

01:57.280 --> 01:58.450
Animator.

02:00.810 --> 02:03.840
And then we will create our first state.

02:03.840 --> 02:08.610
So current state will equal new idol.

02:09.840 --> 02:13.350
And for it we need to pass through all the bits and pieces.

02:13.350 --> 02:15.590
So this particular NPC.

02:15.600 --> 02:17.730
So this game object.

02:18.630 --> 02:24.570
Then it needs the agent and the animator and the player.

02:24.600 --> 02:33.900
Now you could do a get component inside of your states and just pass through the game object, except

02:33.900 --> 02:37.020
that we're creating and destroying new states all of the time.

02:37.020 --> 02:40.050
And so it'll end up lagging a little bit.

02:40.050 --> 02:45.030
And I do have a little bit of an admission that later on we will actually get to a get component inside

02:45.030 --> 02:45.900
one of the states.

02:45.900 --> 02:51.630
But there is a way around that by coming back here and making sure that you grab everything right at

02:51.630 --> 02:53.850
the beginning when the A.I. is set up.

02:53.970 --> 03:02.490
Now, in update, if you've created any A.I. before, the update is where all of the magic happens and

03:02.490 --> 03:06.540
the update can get like true spaghetti code.

03:06.770 --> 03:12.180
It can get really, really messy and very hard to debug and hard to get right.

03:12.690 --> 03:22.830
But using these states in here we put current state is going to equal the current state dot process.

03:23.250 --> 03:32.070
So remember process in the states is working through the enter the update and then the exit and then

03:32.070 --> 03:38.340
when it gets to exit, it returns the next state that it needs to go to, not even exit.

03:38.340 --> 03:45.450
If it gets to somewhere in the update that it needs to throw out, it sets the new next state, which

03:45.450 --> 03:48.810
then gets passed back in the exit.

03:48.810 --> 03:49.850
Let's just remind ourselves.

03:49.850 --> 03:54.990
So go back into state and let's just go up the top where all that is happening here in process.

03:54.990 --> 03:58.410
So it's probably much easier to explain if we're actually looking at it.

03:58.410 --> 04:03.960
So the process is going to run from the AI that's asking it to.

04:03.990 --> 04:07.920
And then if stage is enter, it's going to run the enter.

04:07.920 --> 04:12.480
The enter is then going to make sure that stage gets updated to update.

04:12.480 --> 04:19.290
So the next time that the AI update calls process, it's going to then run this update and it's going

04:19.290 --> 04:27.450
to keep doing that until the update itself flicks you across into the exit state, at which point the

04:27.450 --> 04:31.680
exit gets run and then it returns, whatever that new state is.

04:31.680 --> 04:38.610
And if we have a look down in idle, we can see that happening right here where new state is getting

04:38.610 --> 04:40.290
created to be patrol.

04:40.290 --> 04:44.970
So with a 10% chance in idle, we will go into a patrol.

04:45.710 --> 04:49.260
All right, so that's your AI class.

04:49.260 --> 04:49.890
That's it.

04:49.890 --> 04:51.420
That's all we need in there.

04:51.420 --> 04:55.830
And you know, you can add a few other things if you like, but as far as the behavior is concerned,

04:55.830 --> 04:57.420
that is all we need.

04:57.420 --> 05:00.360
And we just keep adding more states and linking them up.

05:00.720 --> 05:02.220
Okay, so we'll save that.

05:02.220 --> 05:03.960
Now go back to Unity.

05:03.960 --> 05:07.980
You want to drag and drop that II code on to ARG.

05:07.980 --> 05:13.920
So find ARG in your hierarchy, drag and drop that AI onto there.

05:14.010 --> 05:20.700
Now we've got an PHP controller that will have come with your project so that you can walk around,

05:21.420 --> 05:27.840
get that and drag that into the player transform part that's exposed.

05:28.170 --> 05:32.250
Again, it doesn't come into play, but it will a little later on.

05:32.460 --> 05:32.970
All right.

05:32.970 --> 05:36.360
So now we're ready to press play and have a look.

05:36.360 --> 05:39.810
So let me just make this game screen a little bit bigger.

05:39.810 --> 05:40.920
Press play.

05:41.520 --> 05:41.820
All right.

05:41.820 --> 05:48.390
So you can use the WASD keys to move around all the arrow keys for this player controller and we can

05:48.390 --> 05:49.710
walk right up to ARG.

05:49.710 --> 05:51.750
He's not programmed to attack us yet.

05:51.840 --> 05:58.470
Now he is with 10% chance meant to go on a patrol, but he doesn't appear to be.

05:58.470 --> 06:03.090
Now I know why this is, and I'll go back and show you that in a moment.

06:03.090 --> 06:06.660
But for now you can, I guess, just enjoy his idle state.

06:06.810 --> 06:09.570
And that's what he does look like when he's in idle.

06:09.600 --> 06:14.850
Okay, so just press escape to get your mouse cursor back and stop playing and we're just going to go

06:14.850 --> 06:16.890
back into the state code.

06:16.890 --> 06:20.640
Let me show you where that issue is occurring.

06:21.000 --> 06:21.460
Okay.

06:21.510 --> 06:32.040
So the really, really important part of this whole state system is that this here works nicely to go

06:32.040 --> 06:34.260
from one thing to another.

06:34.260 --> 06:40.170
So we go into we go update, update, update, update for as long as you need to go update and then

06:40.170 --> 06:46.400
we go into exit and then return the next state that we need to go to.

06:47.010 --> 06:50.970
What's happening in idle and I've probably done this wrong in patrol as well.

06:50.970 --> 06:53.340
If we go down to idle, let's have a look.

06:53.340 --> 06:59.130
The enter runs at the moment is setting is idle and then it's running.

06:59.130 --> 07:00.240
Its base is enter.

07:00.240 --> 07:07.140
Now its base enter is setting event update, which is great because next time process comes around,

07:07.140 --> 07:08.700
it will run the update.

07:09.060 --> 07:13.710
Now let's go back down into the update.

07:13.710 --> 07:17.670
So now we're coming to update with a random chance.

07:17.810 --> 07:19.600
We start patrolling.

07:19.880 --> 07:26.120
Then we set the stage event to exit, which is all really good because that's going to progress us to

07:26.120 --> 07:29.630
the next stage within our process.

07:30.530 --> 07:31.250
But does it?

07:31.250 --> 07:36.800
Because look down here, we're running this based update and if we go up and have a look what it does,

07:36.890 --> 07:44.180
it is going ahead and setting our stage back to update.

07:44.180 --> 07:47.780
So even though we've set it to exit, it's just going to run back to update.

07:47.780 --> 07:50.990
So we actually don't need to call it in here.

07:50.990 --> 07:59.690
And then doing it in here is just wrecking our code so you could get rid of it because it's not doing

07:59.690 --> 08:00.830
much for us.

08:01.160 --> 08:05.540
Or you could put it above this if statement and allow the if statement to then change it.

08:05.540 --> 08:07.580
So let's just get rid of that out of there.

08:07.580 --> 08:13.250
Now I will just check that I didn't do that in patrol as well because that will give us the same little

08:13.250 --> 08:18.230
situation later on because we're not actually switching out of the patrol.

08:18.230 --> 08:21.260
Once we go into patrol, we're stuck in there forever at the moment.

08:21.260 --> 08:29.210
Let's just get rid of this update because it's kind of superfluous setting update to update constantly.

08:30.400 --> 08:30.970
Okay.

08:30.970 --> 08:35.680
So now that we're confident or I'm confident that this is going to work, let's save it.

08:35.770 --> 08:43.480
We'll go back into Unity Press play and then let's see.

08:43.870 --> 08:48.280
So you can see he's immediately gone off on patrol.

08:48.730 --> 08:50.470
So 10% chance.

08:50.950 --> 08:52.720
That's checking for that.

08:52.720 --> 08:53.530
10% chance.

08:53.530 --> 08:54.730
Every single update.

08:54.730 --> 08:55.900
So that's quite quick.

08:56.380 --> 09:02.230
And now he's going to walk between all of the waypoints.

09:04.320 --> 09:12.090
Now he's not picking them up in order because it will depend on how the game environment goes ahead

09:12.090 --> 09:14.260
and updates those waypoints.

09:14.280 --> 09:17.100
He will, however, go through one by one.

09:17.100 --> 09:25.350
So in this case, the order that that get game objects with tag code that we've got in the game environment

09:25.350 --> 09:27.960
is getting them in a cross like pattern.

09:28.200 --> 09:33.840
Sometimes it does get them in a square pattern, but this time it's not.

09:34.440 --> 09:40.830
So he's just going to patrol and go between all of those points forever and ever.

09:43.950 --> 09:44.310
All right.

09:44.310 --> 09:50.100
Now, you're probably thinking at this point, how can I get my waypoints in the right order?

09:50.160 --> 09:55.260
Well, one way you can do it is position them in order according to their name.

09:55.260 --> 10:01.080
So if we have a look in the hierarchy, we've got these checkpoints like this and they're situated so

10:01.080 --> 10:05.880
that they go around the actual map.

10:06.360 --> 10:15.420
If you want to pick them up in alphabetical order, then you could use their name and use a system link

10:15.420 --> 10:16.660
in there instead.

10:16.680 --> 10:23.880
So if you go into your game environment's code where you're actually picking up those checkpoints to

10:23.880 --> 10:27.600
do that, you just have to add two lines of code, which I've already done here.

10:27.690 --> 10:30.030
The first one at the top is our library.

10:30.030 --> 10:37.740
So add in system dot link that will allow you to do a little order by on your list.

10:37.740 --> 10:41.100
And you can see that down here after we create the instance.

10:41.100 --> 10:51.630
So instant check points is going to equal instant checkpoints ordered by the waypoints name.

10:52.620 --> 10:57.120
And then we're just setting it back to list and putting it back into our checkpoints.

10:57.120 --> 11:04.380
So this will reorder them into alphabetical order, ascending alphabetical order of the name.

11:04.380 --> 11:09.810
So you could go through and rename them like waypoint one waypoint to 8.3, whatever you want, as long

11:09.810 --> 11:13.180
as they're alphabetical and then have that particular order.

11:13.200 --> 11:20.130
It's because we're doing a find game objects with TAG that we can't ever be assured of the actual order

11:20.130 --> 11:26.220
that we will get those game objects in, but we can then force an order using that.
