WEBVTT

00:03.330 --> 00:11.370
In the previous lecture, we programmed in our leaf node and we also did our robber behavior to get

00:11.370 --> 00:13.710
our agent to move towards the diamond.

00:13.740 --> 00:19.800
Now, what we've done is just kind of put something in there that works and we're not going to actually

00:19.800 --> 00:20.890
keep it in there.

00:20.910 --> 00:26.370
The reason being, if we go back into the code here, currently what we're doing is we're running this

00:26.370 --> 00:34.890
tree process which then goes and runs the steel, which then goes down to its child and runs go to Diamond.

00:35.280 --> 00:38.820
Now this immediately returns success here.

00:39.020 --> 00:44.850
Okay, now it can't return success until the agent is actually at the diamond.

00:44.850 --> 00:49.440
So anything could happen on the way to the destination.

00:49.620 --> 00:56.130
In addition, because we're using NAV measures, a lot of your methods that you put in here are going

00:56.130 --> 01:00.870
to look like move to Diamond, move to Van, move to here, move to there.

01:00.870 --> 01:07.290
And therefore, we need sort of to have one method that just looks after our movement on the nav mesh.

01:07.290 --> 01:12.720
So it actually going to create that first before we continue with our behavior tree.

01:12.810 --> 01:15.390
Otherwise, we're going to end up in a bit of a mess.

01:15.450 --> 01:15.960
Okay.

01:15.960 --> 01:25.530
So what we'll do initially is determine if the agent is actually moving or he's idle.

01:25.860 --> 01:33.420
So if he's moving, then he's actually obviously doing a particular behavior, in which case you don't

01:33.420 --> 01:39.240
want to go and set the destination for somewhere else while he's in the middle of moving to the diamond,

01:39.240 --> 01:42.510
for example, because he'll just stop that and then move somewhere else.

01:42.660 --> 01:50.820
So at the top here, what I'm going to do is add in another little enum to track what our agent is doing,

01:50.820 --> 02:01.200
so public in action state or call it, and we're either going to be idle or working.

02:01.440 --> 02:02.460
So it's very simple.

02:02.460 --> 02:09.630
There's just two states that the agent will be in and then we'll create a variable to hold that.

02:09.630 --> 02:17.340
So action state, state equals action state idle.

02:17.340 --> 02:20.970
So we'll start out as being idle right now.

02:20.970 --> 02:26.970
We can use this by setting it between idle and working to trigger when something is actually going to

02:26.970 --> 02:27.560
occur.

02:27.570 --> 02:35.430
So we'll come down to the bottom here underneath our go tos and put in our new method for traversing

02:35.430 --> 02:36.900
the nav mesh.

02:37.350 --> 02:49.530
So this would be node dot status, go to location and that then will have a vector three for destination.

02:51.060 --> 02:53.910
So that's where we're going to go to and to pass that through.

02:54.000 --> 02:58.320
Now, well, first of all, work out how far away we are from our destination.

02:58.320 --> 03:06.330
So float distance to target equals vector three.

03:06.930 --> 03:13.020
And that's the difference between destination and our agent's current position.

03:13.020 --> 03:23.160
So this dot transform position now there's a lot of manual coding in here to work with the actual nav

03:23.160 --> 03:23.670
mesh.

03:23.670 --> 03:29.370
The nav mesh does have a whole bunch of operations that come with it that will tell you how far there

03:29.370 --> 03:34.560
is to the destination and whether it's running and whether there's a path and all of that.

03:34.560 --> 03:38.340
But it's notorious for having a lot of issues with it.

03:38.340 --> 03:45.240
So doing all of these things manually with our own code, we can be absolutely sure of the values that

03:45.240 --> 03:45.750
we're getting.

03:45.750 --> 03:49.710
So this here should actually be vector three dot distance.

03:49.800 --> 03:59.130
And then once we've done that, we're going to say if state equals action, dot, state, dot idle.

