WEBVTT

00:02.710 --> 00:08.860
In the previous lecture, we put together our selector and we got to a point where we could get our

00:08.860 --> 00:10.910
character to move towards our doors.

00:10.930 --> 00:17.110
Now at this stage it's not the behaviour tree letting us down.

00:17.110 --> 00:21.040
Well, there's nothing really letting us down at the moment, but it's not the behaviour tree that we

00:21.040 --> 00:26.320
need to manipulate to fix the movement position of our character.

00:26.320 --> 00:32.350
But we have to extend the methods because we've got a behaviour tree but we're also trying to interact

00:32.350 --> 00:39.010
with a nav mesh and whatever you're doing in whatever type of game you've got, you're going to be interacting

00:39.010 --> 00:41.050
with the game environment itself.

00:41.050 --> 00:47.500
So you're going to have a code that kind of links behavior tree stuff to the actual game.

00:47.500 --> 00:52.900
Now if it was an animated first person shooter or something like that, then you would have to have

00:52.900 --> 00:58.120
a whole bunch of your own methods that call animation states and things like that.

00:58.120 --> 01:03.730
But in this case, we're really only concerned about nav mesh states and moving the nav mesh.

01:03.730 --> 01:11.110
Now, if we go back into our robber code, this is really this is the highest kind of level of the behavior

01:11.110 --> 01:11.380
code.

01:11.410 --> 01:17.530
This is the stuff that you want to write in here that interacts with your behavior tree.

01:17.530 --> 01:24.280
Once you've created your behavior tree structure like we've got, you shouldn't have to go in and touch

01:24.280 --> 01:24.430
that.

01:24.430 --> 01:29.830
You don't want to put any specialty nav mesh code inside of your behavior tree.

01:29.830 --> 01:32.980
It should be very generic that you can apply it to anything.

01:33.220 --> 01:41.410
So we're going to come back into our higher level robber behavior and actually extend our go to door

01:41.410 --> 01:47.710
methods, the back door in the front door down here, because we want to be able to go to the door,

01:47.710 --> 01:50.110
but also go through the door.

01:50.110 --> 01:55.510
So the going through the door is actually getting rid of that nav mesh obstacle.

01:55.510 --> 02:01.750
So this is something we need to build into these behaviors here or these methods that go into our actions,

02:01.780 --> 02:06.520
not something you would put in the actual behavior tree code itself.

02:06.670 --> 02:07.060
Right.

02:07.060 --> 02:14.830
So to begin with, we really need to put something on these two doors to give us an indication of whether

02:14.830 --> 02:16.230
they're there or not.

02:16.240 --> 02:23.260
Now, what I'm going to do is suggest that these doors can be locked and then in the future you could

02:23.260 --> 02:28.990
have a whole bunch of doors around the place and get your agent to check which door is unlocked in order

02:28.990 --> 02:30.010
to get through it.

02:30.010 --> 02:33.760
So we're going to create a new script so quickly.

02:33.760 --> 02:41.680
C-sharp script, let's call that lock, open that up in the editor and this is simply just going to

02:41.680 --> 02:49.660
hold a public bool is locked value let's set it to false initially.

02:50.470 --> 02:50.980
All right.

02:50.980 --> 02:52.060
So that's it.

02:52.360 --> 02:52.810
That's it.

02:52.810 --> 02:53.260
Yeah.

02:53.440 --> 02:54.220
Save that.

02:54.220 --> 02:56.440
Go back into unity.

02:56.440 --> 03:05.380
Now we're going to grab our front door and drag and drop that lock onto there and we will tick it immediately

03:05.380 --> 03:07.600
to say that the front door is locked.

03:07.720 --> 03:15.640
Then we'll get the back door and we'll put that lock over onto here and we won't have it locked.

03:15.640 --> 03:21.910
So if we go back into our behavior for our robber now at the top here, we're going to the front door.

03:22.060 --> 03:24.580
If that fails, we're then going to go to the back door.

03:24.580 --> 03:25.870
Okay, so the front door is locked.

03:25.870 --> 03:30.160
The behavior should be that it'll go to the front door, can't get in the front door and then go around

03:30.160 --> 03:32.200
to the back door, which is what we want there.

03:32.200 --> 03:38.710
Now, we're going to have to extend these two things here.

03:39.220 --> 03:44.800
If you add more doors, then you're going to have more of these particular methods.

03:44.800 --> 03:50.320
But in dealing with the door itself, it's locked.

03:50.320 --> 03:56.620
Behavior is going to be pretty much the same and carry across all of the doors.

03:56.620 --> 04:01.240
So what we'll do in here is give ourselves another method.

