WEBVTT

00:07.060 --> 00:12.310
So far in this short series for hiding, we've worked with these trees.

00:12.310 --> 00:15.820
We're going to now implement a way to sneak up behind the robber.

00:15.820 --> 00:20.350
But before we do, we're going to just test out what we've got with some bigger objects.

00:20.350 --> 00:30.160
So go into the prefab, which is not that one, but I think standard assets, prototypes and there should

00:30.160 --> 00:33.520
be some prefabs in there, which is these ones.

00:33.520 --> 00:38.680
He might have seen them before because they come from some game testing asset that unity puts out.

00:38.860 --> 00:43.660
If you'd like to add in a house or something, then just grab that and drop it in there.

00:43.660 --> 00:45.850
Now, this might be a bit big.

00:45.850 --> 00:50.290
Let's just make it a little bit smaller and drop it into our scene.

00:50.290 --> 00:53.410
Now we need to make sure that this is a navigation static.

00:53.410 --> 00:59.560
So come over into the inspector with that selected and let's just check that it is.

00:59.560 --> 01:00.550
Yes, it is.

01:01.060 --> 01:04.930
So we can go back to our navigation and perform a bake.

01:04.930 --> 01:09.430
So that's going to block us out from going through that.

01:09.970 --> 01:18.040
Now, for the sake of making testing a little bit better, I'm going to move the rubber over here.

01:19.360 --> 01:24.970
Oh, and before we do, we have to make sure we set our house to a hide object.

01:24.970 --> 01:27.520
So make sure that you tag it as hide.

01:27.520 --> 01:29.620
Otherwise, it's just not going to be included.

01:29.650 --> 01:34.570
Okay, so let's press play and let's move the cop.

01:35.650 --> 01:38.950
And so now look through the building.

01:38.950 --> 01:45.580
If we just pause it and have a look where it is, you can see there's definitely no line of sight through

01:45.580 --> 01:53.170
this because the collider that's sitting on this house is snug to it, whereas around the trees it's

01:53.170 --> 01:53.410
not.

01:53.410 --> 01:59.740
So you would have to build probably a more complex shader if you wanted to sort of block off the trunks

01:59.740 --> 02:02.980
different to the actual leaves.

02:04.310 --> 02:11.090
So now with that all working as we expected, we can go back into our code and add in that little bit

02:11.090 --> 02:12.290
of extra.

02:12.290 --> 02:20.150
And we're going to say that the agent needs to hide if he can see the target.

02:20.270 --> 02:27.320
So the first thing that we're going to add is a little method called can see target.

02:27.440 --> 02:31.000
And up here above our update, we'll add that in.

02:31.010 --> 02:36.410
So it's going to be bool can see target.

02:37.580 --> 02:41.900
Then inside of here, we're going to do a ray cast.

02:41.900 --> 02:43.880
So a ray cast.

02:44.940 --> 02:47.640
Hit recast.

02:49.230 --> 02:50.160
Info.

02:51.920 --> 02:57.230
Vector three ray to target.

02:57.380 --> 03:05.740
So we're going to create a ray that's going from the position of our agent to the position of our target.

03:05.750 --> 03:15.830
So target dot transformed position minus this dot transform dot position.

03:17.480 --> 03:22.640
And then we will say if physics.

03:24.240 --> 03:25.850
Dot ray cast.

03:26.660 --> 03:31.280
This dot transformed position.

03:32.850 --> 03:36.150
Right to target.

03:38.030 --> 03:41.780
And the out for that will be our cast info.

03:44.700 --> 03:46.650
If the object that we hit.

03:46.650 --> 03:55.020
So if recast info dot transform game object.

03:56.700 --> 04:02.970
Dot tag equals whatever tag we've got on our policeman or our cop, which is cop.

04:02.970 --> 04:07.980
But just check that your project did pick that up and we will return.

04:07.980 --> 04:08.370
True.

04:08.370 --> 04:14.430
So this is going to perform a radius which if we're hiding behind something and can't be seen by the

04:14.430 --> 04:20.640
cop, then we're not going to hit the cop with this Ray.

04:20.640 --> 04:24.180
It's actually going to hit the collider of the object that we're hiding behind.

04:24.540 --> 04:26.940
Okay, I've got a little spelling mistake up here.

04:26.940 --> 04:27.750
Let's fix that.

04:27.750 --> 04:27.940
Right.

04:27.960 --> 04:30.510
Cast info and we're returning.

04:30.540 --> 04:35.880
True, or else we're going to return false.

04:35.970 --> 04:38.040
So that's how can see target.

04:38.070 --> 04:43.800
Now in the update, what we can do is go if can see target.

04:45.560 --> 04:53.150
Then we will perform our target hiding from our target ability.

04:56.300 --> 04:56.900
Okay.

04:56.900 --> 05:04.490
So back in unity, what we're going to do is make sure the cop is tagged with the cop tag in lowercase.

05:04.490 --> 05:09.490
If you don't have it, add it as a tag and attach it to the cop.

05:09.500 --> 05:17.270
Now, what's going to happen here is when I press play, the robber, if he can see me, is going to

05:17.270 --> 05:18.290
move locations.

05:18.290 --> 05:21.080
Okay, so he's moved over there now.

05:21.080 --> 05:25.230
He's going to stay in place wherever he went.

05:25.250 --> 05:25.700
Where do you go?

05:25.700 --> 05:26.150
Over there.

05:26.190 --> 05:26.480
Okay.

05:26.480 --> 05:30.530
So we're going to now come back around this side.

05:31.550 --> 05:37.790
And as soon as he can draw a ray to the cop, he's going to go and hide.

05:41.680 --> 05:42.180
Okay.

05:42.190 --> 05:47.290
Now, what you can't currently do is to sneak up behind the robber.

05:47.320 --> 05:53.920
Now, do you remember when we did our finite state machine, we actually built in the ability to sneak

05:53.920 --> 05:57.330
up behind UG and he was on a navy mesh as well.

05:57.340 --> 06:04.300
So the very same code can be used to determine like an angle of vision.

06:04.300 --> 06:10.330
So if the cop is actually in front of the robber, then you will have him move.

06:10.330 --> 06:12.480
Otherwise you'll be able to sneak up behind him.

06:12.490 --> 06:15.520
So I'm going to leave this as a challenge for you.

06:15.520 --> 06:21.700
And when we come back in the next video, we will actually implement that code and get it working.
