WEBVTT

00:03.010 --> 00:07.360
In the previous lecture, we created a sequence.

00:07.360 --> 00:10.070
This time we're going to create a selector.

00:10.090 --> 00:15.640
Now, a selector is kind of the opposite of a sequence.

00:15.640 --> 00:16.390
Well, not quite.

00:16.390 --> 00:24.910
But a sequence is like an end statement where everything has to be successful in order for the actual

00:24.910 --> 00:31.270
node, the sequence node itself to be successful a selector is an or statement, so it only needs one

00:31.300 --> 00:33.670
of the things to be successful.

00:33.670 --> 00:42.100
So as it goes through its list of children, if one fails, it will keep trying until it either gets

00:42.100 --> 00:47.980
to the end or one of them is successful because that's all it needs.

00:48.250 --> 00:54.700
Now what we're going to do for our selector is to select from the front door or the back door.

00:54.730 --> 01:01.990
The first thing we need to do is actually make these items part of our nav mesh so that they work properly

01:01.990 --> 01:02.380
for us.

01:02.380 --> 01:06.340
Because as you saw before, you actually can walk straight through that front door if you're trying

01:06.340 --> 01:07.750
to get to the back door.

01:07.780 --> 01:16.060
So let's go to the front door and over in the inspector, I want you to tick the nav mesh obstacle,

01:16.060 --> 01:20.150
which I will have had turned off to make this part of the nav mesh.

01:20.170 --> 01:26.710
Now, you'll also have to tick the carve box so that it will actually affect the path of the agent.

01:26.980 --> 01:32.170
You can see here in the inspector that this is not drawing properly on my screen.

01:32.170 --> 01:34.870
There is a carved checkbox down there.

01:34.870 --> 01:38.440
And if I actually bring this out and make it a bit bigger, you'll see it pops up.

01:38.950 --> 01:39.760
Tick that.

01:39.760 --> 01:42.520
Then you want to go to the back door and do the same thing.

01:42.520 --> 01:47.950
So just make sure you've turned on the nav mesh obstacle and also clicked on carve.

01:48.280 --> 01:48.670
Okay.

01:48.670 --> 01:50.740
Now that will help us with that.

01:50.740 --> 01:59.230
Now if we go back into our robber behaviour, we can add in a go to front door as well.

01:59.230 --> 02:05.980
So I'm going to leave this as a challenge for you is to set this up so that you've got a go to backdoor,

02:05.980 --> 02:09.700
which you'll already have, but you've got to go to front door as well.

02:09.730 --> 02:11.730
Pause the video when we come back.

02:11.740 --> 02:13.660
I'll go through those steps for you.

02:15.510 --> 02:15.950
All right.

02:15.960 --> 02:17.340
How did you go with that?

02:17.350 --> 02:21.690
Well, it's just a matter of copying what you've got for the back door already there.

02:21.690 --> 02:28.470
So let's add in a public game object, front door just up here.

02:28.650 --> 02:33.410
Then we'll come down and add in a go to front door.

02:33.510 --> 02:42.390
So let's just copy this one and make that go to front.

02:43.480 --> 02:44.240
To war.

02:44.420 --> 02:47.540
And then we'll change the text.

02:47.570 --> 02:54.020
Go to the front door and go to front door there as well.

02:54.210 --> 02:54.680
Okay.

02:54.680 --> 02:59.810
So it's wanting you to obviously create go to front door as your method.

02:59.810 --> 03:08.090
So we'll go down here and just copy the back door one and put in the front door down here.

03:08.780 --> 03:15.350
And then we want to go to the front door with that and save that.

03:15.350 --> 03:23.810
Now, as far as adding these in to the nodes, we don't yet have a selector node.

03:23.810 --> 03:30.920
So we want to add the selector node in somewhere and then put for it to select, go to front door or

03:30.920 --> 03:32.450
go to back door.

03:33.020 --> 03:36.140
That means we have to create a selector.

03:36.440 --> 03:38.570
So before we do that, let's just save it.

03:38.570 --> 03:39.920
Switch back to Unity.

03:39.920 --> 03:47.810
And before I forget to do this, I'm going to select the robber and find my front door, which is down

03:47.810 --> 03:53.360
here in the inspector and drag and drop that front door over there so it's ready to go.

03:54.290 --> 03:58.070
Okay, so let's create our selector.

03:58.160 --> 04:05.210
So create a new C-sharp script, call it selector, and then we'll open that up in the editor.

