In the previous lecture, we put together our selector and we got to a point where we could get our
character to move towards our doors.
Now at this stage it's not the behaviour tree letting us down.
Well, there's nothing really letting us down at the moment, but it's not the behaviour tree that we
need to manipulate to fix the movement position of our character.
But we have to extend the methods because we've got a behaviour tree but we're also trying to interact
with a nav mesh and whatever you're doing in whatever type of game you've got, you're going to be interacting
with the game environment itself.
So you're going to have a code that kind of links behavior tree stuff to the actual game.
Now if it was an animated first person shooter or something like that, then you would have to have
a whole bunch of your own methods that call animation states and things like that.
But in this case, we're really only concerned about nav mesh states and moving the nav mesh.
Now, if we go back into our robber code, this is really this is the highest kind of level of the behavior
code.
This is the stuff that you want to write in here that interacts with your behavior tree.
Once you've created your behavior tree structure like we've got, you shouldn't have to go in and touch
that.
You don't want to put any specialty nav mesh code inside of your behavior tree.
It should be very generic that you can apply it to anything.
So we're going to come back into our higher level robber behavior and actually extend our go to door
methods, the back door in the front door down here, because we want to be able to go to the door,
but also go through the door.
So the going through the door is actually getting rid of that nav mesh obstacle.
So this is something we need to build into these behaviors here or these methods that go into our actions,
not something you would put in the actual behavior tree code itself.
Right.
So to begin with, we really need to put something on these two doors to give us an indication of whether
they're there or not.
Now, what I'm going to do is suggest that these doors can be locked and then in the future you could
have a whole bunch of doors around the place and get your agent to check which door is unlocked in order
to get through it.
So we're going to create a new script so quickly.
C-sharp script, let's call that lock, open that up in the editor and this is simply just going to
hold a public bool is locked value let's set it to false initially.
All right.
So that's it.
That's it.
Yeah.
Save that.
Go back into unity.
Now we're going to grab our front door and drag and drop that lock onto there and we will tick it immediately
to say that the front door is locked.
Then we'll get the back door and we'll put that lock over onto here and we won't have it locked.
So if we go back into our behavior for our robber now at the top here, we're going to the front door.
If that fails, we're then going to go to the back door.
Okay, so the front door is locked.
The behavior should be that it'll go to the front door, can't get in the front door and then go around
to the back door, which is what we want there.
Now, we're going to have to extend these two things here.
If you add more doors, then you're going to have more of these particular methods.
But in dealing with the door itself, it's locked.
Behavior is going to be pretty much the same and carry across all of the doors.
So what we'll do in here is give ourselves another method.
So public node status go to door, which makes it a generic go to door and we'll pass through the game
object of door.
And if you have a whole bunch of objects that you have to interact with, which are basically the same
type of object, you could put that sort of object behavior in here or the objects properties, I guess.
So in this case, it's the door and it's whether it's locked or not.
Okay, so we'll put in here no status s equals.
Go to location and we will pass through our door dot transform position.
Okay then if SX equals node dot status dot success, that means the agent has made it to the door.
Now, in that case, we now have to add a little extra logic if door.
Or should I say not door dot get component lock dot is locked.
So if the door's not locked, then we're going to go door dot set active to false.
This will actually get rid of the door.
It's still going to exist.
It's just going to be turned off, which will mean it won't be an obstacle on the nav mesh anymore.
And then we're going to put return no dot status dot success because that's when we say we have succeeded
in doing this.
Now, if the door happens to be locked, then we want to go return node dot status dot failure, at
which point this will go back to our sequence and tell our sequence that this particular door has failed.
Go to the next door.
Try the next door.
Sorry.
Not in the sequence, in the selector.
So there's this one's failed.
It's going to the next one.
Okay.
And then we're going to put an else and just return.
So if this is still running, we still want to be returning the running from here.
We can now replace, go to location in the go to back door and go to front door with our go to door.
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
through the front door down there.
Okay, time to test this out, so save it.
Let's go back into unity now.
Still going to be going to the front door and then to the back door if need be.
So press play and off he goes to the front door.
Can't get in the front door.
I'm going to go around the back door.
So he's going around to try the back door and the back door is unlocked.
We know that it's unlocked, gets rid of it and comes in, continues to get the diamond, getting the
diamond and then making our getaway.
Now, it doesn't say go to back door to get out because that backdoor obstacle is not there.
The nav mesh actually will route the robber back through that way anyway because it becomes a path,
because there's an obstacle over here, you can't actually use that.
Great.
So that's working really well.
Now, one thing you might have noticed, my robber, if I click on him in this scenario, he's going
a little bit faster than before.
And that's because when you're testing this, it gets a little laborious waiting for your robber to,
you know, move his self around your map.
So all I've done is select the robber and down in the nav mesh agent set the speed to ten.
Okay?
So that is basically extending your code and your methods and where you should actually keep the interactions
of the game engine stuff.
Okay.
So the NAV measures or animations or whatever you're doing separate and out of that behavior tree because
you want to keep that as generic as possible.
All right.
Time for a challenge.
What I'd like you to do is to extend your code so that when the robber gets to the diamond, the diamond
disappears from your scene or he picks it up, whatever you want to do with it.
Okay.
So this is your challenge is to basically modify and let's go into the code, go to Diamond.
So this is the one here that you're going to be working with.
You've got the diamond object there.
You want to pick it up or get rid of it when that is successful.
Pause the video now give it a go.
When we come back, I'll show you how to do that.
Okay.
Welcome back.
How did you go?
Well, we've got a similar situation with our go to diamond, as we do with our doors, pretty much
with respect to the logic that's going to go on in here.
If it is successful, then we don't want to return the success straight away.
We actually just want to grab hold of the diamond.
Okay, so let's go up into here and we'll modify this.
So let's put in.
Node status.
S equals that and then we want if s is successful.
So if s equals no status not successful, then what we're going to do is, well, you could disable
the diamond or you could actually attach the diamond to the agent.
So let's try and attach the diamond to the agent.
So what we would do in this case is we would go diamond dot, transform dot parent equals this dot game
object, dot transform.
So that becomes the parent.
Now otherwise we're going to do a return of SE down here.
If it's successful, it's going to return successful if it's not going to return whatever running or.
Yeah, okay.
So save that.
Let's go back into unity and let's try this out.
So we're going to press play.
We're going to go into the back door again.
Now, the diamond in this case is the entire plinth with the diamond on top of it.
So the whole lot.
So going to go.
If you wanted to grab just the diamond object, you'd have to dig around in the plinth hierarchy over
here and grab that.
But you'd have to have the diamond as a separate game object that you passed into the script.
Because the plinth is the thing that's actually on the NAV mission.
You have to make your way to the plinth, but it doesn't mean you can't pick up the diamond.
You just can't use the purple diamond itself as your nav mesh destination because it's not on the nav
mesh.
Okay.
So he's grabbed the diamond and off he goes with it.
Now.
He's.
Yeah, he's skidding around a bit because of the speed that I've got up so fast, and I haven't really
compensated for that, but that's working.
So how did you go with that challenge?
I hope you figured that out.
Now you can extend that even more if you want to steal any of these paintings that are in the scene,
because you just have to do the same thing that you did with Door.
So instead of go to Diamond, be like go to art work, and then you could go to any one of these particular
ones, steal it, pick it up and take it out of the gallery.