04:02.180 --> 04:08.990
Then we will go agent set destination to our destination.

04:09.890 --> 04:15.170
And this will also mean that this will only run once when you're idle.

04:15.170 --> 04:20.090
If you're working and this comes in here again, it's not going to constantly keep setting the same

04:20.090 --> 04:25.340
destination over and over again, which is going to save a little bit of processing time for you.

04:25.370 --> 04:29.900
So let's turn this to state equals action.

04:29.900 --> 04:31.880
State dot working.

04:32.710 --> 04:38.360
Okay, so now if we're not idle, it means we must be on our way to a destination.

04:38.360 --> 04:44.900
So else if vector three dot distance.

04:44.900 --> 04:56.420
And this time we'll look for the distance between the agent dot path and position and the destination

04:58.040 --> 05:02.900
is greater than or equal to, let's say two.

05:03.500 --> 05:10.160
Then we haven't made it to the destination and therefore the agent has failed.

05:10.160 --> 05:12.530
So we'll set state back to idle.

05:12.530 --> 05:16.880
State equals action, state idle.

05:17.450 --> 05:22.620
And we will now return node status failure.

05:22.790 --> 05:31.460
Okay, remember whatever we doing, we always have to return running failure or success.

05:31.790 --> 05:35.530
So in this case we're going to be returning failure.

05:35.540 --> 05:44.840
And then down in here we're going to go else if dist to target to target as we created before is less

05:44.840 --> 06:00.680
than two, then we are successful and in here put state equals again action state idle and return no

06:00.680 --> 06:05.540
dot status dot success.

06:07.520 --> 06:16.160
And then the very first time we run through this and we set state to working and this destination here,

06:16.160 --> 06:23.390
we're going to then return no dot status dot running.

06:24.140 --> 06:32.450
Now this state of running here is actually going to prevent us coming back in here and checking this

06:32.480 --> 06:34.400
immediately again.

06:34.640 --> 06:36.530
Anyway, we'll get around to using that in a moment.

06:36.530 --> 06:37.750
So just save that.

06:37.760 --> 06:45.590
Now we can actually use go to location if we come up into our go to Diamond and go to Van.

06:45.860 --> 06:54.470
So here is a little challenge for you, is to rewrite these two methods so that they don't use that,

06:54.470 --> 06:58.490
but they actually use this new go to location method that we just created.

06:58.490 --> 07:00.530
So pause the video, go off and do that.

07:00.530 --> 07:04.190
And when we come back, I'll show you how to do that.

07:05.630 --> 07:06.200
Okay.

07:06.200 --> 07:06.890
Welcome back.

07:06.890 --> 07:08.000
How did you go?

07:08.000 --> 07:11.630
Well, we're basically getting rid of our destination up here.

07:11.900 --> 07:20.630
And all we need to do is just return, go to location and, of course, spell it correctly.

07:20.630 --> 07:26.750
And in here, we're going to send through our diamond dot transformed position.

07:29.430 --> 07:31.100
And that's it pretty much.

07:31.110 --> 07:40.680
And then we want that again down in the van and in here, we're just going to have van don't transform

07:40.860 --> 07:41.190
position.

07:41.190 --> 07:46.440
So any of these things that you create go to Diamond and go to vent are going to now use this go to

07:46.440 --> 07:54.360
location, which means you can control everything as far as the nav mesh movement from in here is going

07:54.360 --> 07:57.810
to make life an awful lot easier.

07:57.930 --> 08:03.090
Okay, so we've done a little bit of housekeeping with our nav mesh movement code, which we'll be able

08:03.090 --> 08:04.650
to use over and over again.

08:04.770 --> 08:10.170
Currently, what you've got isn't going to run the way you expect it to, so don't even try.

08:10.170 --> 08:14.670
In the next video we will come back and we will actually link that.

08:14.670 --> 08:21.000
Go to Diamond and go to Van properly by creating a sequence node.