04:01.240 --> 04:12.370
So public node status go to door, which makes it a generic go to door and we'll pass through the game

04:12.520 --> 04:14.980
object of door.

04:15.310 --> 04:20.080
And if you have a whole bunch of objects that you have to interact with, which are basically the same

04:20.080 --> 04:26.770
type of object, you could put that sort of object behavior in here or the objects properties, I guess.

04:26.770 --> 04:29.950
So in this case, it's the door and it's whether it's locked or not.

04:30.160 --> 04:35.740
Okay, so we'll put in here no status s equals.

04:35.740 --> 04:44.650
Go to location and we will pass through our door dot transform position.

04:46.190 --> 04:57.440
Okay then if SX equals node dot status dot success, that means the agent has made it to the door.

04:57.470 --> 05:04.020
Now, in that case, we now have to add a little extra logic if door.

05:04.040 --> 05:14.960
Or should I say not door dot get component lock dot is locked.

05:15.230 --> 05:24.500
So if the door's not locked, then we're going to go door dot set active to false.

05:24.500 --> 05:26.560
This will actually get rid of the door.

05:26.570 --> 05:28.040
It's still going to exist.

05:28.040 --> 05:34.370
It's just going to be turned off, which will mean it won't be an obstacle on the nav mesh anymore.

05:34.520 --> 05:45.740
And then we're going to put return no dot status dot success because that's when we say we have succeeded

05:45.740 --> 05:46.590
in doing this.

05:46.610 --> 05:56.270
Now, if the door happens to be locked, then we want to go return node dot status dot failure, at

05:56.270 --> 06:04.550
which point this will go back to our sequence and tell our sequence that this particular door has failed.

06:04.700 --> 06:05.900
Go to the next door.

06:05.930 --> 06:07.060
Try the next door.

06:07.070 --> 06:07.520
Sorry.

06:07.520 --> 06:09.680
Not in the sequence, in the selector.

06:09.980 --> 06:11.270
So there's this one's failed.

06:11.270 --> 06:12.470
It's going to the next one.

06:12.680 --> 06:13.130
Okay.

06:13.130 --> 06:17.060
And then we're going to put an else and just return.

06:17.470 --> 06:23.720
So if this is still running, we still want to be returning the running from here.

06:23.720 --> 06:32.090
We can now replace, go to location in the go to back door and go to front door with our go to door.

06:32.090 --> 06:43.190
So let's put go to door and we're just going to pass through the back door and then go to door and pass

06:43.190 --> 06:47.810
through the front door down there.

06:47.840 --> 06:51.130
Okay, time to test this out, so save it.

06:51.140 --> 06:54.070
Let's go back into unity now.

06:54.080 --> 06:58.050
Still going to be going to the front door and then to the back door if need be.

06:58.070 --> 07:02.120
So press play and off he goes to the front door.

07:02.150 --> 07:03.680
Can't get in the front door.

07:03.690 --> 07:05.240
I'm going to go around the back door.

07:05.420 --> 07:10.670
So he's going around to try the back door and the back door is unlocked.

07:10.670 --> 07:19.160
We know that it's unlocked, gets rid of it and comes in, continues to get the diamond, getting the

07:19.160 --> 07:22.900
diamond and then making our getaway.

07:22.910 --> 07:27.890
Now, it doesn't say go to back door to get out because that backdoor obstacle is not there.

07:27.890 --> 07:35.600
The nav mesh actually will route the robber back through that way anyway because it becomes a path,

07:35.630 --> 07:38.660
because there's an obstacle over here, you can't actually use that.

07:39.320 --> 07:39.770
Great.

07:39.770 --> 07:41.700
So that's working really well.

07:41.720 --> 07:47.120
Now, one thing you might have noticed, my robber, if I click on him in this scenario, he's going

07:47.120 --> 07:48.770
a little bit faster than before.

07:48.770 --> 07:53.210
And that's because when you're testing this, it gets a little laborious waiting for your robber to,

07:53.240 --> 07:56.570
you know, move his self around your map.

07:56.570 --> 08:03.440
So all I've done is select the robber and down in the nav mesh agent set the speed to ten.

08:04.070 --> 08:04.540
Okay?

08:04.550 --> 08:13.670
So that is basically extending your code and your methods and where you should actually keep the interactions

08:13.670 --> 08:16.340
of the game engine stuff.

08:16.370 --> 08:16.590
Okay.

08:16.670 --> 08:23.720
So the NAV measures or animations or whatever you're doing separate and out of that behavior tree because

08:23.720 --> 08:26.600
you want to keep that as generic as possible.