04:05.210 --> 04:08.930
And of course, the selector is going to be a node.

04:10.100 --> 04:15.350
Then we can get rid of these and you can write the rest.

04:15.350 --> 04:16.250
And I'm not kidding.

04:16.250 --> 04:17.450
This is your challenge.

04:17.690 --> 04:19.310
You know how to create a sequence.

04:19.310 --> 04:20.330
Let's go and have a look at the sequence.

04:20.330 --> 04:22.130
Now the sequence is here.

04:22.130 --> 04:24.920
And I said, it's like an end statement.

04:24.920 --> 04:28.880
So all of them have to be true or successful.

04:28.880 --> 04:35.210
All the children that is in order for the actual process itself to return successful.

04:35.210 --> 04:38.840
So this is going to stop as soon as it hits a successful.

04:38.840 --> 04:42.110
It's not going to go on to any of the other children.

04:42.320 --> 04:50.390
The selector is going to do a similar thing as far as start at the first child, run the first child.

04:50.390 --> 04:55.580
Now, if that child fails, it's not going to fail the selector.

04:55.820 --> 05:02.210
It's just going to go to the next child and keep going through the children until it reaches the end.

05:02.210 --> 05:04.190
At which point it's going to say, okay, I failed.

05:04.190 --> 05:10.460
If it didn't find any successful children or as soon as it finds a successful child, it will return

05:10.460 --> 05:11.870
at that point.

05:12.500 --> 05:13.010
Okay.

05:13.010 --> 05:22.820
So following or even maybe starting with what's in the sequence, you can rejig this to create the selector

05:22.820 --> 05:23.900
code.

05:24.200 --> 05:28.430
Pause the video now and when we come back, I will go through that for you.

05:29.540 --> 05:30.200
Okay.

05:30.200 --> 05:31.070
Welcome back.

05:31.070 --> 05:32.030
How did you go?

05:32.030 --> 05:35.120
Did you figure out how to get it to work?

05:35.270 --> 05:36.770
Let's start at the beginning.

05:36.800 --> 05:48.410
First of all, I'm going to create a public selector and the name so string in coming through here and

05:48.410 --> 05:56.210
then we will create a name and then we'll say name equals MN.

05:56.840 --> 05:58.730
Okay, let's get into this process.

05:58.730 --> 06:05.330
So it's going to be a public override status.

06:07.090 --> 06:08.050
Process.

06:08.710 --> 06:11.810
Pretty much the same as the selector so far.

06:11.830 --> 06:13.960
Let's get rid of that return of the base.

06:14.320 --> 06:24.830
And we're going to say here that status, child status equals children, current child.

06:24.850 --> 06:34.960
Now, this will be the first child in the list process and it's going to start running that.

06:36.430 --> 06:48.100
Now we're going to say if child status equals status dot running, then we're in the middle of performing

06:48.100 --> 06:48.640
a task.

06:48.640 --> 06:53.830
So we will return status dot running.

06:54.010 --> 06:55.630
Otherwise we will test.

06:55.630 --> 07:07.630
If child status equals status dot successful, then we've pretty much finished the selection and we'll

07:07.630 --> 07:10.360
go current child equals zero.

07:10.360 --> 07:17.230
So we're setting it back to the starting point for ourselves if we use this again and then return status

07:17.740 --> 07:18.340
dot.

07:19.450 --> 07:20.560
Success.

07:20.830 --> 07:25.360
Now, if it's not successful, then it's failed, obviously.

07:25.360 --> 07:27.460
And we want to go to the next child.

07:27.460 --> 07:31.660
So current child plus plus.

07:32.170 --> 07:45.310
Then we'll say if the current child is greater than or equal to the children don't count, then we want

07:45.970 --> 07:47.800
current child equals zero.

07:47.800 --> 07:52.450
So resetting that once more and we're going to return fail.

07:52.450 --> 07:54.580
So status failure.

07:55.270 --> 07:59.590
Otherwise we're going to return status stop running.

07:59.980 --> 08:06.040
Okay, so we're looking for the first child through the list of children that is successful.

08:06.460 --> 08:09.310
Okay, so we will save this.

08:09.340 --> 08:19.060
Now we have a selector so we can go back into our robber behavior and add that into our tree.

08:19.600 --> 08:20.080
All right.

08:20.080 --> 08:24.370
So we're going to need a selector node.

08:24.370 --> 08:32.740
So under these leafs, I'm going to put selector and let's call that open door, which will equal a

