In the previous lecture, we created a sequence.
This time we're going to create a selector.
Now, a selector is kind of the opposite of a sequence.
Well, not quite.
But a sequence is like an end statement where everything has to be successful in order for the actual
node, the sequence node itself to be successful a selector is an or statement, so it only needs one
of the things to be successful.
So as it goes through its list of children, if one fails, it will keep trying until it either gets
to the end or one of them is successful because that's all it needs.
Now what we're going to do for our selector is to select from the front door or the back door.
The first thing we need to do is actually make these items part of our nav mesh so that they work properly
for us.
Because as you saw before, you actually can walk straight through that front door if you're trying
to get to the back door.
So let's go to the front door and over in the inspector, I want you to tick the nav mesh obstacle,
which I will have had turned off to make this part of the nav mesh.
Now, you'll also have to tick the carve box so that it will actually affect the path of the agent.
You can see here in the inspector that this is not drawing properly on my screen.
There is a carved checkbox down there.
And if I actually bring this out and make it a bit bigger, you'll see it pops up.
Tick that.
Then you want to go to the back door and do the same thing.
So just make sure you've turned on the nav mesh obstacle and also clicked on carve.
Okay.
Now that will help us with that.
Now if we go back into our robber behaviour, we can add in a go to front door as well.
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,
which you'll already have, but you've got to go to front door as well.
Pause the video when we come back.
I'll go through those steps for you.
All right.
How did you go with that?
Well, it's just a matter of copying what you've got for the back door already there.
So let's add in a public game object, front door just up here.
Then we'll come down and add in a go to front door.
So let's just copy this one and make that go to front.
To war.
And then we'll change the text.
Go to the front door and go to front door there as well.
Okay.
So it's wanting you to obviously create go to front door as your method.
So we'll go down here and just copy the back door one and put in the front door down here.
And then we want to go to the front door with that and save that.
Now, as far as adding these in to the nodes, we don't yet have a selector node.
So we want to add the selector node in somewhere and then put for it to select, go to front door or
go to back door.
That means we have to create a selector.
So before we do that, let's just save it.
Switch back to Unity.
And before I forget to do this, I'm going to select the robber and find my front door, which is down
here in the inspector and drag and drop that front door over there so it's ready to go.
Okay, so let's create our selector.
So create a new C-sharp script, call it selector, and then we'll open that up in the editor.
And of course, the selector is going to be a node.
Then we can get rid of these and you can write the rest.
And I'm not kidding.
This is your challenge.
You know how to create a sequence.
Let's go and have a look at the sequence.
Now the sequence is here.
And I said, it's like an end statement.
So all of them have to be true or successful.
All the children that is in order for the actual process itself to return successful.
So this is going to stop as soon as it hits a successful.
It's not going to go on to any of the other children.
The selector is going to do a similar thing as far as start at the first child, run the first child.
Now, if that child fails, it's not going to fail the selector.
It's just going to go to the next child and keep going through the children until it reaches the end.
At which point it's going to say, okay, I failed.
If it didn't find any successful children or as soon as it finds a successful child, it will return
at that point.
Okay.
So following or even maybe starting with what's in the sequence, you can rejig this to create the selector
code.
Pause the video now and when we come back, I will go through that for you.
Okay.
Welcome back.
How did you go?
Did you figure out how to get it to work?
Let's start at the beginning.
First of all, I'm going to create a public selector and the name so string in coming through here and
then we will create a name and then we'll say name equals MN.
Okay, let's get into this process.
So it's going to be a public override status.
Process.
Pretty much the same as the selector so far.
Let's get rid of that return of the base.
And we're going to say here that status, child status equals children, current child.
Now, this will be the first child in the list process and it's going to start running that.
Now we're going to say if child status equals status dot running, then we're in the middle of performing
a task.
So we will return status dot running.
Otherwise we will test.
If child status equals status dot successful, then we've pretty much finished the selection and we'll
go current child equals zero.
So we're setting it back to the starting point for ourselves if we use this again and then return status
dot.
Success.
Now, if it's not successful, then it's failed, obviously.
And we want to go to the next child.
So current child plus plus.
Then we'll say if the current child is greater than or equal to the children don't count, then we want
current child equals zero.
So resetting that once more and we're going to return fail.
So status failure.
Otherwise we're going to return status stop running.
Okay, so we're looking for the first child through the list of children that is successful.
Okay, so we will save this.
Now we have a selector so we can go back into our robber behavior and add that into our tree.
All right.
So we're going to need a selector node.
So under these leafs, I'm going to put selector and let's call that open door, which will equal a
new selector called Open Door.
Now the open door is going to include go to back door and go to front door.
So I'm going to put open door, dot, add child, go to back.
Door.
And open door.
Add child.
Go to front door.
So instead now with this steal of adding the child, go to backdoor.
You actually going to add in this open door like that?
And then when it gets to the open door node, it will then have a choice to make between these two.
Now it will go through them in order.
So it's going to go to the back door first and then go to the front door because we've turned on the
front door and the back door as obstacles in our mesh, the nav mesh will take us to one of these locations,
but it won't actually be able to continue to get to the diamond.
But that's okay at the moment.
Now, in order to go back to the van, we could actually get rid of going back to the door because it
will just create a path to get back there as easily as it can.
Which by this point means that one of those doors is going to be hopefully open.
Now, we haven't got to that dynamic just yet, but for now, let's just comment that out and we will
end up planning a path from the diamond straight back to the van.
Let's just save this, because I just want to see and show you about this.
Back and front doors working.
Okay.
So the agent should go to the back door first in this case.
So let's go back here.
We'll press play and he's going to go to the back door.
So you can see now that is completely skipping over, going there and he's going to head around to the
back door, won't be able to get through it because it is actually now an obstacle blocking the way
and stop playing.
If we go back into the code, we can actually now swap those around.
So let's just put the front door first, copy that and go up here, put that there and remove the other
one.
Save it and we'll go back and press play.
And this time he'll head off towards this door here, as you can see.
Just quickly, I want to show you the behavior you're probably getting that you might be questioning.
Let's just press play.
And what's happening here is he's going to the front door and then what happens is he goes around the
side here and apparently gets stuck on this wall.
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.
He actually did get to the front door because as far as that nav mesh is concerned, he made it to that
particular location.
Now, what does he do after successful going to the front door?
He tries to go to the diamond.
So this is him trying to go to the diamond.
But there is no way to get in here now because we've got these two nav mesh obstacles in the road.
But that's okay because we're going to work on this.
So if you've got this behavior, just know it's not him trying to get to the back door.
It was him trying to continue his behavior to get to the diamond.
And he actually got stuck.
Okay.
So that's what the Select is doing is just picking one of those things.
Now you can't see what happens if one of them is locked at the moment and he can't get through and he
actually has to then go to the next one in the selection child list.
So we will come back in the next lecture and work on that part.