So far in this short series for hiding, we've worked with these trees.
We're going to now implement a way to sneak up behind the robber.
But before we do, we're going to just test out what we've got with some bigger objects.
So go into the prefab, which is not that one, but I think standard assets, prototypes and there should
be some prefabs in there, which is these ones.
He might have seen them before because they come from some game testing asset that unity puts out.
If you'd like to add in a house or something, then just grab that and drop it in there.
Now, this might be a bit big.
Let's just make it a little bit smaller and drop it into our scene.
Now we need to make sure that this is a navigation static.
So come over into the inspector with that selected and let's just check that it is.
Yes, it is.
So we can go back to our navigation and perform a bake.
So that's going to block us out from going through that.
Now, for the sake of making testing a little bit better, I'm going to move the rubber over here.
Oh, and before we do, we have to make sure we set our house to a hide object.
So make sure that you tag it as hide.
Otherwise, it's just not going to be included.
Okay, so let's press play and let's move the cop.
And so now look through the building.
If we just pause it and have a look where it is, you can see there's definitely no line of sight through
this because the collider that's sitting on this house is snug to it, whereas around the trees it's
not.
So you would have to build probably a more complex shader if you wanted to sort of block off the trunks
different to the actual leaves.
So now with that all working as we expected, we can go back into our code and add in that little bit
of extra.
And we're going to say that the agent needs to hide if he can see the target.
So the first thing that we're going to add is a little method called can see target.
And up here above our update, we'll add that in.
So it's going to be bool can see target.
Then inside of here, we're going to do a ray cast.
So a ray cast.
Hit recast.
Info.
Vector three ray to target.
So we're going to create a ray that's going from the position of our agent to the position of our target.
So target dot transformed position minus this dot transform dot position.
And then we will say if physics.
Dot ray cast.
This dot transformed position.
Right to target.
And the out for that will be our cast info.
If the object that we hit.
So if recast info dot transform game object.
Dot tag equals whatever tag we've got on our policeman or our cop, which is cop.
But just check that your project did pick that up and we will return.
True.
So this is going to perform a radius which if we're hiding behind something and can't be seen by the
cop, then we're not going to hit the cop with this Ray.
It's actually going to hit the collider of the object that we're hiding behind.
Okay, I've got a little spelling mistake up here.
Let's fix that.
Right.
Cast info and we're returning.
True, or else we're going to return false.
So that's how can see target.
Now in the update, what we can do is go if can see target.
Then we will perform our target hiding from our target ability.
Okay.
So back in unity, what we're going to do is make sure the cop is tagged with the cop tag in lowercase.
If you don't have it, add it as a tag and attach it to the cop.
Now, what's going to happen here is when I press play, the robber, if he can see me, is going to
move locations.
Okay, so he's moved over there now.
He's going to stay in place wherever he went.
Where do you go?
Over there.
Okay.
So we're going to now come back around this side.
And as soon as he can draw a ray to the cop, he's going to go and hide.
Okay.
Now, what you can't currently do is to sneak up behind the robber.
Now, do you remember when we did our finite state machine, we actually built in the ability to sneak
up behind UG and he was on a navy mesh as well.
So the very same code can be used to determine like an angle of vision.
So if the cop is actually in front of the robber, then you will have him move.
Otherwise you'll be able to sneak up behind him.
So I'm going to leave this as a challenge for you.
And when we come back in the next video, we will actually implement that code and get it working.