So I always looked at some pretty simple sequential behavior trees.
Now we're going to add in a condition that tests if something is true or false before the behavior tree
continues to run.
And so we're going to add in a decision node within the code.
We can do that inside of our attack.
But first of all we need to Rostow method that's going to do the decision for us.
Currently we're attacking all the time when you only really want the book to attack if it can truly
say the playa.
And in this case when you start off a cup because the player is behind a wall.
Also if the player is out sondes some visible range you don't want it to attack either.
Now if you're a member very top of this program there is a visible range and a shot range set up so
this visible range which is 80 is going to determine if the play is within 80 units of the bot than
it can see it will also test at the same time if there's no wall between the player and the ball which
is not in a situation because you want the ball to kind of be aware that there is infrastructure around
it.
So as the last method in here underneath the file add in a new one which we'll use in our behavior tree
code.
So it's called C player.
It's a boolean.
So it's like we had before but this time we're actually going to use it as a logic test in the behavior
tree.
So what this is doing is working out the vector from the playa to the Bosch or I should say the other
way around from the bot to the player.
And then we're also going to have a recast hit.
So we'll cast a long distance vector to see if we hit something and if we do hit something which is
in this case a wall then we obviously can't say the pledge because as a woman the riots have got a little
bull said.
He's saying Say wall equals false to start with.
I'm also doing a debugged draw right in the environment so we can see where that distance vector is
being projected.
This is really Noss if you're trying to de-bug visually what's going on and where your vict desire.
I always use this now.
It's going to project array do the physics right cost as we've done before and then see if we have hit
something called a wall.
If we have then C will will equal true.
Now just for a moment back in unity if you go to the math and drop that down there's always wars.
And each one has been tagged with war 1 that's how that physics right cost is going to work.
Now make sure that your right that you're projecting does actually go through the wall or whatever object
you're trying to detect as an obstacle.
And that's why this de-bug right is such a great idea.
After doing that I'm also printing this see will value in some debug code for the behavior tree itself
so that you can see it when you're working through the different nodes in that and what they're doing
which is also quite useful.
And then finally to determine whether we can see the player or not.
The test is if the magnitude of that direction vector distance between the two places is less than the
visible range and there's no wall in the way this time we're not concerned whether the ball is facing
in the right direction or not.
So let's just imagine it has a little write up system on board and it doesn't need to be looking in
a direction to be able to tell if the players knew by now if the player is in that radius and there's
no wul in the way then we're going to return true that we do see the player.
Otherwise we will return a false.
So this is a method for new node.
OK so we'll save that and then we'll go into the It packs script.
And what we're going to do is add a new node into our tree above the sequence above here.
I'm now going to add these so the code is while see playa.
Now this while node is a special testing node and it will allow you to run something that is a true
or a false value.
And even invert it to check it doesn't have to be true to run.
It can also be false and it won't stop your tree from running.
OK so you could even put for example while not see plier if you wanted to switch the logic around and
we will a little light out but not in this case here.
Now this while node can only have one child.
So we need to make sure that we've indented all of these like that otherwise the behavior tray system
will complain about it.
So now the sequence of shooting is only going to happen if the C ply a method gives us a true up here.
So let's say that and let's test it out.
So back in unity click on the Droid to check that the script is OK and you'll get pink text where everything's
fine with the attack script.
OK let's press play and have a look at what we get.
So over in the back of tree while C player is sitting there will a Cool's tree you can see that they
can also see in the saying that de-bug were I going through and hitting the wall which is causing the
wall equals true here and it's making us so that we can't see the player.
Let's move the player.
Around interviews so if it come around the corner and the go soon as I come into view and I'm within
the visual range then the joy starts firing.
And if I turn around and go back again it will stop as soon as there is a wall in the road.
And if I go through this wall because I'm not constrained as the playa by the neph mesh it no longer
can see me if I come back in each will and because of the previous code it will also turn towards the
player in order to shoot.
And you can see the behavior Churi ticking along quite nicely with that little bit of logic so you're
starting to see the power behind you trees that you can create a whole bunch of methods that do like
a single action for your player and then you can string them together in the behavior tree which makes
it a lot easier for someone who's not really familiar with the code.
But it also makes your eye quite powerful where you can mix and match different behaviors just for interest
right.
Let's go back into attack and change that code up at the top with the wall statement to be a notch and
reverse that logic so that when we don't see the player we're going to attack the player which doesn't
make logical sense but what that means is that while your not invisible view of the bush with your player
character it's going to be shooting in your direction.
So just save that one here.
I should also mention this while Stutman because of the used to normal programming you're going to think
oh while statement wall statement Mayne's looping in the case of behavior trees it doesn't mean looping.
So this is not a loop.
It's if anything it's an if statement.
If you want to think of it as something that's not going to cause this to loop around inside of here
and it's just cold while right so save that let's go back and have a look at how that affects our behavior.
So let's press play and we would expect we're going to get shot at straight away because we can't see
the player.
Now if I move into range then we won't get shot at.
And in fact that whole sequence stops running so we're not even going to turn and look at the player
because the speed here is not being satisfied as far as being able to see the player.
Right.
So that's how you can add some logic in as well.
Now you don't have to use the while not in this case because the same player could be part of the sequence
if you put say player in the sequence let's just go back in to heah and take that out of their and put
it down here.
Then it's quite legitimately allowed to fail.
And because this is a sequence the rest of it won't run anyway so let's save it and have a little look
at it.
Check that it's reloaded in here.
We now have a sequence if we press play k we can't see the player.
So the sequence has stopped with the C player and let's try and move closer to it to see what happens
it will fire the same player.
And then the rest will run as it did before.
So in this case the while statement is not helping us with the same player because it has to be true
in order for the rest of the code to run.
So in this case what we've done is turned the tree on its head as far as this C player is now a leaf
node where as before it was a decorator node because it had children underneath it and it was allowing
you to make a decision and the decision in that case comes into its own when you want to invert the
value that's coming out of it.
So the while not is going to be a lot in common in this particular case but it depends on what you're
trying to do.
So that's the sequence.
And also adding in that logic in the next video we will look at the Selecta nerd's.