At this point, you've got a whole set of behaviors programmed into your bot, and what's really cool
about this is you can start to combine them to get all different sorts of complex behaviors and effects
from your agents.
So what we're going to do in this video is we're going to create a little scenario where the robot is
going to sneak up on the cop.
But when the cop is looking towards the robot, the robot will go and hide.
So it's kind of like, you know, sneaking up on someone and making sure you can hide behind things
if they turn around to try and look at you.
So the first thing we need to do is figure out how does the cop know the robbers there or how does the
robber know the cop is looking at them?
OK, so let's find our cop object, which is down here now.
He's got a forward vector.
OK, so there's vectors involved in this again.
And I think you're getting appreciation of just how much victims do in game development as far as moving
characters around and working out where things are.
They are just brilliant.
And you really need to have a thorough understanding of vectors because it's a powerful thing to have
in your toolkit now.
OK, we're facing forward.
This is the forward facing vector of the cop, so let's just rotate him around.
So if he was facing like in this direction and it's just the key, you can see that the forward facing
vector is that way.
So if he's facing that way, then you could be pretty happy in saying that he can see the robber even
though he's not directly facing him.
So we have these two vectors.
We have the forward vector that the facing direction for your cop.
And then there's also going to be the vector to the robber here.
And so we're going to actually test what the angle is between the forward facing of the cop and the
direction to the actual robber.
Now it's within a certain range, say, like 60 degrees.
We can say that the cop is looking in the direction of the robber.
Therefore, the robber at that point will go and hide.
If not so, the cop's face the other way and is wandering away, then the robber is going to pursue.
So let's go into our bot code and start adding this in.
The first thing we will need is another method that's telling the agent if the target can see them.
So we'll create another little method down here that returns.
True or false.
So Bull and let's say Target can see me.
And for this, we need to work out, Victor, three to agent.
Equals this dot transform the position minus the target's position, target, dot transform.
Position.
Then we can work out the angle, so float looking angle equals vector three, the angle between the
target dot transformed forward and the two agent and then will say if looking angle is less than 60,
we will return.
True.
Yes, we can see who else we're going to return.
False.
All right.
So that's the target.
Can see me.
What we do now down in our update is to use that in our IF statement here so can see Target and Target
can see me, then we will go and hide or else we'll do a pursue.
All right, so save that, let's go back into Unity and Pressplay and have a look how this works.
So we're currently looking at the cop, so he's going to go and hide now if I turn around and start
to wander away.
He's going to chase me.
Just find a safe and come back over this way.
I'll go over here, and when he gets here, I'll turn around and go, Oh, I can see you, and he goes
and hides behind the tree.
And let me now move over here.
And I'm just trying to get him to go and hide behind.
They're OK now, what's going to happen if you keep looking at him, he's going to keep popping out
around the corner because he's hiding and obviously you can't see him well, you're facing in that direction,
but you can't do that.
Can see me think that's going to cause the hide to happen.
Therefore, he's just going to keep popping out, checking.
Is it okay to come out here?
Is it okay to come out yet?
Now, once I turn around, then he's going to come and pursue, which is fine.
OK, now you might actually want him to complete the hiding.
So if we're like out here.
And I turn around and see him, and then I turn back again, he will come and chase me.
Because there's no sort of delay or cool down in your action and you might want that, you might want
him to actually do that.
So let's now put that into the code where we're actually going to finish or not finish, but cool down
a particular behavior, which means that if he has seen you, then you're just going to run and hide
regardless of whether he turns back immediately or not.
So it will put something in like, you know, for five seconds, just continue to go and hide or, you
know, just continue to pursue without changing back and forth, because these will flip back and forth
between these behaviors based on the true or false.
And if they do, it actually doesn't give you any time to sort of complete any particular behavior.
And you might want that to happen.
OK, so what we're going to do is we're going to, first of all, add in a new little bool value, which
is going to be called cool down, and we'll set that initially to false.
And then down in our update, what we're going to do here is put if we're not cooling down, then we
can choose a behavior.
So that's just these.
Now, when does cool down become true?
We'll cool down is going to become true when we are doing this hide.
So that will actually spend some time hiding before coming out of hiding.
So in here with this clever hide, after you've executed it once, we're going to say cool down.
Equals true like that.
Now, that's going to stop this coming back in here again, but at some point we've got to set cool
down back to false so that we can come in and choose a new behavior.
So I'm going to have and invoked method and so will invoke under here behavior.
Cool down.
And we will do that in five seconds, and that means we actually need to right this method, so just
up here will have a void behavior cool down.
And all that's going to do is go cool down.
Equals false and set it back to false after these five seconds for us now, of course, you could have
behavior cooldowns for the different behavior.
This is just a generic one for everything.
And you could have other behaviors that you're doing in here with different it invokes of the cool down
to complete them.
But we're going to do five seconds here, which means that once he does go and trigger hiding, if the
cop turns around or not, he's still going to complete that hiding and he will hide for five seconds
before he even comes out to check again.
All right.
So let's press play and we will be facing him initially.
Okay, so he goes and hides.
Now, after five seconds, he's going to come back and check if the coast is clear.
Now, see how that's just a little bit better than him bouncing back and forth.
So if I turn around when he comes out, he's going to think it's OK.
No, no.
You can see me.
I'm going to go and hide again.
And I'll be looking when it comes around the corner.
There he goes, and he's going to go and hide and he'll come back again.
OK, so that is a really simple way of adding some really sort of complex behaviors into our system
just by putting in a few statements of the conditions when you're going to run them and executing the
behaviors we've already written and just combining them to get different effects.