Thus far, we've added in many different behaviors for our little agent and we've also got the functionality
to shoot at it and for its health to go down and its health to come up but we're not using that for
anything.
So let's go and add some more behavior methods into our AI script and then build behavior trees
for them.
So let's open up AI to begin with
and we're going to add some more tasks.
I'll just put them at the bottom
for consistency, it doesn't really matter what order these are in.
So we want to make the bot clever enough that it knows to flee when it's in trouble and it will be
in trouble (obviously) when it's health gets low, but it will also remain in trouble if the player is chasing
it or the player is close enough to it.
So we want to be able to test for those conditions, as well as set it up to flee.
Under here, let's add these methods in.
First of all, I've got a method called 'IsHealthLessThan' and it will accept a 'health' float value.
This is not the health that's stored in the agent class.
This is health that we will pass through the from our Behavior Tree and we're going to test it against
the health of the agent.
It's going to return 'true' if the agent's health is less than the value being passed through.
So therefore we've got this variable that we can put into the Behavior Tree to change the behavior
of the agent.
Ok.
So you might have it that it doesn't want to flee away from danger unless it's health is at 20 or you might
want it so it flees away from danger when its health is at 80, depending on how sensitive it is and then
you can have different ones for different types of agents.
Now besides checking for the health, we'll also test whether the bot is in danger or not
and the 'InDanger' here is doing a similar thing, where it's taking a value that's coming from the
Behavior Tree
via this 'minDist' float and we'll test it against the distance between the player and the agent
and if that distance is less then the minDist, then it IS in danger, it's still within range of the player.
Finally, we've got another method called 'TakeCover' and what TakeCover is going to do is to work out
'moving away from the player'
and this is the same logic (the same vector calculation) we used in the crowd-fleeing, where we get a vector
not TOWARDS the player but AWAY from the player
and then we add that to the current agent's position and notice I'm multiplying it by 2.
So it'll go twice as far away from the player than it currently is.
Once we have a new destination, we can set the agent's destination here
and as you might suspect, we will then use the MoveToDestination method that we've written a while
ago and call that to actually get the agent to move.
So that's your AI, save that.
Now we'll come back into Unity and we will create a new script. So I'll just duplicate the Wander script and
let's call that Flee.BT. Open that up and I'll add it to my collection of scripts that I'm getting.
Now in here, we'll test for health being low and
we'll test if we are within a certain danger radius and then we will also put in our 'sequence' which
we need to TakeCover and then MoveToDestination.
So it's going to look something like this.
'while IsHealthLessThan(40.0)', in this case
and also 'while InDanger(50.0)', so we're still within 50 distance of the player,
then the 'sequence' will run for 'TakeCover' and 'MoveToDestination'.
Save that and let's go and see how it makes our agent behave.
So I'm going to select the agent and give it the new script of Flee...drag-and-drop that over into your
Panda Behavior, press Play and then you'll have to go over to the agent and start firing on it to trigger
the script to work because you need to be close to it AND it's health needs to be less than 40. So while
you're doing it, if you keep an eye on its script, you'll see when it triggers.
Now as long as the script stays below 40 for health, you can keep triggering it by staying near it and
following it around and you'll find that the behavior itself is getting it to move away from you.
Once it does reach over 40 again as it's going away, the whole thing will stop because you're negating
this while-statement here and therefore it can't continue.
You would therefore have to shoot at it again to make its health go below 40, to trigger this tree to
happen again.
Now that's all this agent is doing, it's not going to turn around and attack you back because you've
not got that in this particular script.
Another thing we can use our IsHealthLessThan method for is to test whether our bot has died
and for that we need another behavior script.
So again, I'll just copy the Wander and let's call that
Dead.BT
and instead of a 'sequence', we'll have a 'while IsHealthLessThan(0.1)', which is a very, very
minimal health.
In that case, our bot will be dead.
So what do we do with it when it's dead? Let's explode it.
'Explode' is not a method that comes with the behavior tree and it's something that you have to write yourself.
So we'll save this and then we'll go back to our AI script and add in an 'Explode' method.
We'll do that down the bottom.
So...'Explode'...all it's going to do is to destroy the healthBar game object and then destroy the game object
of the agent itself (and then it will just be gone from the game). If you wanted an explosion effect particle
system, you would add it into here.
Again, this is a bool-returning method with a return of 'true' and there's no reason why this couldn't
be used as a 'void' with a Task.current.Succeed, unless somewhere in your tree you wanted to test
if the explosion had occurred and then deal with it that way.
Ok, so save that, go back into Unity, get your 'Dead' script and you want to attach that as the Behavior Tree
for your Droid. Let's put it there and then we'll press Play and we'll come over and shoot until its health goes down
to 0 to test it. So...shoot and then it's gone.
So that was the Explode.
All of the behavior trees that we've created are all well and good by themselves but when they really
come into play is when we put them all together and make a much more complex behavior tree to represent
the entire behavior of our agent and we'll do that in the next lecture.