08:27.380 --> 08:28.040
All right.

08:28.040 --> 08:30.650
Time for a challenge.

08:31.250 --> 08:39.980
What I'd like you to do is to extend your code so that when the robber gets to the diamond, the diamond

08:39.980 --> 08:45.440
disappears from your scene or he picks it up, whatever you want to do with it.

08:45.560 --> 08:46.020
Okay.

08:46.070 --> 08:54.470
So this is your challenge is to basically modify and let's go into the code, go to Diamond.

08:54.860 --> 08:58.070
So this is the one here that you're going to be working with.

08:58.340 --> 09:00.530
You've got the diamond object there.

09:00.920 --> 09:07.640
You want to pick it up or get rid of it when that is successful.

09:07.640 --> 09:09.140
Pause the video now give it a go.

09:09.140 --> 09:11.840
When we come back, I'll show you how to do that.

09:13.550 --> 09:14.090
Okay.

09:14.090 --> 09:14.770
Welcome back.

09:14.780 --> 09:16.250
How did you go?

09:16.250 --> 09:22.760
Well, we've got a similar situation with our go to diamond, as we do with our doors, pretty much

09:22.760 --> 09:28.280
with respect to the logic that's going to go on in here.

09:28.430 --> 09:34.580
If it is successful, then we don't want to return the success straight away.

09:34.580 --> 09:39.110
We actually just want to grab hold of the diamond.

09:39.830 --> 09:43.430
Okay, so let's go up into here and we'll modify this.

09:43.430 --> 09:44.990
So let's put in.

09:45.520 --> 09:47.200
Node status.

09:47.950 --> 09:55.270
S equals that and then we want if s is successful.

09:55.360 --> 10:07.300
So if s equals no status not successful, then what we're going to do is, well, you could disable

10:07.300 --> 10:13.120
the diamond or you could actually attach the diamond to the agent.

10:13.120 --> 10:16.930
So let's try and attach the diamond to the agent.

10:16.930 --> 10:28.450
So what we would do in this case is we would go diamond dot, transform dot parent equals this dot game

10:28.450 --> 10:30.670
object, dot transform.

10:31.930 --> 10:33.490
So that becomes the parent.

10:33.610 --> 10:40.090
Now otherwise we're going to do a return of SE down here.

10:40.420 --> 10:45.280
If it's successful, it's going to return successful if it's not going to return whatever running or.

10:45.280 --> 10:46.270
Yeah, okay.

10:46.270 --> 10:47.680
So save that.

10:47.680 --> 10:52.270
Let's go back into unity and let's try this out.

10:52.270 --> 10:55.300
So we're going to press play.

10:56.020 --> 11:00.670
We're going to go into the back door again.

11:00.910 --> 11:06.220
Now, the diamond in this case is the entire plinth with the diamond on top of it.

11:06.220 --> 11:06.940
So the whole lot.

11:06.940 --> 11:08.350
So going to go.

11:08.740 --> 11:13.270
If you wanted to grab just the diamond object, you'd have to dig around in the plinth hierarchy over

11:13.270 --> 11:14.320
here and grab that.

11:14.590 --> 11:19.030
But you'd have to have the diamond as a separate game object that you passed into the script.

11:19.030 --> 11:23.500
Because the plinth is the thing that's actually on the NAV mission.

11:23.500 --> 11:28.240
You have to make your way to the plinth, but it doesn't mean you can't pick up the diamond.

11:28.240 --> 11:34.660
You just can't use the purple diamond itself as your nav mesh destination because it's not on the nav

11:34.660 --> 11:35.260
mesh.

11:35.680 --> 11:36.000
Okay.

11:36.010 --> 11:40.510
So he's grabbed the diamond and off he goes with it.

11:42.910 --> 11:43.300
Now.

11:43.300 --> 11:43.720
He's.

11:43.720 --> 11:44.260
He's.

11:44.500 --> 11:49.660
Yeah, he's skidding around a bit because of the speed that I've got up so fast, and I haven't really

11:49.660 --> 11:52.390
compensated for that, but that's working.

11:52.390 --> 11:54.730
So how did you go with that challenge?

11:54.730 --> 11:56.110
I hope you figured that out.

11:56.110 --> 12:02.740
Now you can extend that even more if you want to steal any of these paintings that are in the scene,

12:02.950 --> 12:06.030
because you just have to do the same thing that you did with Door.

12:06.430 --> 12:13.120
So instead of go to Diamond, be like go to art work, and then you could go to any one of these particular

12:13.120 --> 12:16.960
ones, steal it, pick it up and take it out of the gallery.
