Being able to calculate the line of sight between an eye and its target or whoever's chasing it is a
good skill to have programmed into it as a behavior.
It makes it seem a little bit more real if it thinks it's actually being watched.
And we've covered this before when we were doing our finite state machines and chasing UG around on
that map.
Now, I left it as a challenge for you in the last lecture to actually put this in to the code that
you've already got for the robber.
So, first of all, let's have a look and remind ourselves of the angles that are involved.
There is, of course, a vector towards the cop from the robber, and then there's the robbers forward
vector.
And it's this angle here that we want to check, whether it's small enough that we consider that he
can see it.
If it's a very large angle, then the robber is actually going to be looking in the other direction.
So if he's looking over here, the angle from there around to here is going to be much greater than
if he's looking over this way.
And the angle between here and there is obviously quite small.
So we can use that as a comparison to sort of say, well, he can see the cop if it's in a certain range.
So did you get that working when you put it in?
Let's go through the code and get that going now.
Okay.
So open up your bot script and we're going to put this in the can see target.
So it's pretty straightforward because we've got a fair bit of the mathematics already done.
The only thing we need to do is really calculate the angle.
So we want to calculate the angle between the forward vector of the agent and the direction to the target.
Now we've already got the vector to the target or the direction to tag, which is our ray to target
that we're using for our rake us.
So we can just use that in our angle calculation.
So float look angle.
Equals victim three angle and that will calculate the angle between this dot transformer forward and
ray to target.
Okay.
So then once we've got that, we can test that with some particular angle.
So if we put that down in our if statement, we'll put look angle and we want it to be less than, say,
60 degrees and we do a raker.
So if angle is less than 60 degrees, we're looking in the right direction and we can do a ray cast
towards the cop so that the cop because remember, we might be looking in the right direction of the
cop, but he might be on the other side of building.
So that means this ray is going to fail as well.
So, okay, we've got that, save that.
Now we're going to go and just check this out.
So with him currently facing in the right direction, if we press play, he's going to hopefully go
and hide.
Yep.
So he says, look, cops on me, I'm going to go and hide.
Okay?
So now let's just turn him around this way.
So he's not looking at the cop and we can press play and now he's not fazed at all.
We can come up here and he can't see the cop, but if the cop goes past him and becomes in his visual
range, then he's going to flee.
So now he's hiding behind the tree, but looking in the other direction.
But if we get in front, he will turn around and come back here.
Okay, great.
We can even add more to this dynamic in that we could program in whether the cop can see the robber.
So the robber, he might be looking at the cop or the cop could be looking in the other direction.
So it's like, Oh, he can't see me or he's not looking at me, so I don't have to do anything right.
So this is your next challenge.
What I'd like you to do in the class is come down here and we're going to create a bull can see me.
So this is can my enemy see me return?
True, if they can and false if they can't.
Again, use the look angle of 60.
So the mathematics is going to be pretty similar to what's going on in here.
So yeah, give that a go.
And when we come back, I will go through it with you.
Welcome back.
How did you go with that?
Well, as I said, the calculations are pretty much the same as what we've had before.
So let's grab this code.
So this is getting the direction now.
This is getting the direction from the robber, which is this transformed position to the cop.
Now, in this case, we'd want to work out what the other angle is.
So we're going to actually swap this around and get the vector that goes in the other direction like
that.
Okay.
So then we can work out the angle like we did before using that.
We can use this here.
Now, the order in finding the angle isn't going to matter in this case.
It's going to give you a same sort of calculation, except that this is still calculating the angle
between the robber and the rate of the target, which is incorrect because we actually want to work
that out based on the cop's forward direction.
So this would be the target's transform forward.
Okay.
Now, once we have that, we can then do a test.
So if looking or look angle is less than 60, then we will return true.
Otherwise will return false.
Okay, so we've done that.
Now we have to build it in to our update down here.
So we're going to make it a little bit more complex down here.
As far as hiding, we're going to say something like if can see.
Target and can see me, then we'll do a clever hide.
Otherwise, we're going to pursue the cops so the cops not looking.
We will do a pursue like that.
Okay, with that done, save it and let's go back and test it out.
So back in Unity, we'll press play and initially the robber is going to turn around, pursue cop and
go, oh no, oh no, can't do it.
So he's going between hiding and going with the cop so he can't pursue the cop and hide all the time.
If the cop moves over here is going to pursue it because he's not looking at him, but if he turns around
can get him to go away.
But he only goes away for a little bit of a second because as soon as he's turned around, he's forgot
that he's seen the cop and that's not going to work.
So let's add in a little bit more.
We're going to put in a little bit of a cooldown period, and I'm just going to put that up here.
I'm going to put a bull for cooldown equals false.
And you probably should put this up at the top of your program.
But because we're down here and you can see it, it's just there.
Then we want void behavior, cool down.
And what that is going to do is set cooldown to equal false.
Now cooldown will get set to true when we've started to run a clever hide so down here will go cooldown.
Equals true, which is going to set that off in a minute.
I'll show you where that is.
And then once we do that, we can go evoke behavior, cool down.
So that will then fire in, let's say, 5 seconds.
So give me 5 seconds to go on hide and then this will go back to false, which will allow this code
to run as soon as we put these next lines of logic in.
So in here we'll put if we're not cooling down because if we're cooling down, we're in the middle of
trying to go and do a clever hide and hide behind something otherwise.
Like that.
Okay.
Right.
So save that.
Let's go back into Unity and check it out.
So this time when he turns around and goes, look, there's a cop.
He'll go and hide immediately.
Look.
Yep.
5 seconds.
I'm going to go and hide behind this tree.
I might forget the cop is there and come back and have a look.
If he's there, he's still there.
I'm going to go and hide again.
Cop doesn't look this time.
And when the robber comes back, he goes, Oh, he's not watching.
I'm going to sneak up behind him.
Yeah, great.
So it's working quite well.
So there's so many different things you can do now that you have these basic seeking behaviors implemented
with these very simple little bots on the nav mesh.
Have a lot of fun with them.
I hope you enjoyed this section.
There's obviously different dynamics in game mechanics that you can implement with this.
And yeah, we're done with this section.