In the previous lecture, we programmed in our leaf node and we also did our robber behavior to get
our agent to move towards the diamond.
Now, what we've done is just kind of put something in there that works and we're not going to actually
keep it in there.
The reason being, if we go back into the code here, currently what we're doing is we're running this
tree process which then goes and runs the steel, which then goes down to its child and runs go to Diamond.
Now this immediately returns success here.
Okay, now it can't return success until the agent is actually at the diamond.
So anything could happen on the way to the destination.
In addition, because we're using NAV measures, a lot of your methods that you put in here are going
to look like move to Diamond, move to Van, move to here, move to there.
And therefore, we need sort of to have one method that just looks after our movement on the nav mesh.
So it actually going to create that first before we continue with our behavior tree.
Otherwise, we're going to end up in a bit of a mess.
Okay.
So what we'll do initially is determine if the agent is actually moving or he's idle.
So if he's moving, then he's actually obviously doing a particular behavior, in which case you don't
want to go and set the destination for somewhere else while he's in the middle of moving to the diamond,
for example, because he'll just stop that and then move somewhere else.
So at the top here, what I'm going to do is add in another little enum to track what our agent is doing,
so public in action state or call it, and we're either going to be idle or working.
So it's very simple.
There's just two states that the agent will be in and then we'll create a variable to hold that.
So action state, state equals action state idle.
So we'll start out as being idle right now.
We can use this by setting it between idle and working to trigger when something is actually going to
occur.
So we'll come down to the bottom here underneath our go tos and put in our new method for traversing
the nav mesh.
So this would be node dot status, go to location and that then will have a vector three for destination.
So that's where we're going to go to and to pass that through.
Now, well, first of all, work out how far away we are from our destination.
So float distance to target equals vector three.
And that's the difference between destination and our agent's current position.
So this dot transform position now there's a lot of manual coding in here to work with the actual nav
mesh.
The nav mesh does have a whole bunch of operations that come with it that will tell you how far there
is to the destination and whether it's running and whether there's a path and all of that.
But it's notorious for having a lot of issues with it.
So doing all of these things manually with our own code, we can be absolutely sure of the values that
we're getting.
So this here should actually be vector three dot distance.
And then once we've done that, we're going to say if state equals action, dot, state, dot idle.
Then we will go agent set destination to our destination.
And this will also mean that this will only run once when you're idle.
If you're working and this comes in here again, it's not going to constantly keep setting the same
destination over and over again, which is going to save a little bit of processing time for you.
So let's turn this to state equals action.
State dot working.
Okay, so now if we're not idle, it means we must be on our way to a destination.
So else if vector three dot distance.
And this time we'll look for the distance between the agent dot path and position and the destination
is greater than or equal to, let's say two.
Then we haven't made it to the destination and therefore the agent has failed.
So we'll set state back to idle.
State equals action, state idle.
And we will now return node status failure.
Okay, remember whatever we doing, we always have to return running failure or success.
So in this case we're going to be returning failure.
And then down in here we're going to go else if dist to target to target as we created before is less
than two, then we are successful and in here put state equals again action state idle and return no
dot status dot success.
And then the very first time we run through this and we set state to working and this destination here,
we're going to then return no dot status dot running.
Now this state of running here is actually going to prevent us coming back in here and checking this
immediately again.
Anyway, we'll get around to using that in a moment.
So just save that.
Now we can actually use go to location if we come up into our go to Diamond and go to Van.
So here is a little challenge for you, is to rewrite these two methods so that they don't use that,
but they actually use this new go to location method that we just created.
So pause the video, go off and do that.
And when we come back, I'll show you how to do that.
Okay.
Welcome back.
How did you go?
Well, we're basically getting rid of our destination up here.
And all we need to do is just return, go to location and, of course, spell it correctly.
And in here, we're going to send through our diamond dot transformed position.
And that's it pretty much.
And then we want that again down in the van and in here, we're just going to have van don't transform
position.
So any of these things that you create go to Diamond and go to vent are going to now use this go to
location, which means you can control everything as far as the nav mesh movement from in here is going
to make life an awful lot easier.
Okay, so we've done a little bit of housekeeping with our nav mesh movement code, which we'll be able
to use over and over again.
Currently, what you've got isn't going to run the way you expect it to, so don't even try.
In the next video we will come back and we will actually link that.
Go to Diamond and go to Van properly by creating a sequence node.