08:32.740 --> 08:37.990
new selector called Open Door.

08:39.370 --> 08:46.150
Now the open door is going to include go to back door and go to front door.

08:46.150 --> 08:54.370
So I'm going to put open door, dot, add child, go to back.

08:55.170 --> 08:55.920
Door.

08:58.090 --> 08:59.980
And open door.

09:00.340 --> 09:02.200
Add child.

09:02.890 --> 09:05.800
Go to front door.

09:07.930 --> 09:13.350
So instead now with this steal of adding the child, go to backdoor.

09:13.360 --> 09:17.980
You actually going to add in this open door like that?

09:17.980 --> 09:24.400
And then when it gets to the open door node, it will then have a choice to make between these two.

09:24.430 --> 09:26.890
Now it will go through them in order.

09:26.890 --> 09:33.520
So it's going to go to the back door first and then go to the front door because we've turned on the

09:33.520 --> 09:40.810
front door and the back door as obstacles in our mesh, the nav mesh will take us to one of these locations,

09:40.810 --> 09:45.310
but it won't actually be able to continue to get to the diamond.

09:45.790 --> 09:47.520
But that's okay at the moment.

09:47.530 --> 09:55.240
Now, in order to go back to the van, we could actually get rid of going back to the door because it

09:55.240 --> 10:00.700
will just create a path to get back there as easily as it can.

10:01.060 --> 10:06.320
Which by this point means that one of those doors is going to be hopefully open.

10:06.340 --> 10:13.300
Now, we haven't got to that dynamic just yet, but for now, let's just comment that out and we will

10:13.330 --> 10:17.380
end up planning a path from the diamond straight back to the van.

10:17.890 --> 10:21.070
Let's just save this, because I just want to see and show you about this.

10:21.070 --> 10:23.470
Back and front doors working.

10:23.470 --> 10:23.700
Okay.

10:23.770 --> 10:28.900
So the agent should go to the back door first in this case.

10:28.900 --> 10:30.070
So let's go back here.

10:30.340 --> 10:34.920
We'll press play and he's going to go to the back door.

10:35.170 --> 10:40.060
So you can see now that is completely skipping over, going there and he's going to head around to the

10:40.060 --> 10:45.190
back door, won't be able to get through it because it is actually now an obstacle blocking the way

10:45.640 --> 10:47.380
and stop playing.

10:47.380 --> 10:52.110
If we go back into the code, we can actually now swap those around.

10:52.120 --> 11:02.110
So let's just put the front door first, copy that and go up here, put that there and remove the other

11:02.110 --> 11:02.650
one.

11:03.190 --> 11:06.730
Save it and we'll go back and press play.

11:06.730 --> 11:12.940
And this time he'll head off towards this door here, as you can see.

11:13.750 --> 11:18.490
Just quickly, I want to show you the behavior you're probably getting that you might be questioning.

11:18.490 --> 11:20.200
Let's just press play.

11:20.290 --> 11:26.950
And what's happening here is he's going to the front door and then what happens is he goes around the

11:26.950 --> 11:31.390
side here and apparently gets stuck on this wall.

11:31.390 --> 11:36.580
So it isn't that he tried to go to the front door and he couldn't and it failed and he went over here.

11:36.580 --> 11:41.170
He actually did get to the front door because as far as that nav mesh is concerned, he made it to that

11:41.170 --> 11:42.430
particular location.

11:42.430 --> 11:46.360
Now, what does he do after successful going to the front door?

11:46.390 --> 11:48.010
He tries to go to the diamond.

11:48.010 --> 11:50.530
So this is him trying to go to the diamond.

11:50.530 --> 11:56.860
But there is no way to get in here now because we've got these two nav mesh obstacles in the road.

11:56.860 --> 11:59.170
But that's okay because we're going to work on this.

11:59.170 --> 12:02.980
So if you've got this behavior, just know it's not him trying to get to the back door.

12:02.980 --> 12:07.180
It was him trying to continue his behavior to get to the diamond.

12:07.180 --> 12:09.100
And he actually got stuck.

12:10.400 --> 12:10.760
Okay.

12:10.760 --> 12:15.020
So that's what the Select is doing is just picking one of those things.

12:15.020 --> 12:20.150
Now you can't see what happens if one of them is locked at the moment and he can't get through and he

12:20.150 --> 12:25.010
actually has to then go to the next one in the selection child list.

12:25.400 --> 12:30.740
So we will come back in the next lecture and work on that part.
