WEBVTT

00:06.650 --> 00:12.950
Being able to calculate the line of sight between an eye and its target or whoever's chasing it is a

00:12.950 --> 00:17.130
good skill to have programmed into it as a behavior.

00:17.150 --> 00:21.340
It makes it seem a little bit more real if it thinks it's actually being watched.

00:21.350 --> 00:27.830
And we've covered this before when we were doing our finite state machines and chasing UG around on

00:27.860 --> 00:28.940
that map.

00:29.060 --> 00:34.790
Now, I left it as a challenge for you in the last lecture to actually put this in to the code that

00:34.790 --> 00:37.020
you've already got for the robber.

00:37.040 --> 00:42.890
So, first of all, let's have a look and remind ourselves of the angles that are involved.

00:42.920 --> 00:49.910
There is, of course, a vector towards the cop from the robber, and then there's the robbers forward

00:49.910 --> 00:50.630
vector.

00:50.630 --> 00:56.810
And it's this angle here that we want to check, whether it's small enough that we consider that he

00:56.810 --> 00:57.790
can see it.

00:57.800 --> 01:04.250
If it's a very large angle, then the robber is actually going to be looking in the other direction.

01:04.250 --> 01:12.410
So if he's looking over here, the angle from there around to here is going to be much greater than

01:12.410 --> 01:15.680
if he's looking over this way.

01:15.680 --> 01:20.600
And the angle between here and there is obviously quite small.

01:20.600 --> 01:27.410
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.

01:27.590 --> 01:29.960
So did you get that working when you put it in?

01:29.960 --> 01:33.050
Let's go through the code and get that going now.

01:34.100 --> 01:34.430
Okay.

01:34.430 --> 01:38.480
So open up your bot script and we're going to put this in the can see target.

01:38.480 --> 01:43.430
So it's pretty straightforward because we've got a fair bit of the mathematics already done.

01:43.460 --> 01:46.040
The only thing we need to do is really calculate the angle.

01:46.040 --> 01:54.560
So we want to calculate the angle between the forward vector of the agent and the direction to the target.

01:54.590 --> 01:59.180
Now we've already got the vector to the target or the direction to tag, which is our ray to target

01:59.180 --> 02:00.840
that we're using for our rake us.

02:00.860 --> 02:04.160
So we can just use that in our angle calculation.

02:04.160 --> 02:06.740
So float look angle.

02:08.960 --> 02:19.130
Equals victim three angle and that will calculate the angle between this dot transformer forward and

02:19.130 --> 02:21.710
ray to target.

02:23.290 --> 02:23.800
Okay.

02:23.800 --> 02:28.360
So then once we've got that, we can test that with some particular angle.

02:28.360 --> 02:36.190
So if we put that down in our if statement, we'll put look angle and we want it to be less than, say,

02:36.190 --> 02:40.270
60 degrees and we do a raker.

02:40.270 --> 02:45.580
So if angle is less than 60 degrees, we're looking in the right direction and we can do a ray cast

02:45.580 --> 02:50.920
towards the cop so that the cop because remember, we might be looking in the right direction of the

02:50.920 --> 02:52.990
cop, but he might be on the other side of building.

02:52.990 --> 02:56.080
So that means this ray is going to fail as well.

02:56.080 --> 02:58.810
So, okay, we've got that, save that.

02:59.260 --> 03:02.440
Now we're going to go and just check this out.

03:02.440 --> 03:10.360
So with him currently facing in the right direction, if we press play, he's going to hopefully go

03:10.360 --> 03:11.290
and hide.

03:11.860 --> 03:12.070
Yep.

03:12.130 --> 03:14.350
So he says, look, cops on me, I'm going to go and hide.

03:14.380 --> 03:14.680
Okay?

03:14.680 --> 03:18.790
So now let's just turn him around this way.

03:18.790 --> 03:26.440
So he's not looking at the cop and we can press play and now he's not fazed at all.

03:26.440 --> 03:33.250
We can come up here and he can't see the cop, but if the cop goes past him and becomes in his visual

03:33.250 --> 03:36.160
range, then he's going to flee.

03:36.160 --> 03:38.680
So now he's hiding behind the tree, but looking in the other direction.

03:38.680 --> 03:42.400
But if we get in front, he will turn around and come back here.

03:43.270 --> 03:44.230
Okay, great.

03:44.230 --> 03:54.550
We can even add more to this dynamic in that we could program in whether the cop can see the robber.

03:54.550 --> 03:58.480
So the robber, he might be looking at the cop or the cop could be looking in the other direction.

03:58.480 --> 04:04.090
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.

04:04.090 --> 04:07.060
So this is your next challenge.

04:07.070 --> 04:18.970
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.

04:20.470 --> 04:24.760
So this is can my enemy see me return?

04:24.760 --> 04:28.030
True, if they can and false if they can't.

04:28.030 --> 04:30.370
Again, use the look angle of 60.

04:30.370 --> 04:35.230
So the mathematics is going to be pretty similar to what's going on in here.

04:36.130 --> 04:38.320
So yeah, give that a go.

04:38.320 --> 04:41.980
And when we come back, I will go through it with you.

04:43.330 --> 04:44.170
Welcome back.

04:44.170 --> 04:45.850
How did you go with that?

04:45.850 --> 04:51.190
Well, as I said, the calculations are pretty much the same as what we've had before.

04:51.220 --> 04:53.500
So let's grab this code.

04:53.500 --> 04:56.110
So this is getting the direction now.

04:56.110 --> 05:03.340
This is getting the direction from the robber, which is this transformed position to the cop.

05:03.340 --> 05:06.970
Now, in this case, we'd want to work out what the other angle is.

05:06.970 --> 05:13.750
So we're going to actually swap this around and get the vector that goes in the other direction like

05:13.750 --> 05:14.230
that.

05:14.740 --> 05:15.280
Okay.

05:15.280 --> 05:20.710
So then we can work out the angle like we did before using that.

05:20.710 --> 05:22.300
We can use this here.

05:23.110 --> 05:26.620
Now, the order in finding the angle isn't going to matter in this case.

05:26.620 --> 05:32.740
It's going to give you a same sort of calculation, except that this is still calculating the angle

05:32.740 --> 05:39.040
between the robber and the rate of the target, which is incorrect because we actually want to work

05:39.040 --> 05:41.830
that out based on the cop's forward direction.

05:41.830 --> 05:46.660
So this would be the target's transform forward.

05:46.780 --> 05:47.080
Okay.

05:47.080 --> 05:49.570
Now, once we have that, we can then do a test.

05:49.570 --> 05:59.740
So if looking or look angle is less than 60, then we will return true.

06:00.940 --> 06:03.730
Otherwise will return false.

06:04.360 --> 06:05.860
Okay, so we've done that.

06:05.860 --> 06:09.730
Now we have to build it in to our update down here.

06:09.730 --> 06:12.160
So we're going to make it a little bit more complex down here.

06:12.160 --> 06:17.560
As far as hiding, we're going to say something like if can see.

06:22.040 --> 06:30.020
Target and can see me, then we'll do a clever hide.

06:30.770 --> 06:33.950
Otherwise, we're going to pursue the cops so the cops not looking.

06:34.040 --> 06:36.610
We will do a pursue like that.

06:36.620 --> 06:40.100
Okay, with that done, save it and let's go back and test it out.

06:40.100 --> 06:50.150
So back in Unity, we'll press play and initially the robber is going to turn around, pursue cop and

06:50.150 --> 06:52.790
go, oh no, oh no, can't do it.

06:52.790 --> 07:01.430
So he's going between hiding and going with the cop so he can't pursue the cop and hide all the time.

07:01.460 --> 07:07.010
If the cop moves over here is going to pursue it because he's not looking at him, but if he turns around

07:07.970 --> 07:09.380
can get him to go away.

07:09.380 --> 07:13.730
But he only goes away for a little bit of a second because as soon as he's turned around, he's forgot

07:13.730 --> 07:16.010
that he's seen the cop and that's not going to work.

07:16.010 --> 07:18.290
So let's add in a little bit more.

07:18.290 --> 07:22.880
We're going to put in a little bit of a cooldown period, and I'm just going to put that up here.

07:22.880 --> 07:27.350
I'm going to put a bull for cooldown equals false.

07:27.350 --> 07:30.410
And you probably should put this up at the top of your program.

07:30.410 --> 07:33.020
But because we're down here and you can see it, it's just there.

07:33.470 --> 07:38.150
Then we want void behavior, cool down.

07:39.080 --> 07:45.650
And what that is going to do is set cooldown to equal false.

07:45.890 --> 07:53.660
Now cooldown will get set to true when we've started to run a clever hide so down here will go cooldown.

07:54.900 --> 07:59.730
Equals true, which is going to set that off in a minute.

07:59.730 --> 08:00.570
I'll show you where that is.

08:00.570 --> 08:08.190
And then once we do that, we can go evoke behavior, cool down.

08:08.190 --> 08:11.880
So that will then fire in, let's say, 5 seconds.

08:11.880 --> 08:18.480
So give me 5 seconds to go on hide and then this will go back to false, which will allow this code

08:18.480 --> 08:21.520
to run as soon as we put these next lines of logic in.

08:21.540 --> 08:27.300
So in here we'll put if we're not cooling down because if we're cooling down, we're in the middle of

08:27.300 --> 08:32.700
trying to go and do a clever hide and hide behind something otherwise.

08:34.680 --> 08:35.790
Like that.

08:36.000 --> 08:36.840
Okay.

08:36.960 --> 08:37.350
Right.

08:37.350 --> 08:38.370
So save that.

08:38.790 --> 08:42.690
Let's go back into Unity and check it out.

08:43.380 --> 08:46.540
So this time when he turns around and goes, look, there's a cop.

08:46.560 --> 08:49.310
He'll go and hide immediately.

08:49.320 --> 08:49.630
Look.

08:49.650 --> 08:50.090
Yep.

08:50.100 --> 08:50.910
5 seconds.

08:50.910 --> 08:52.680
I'm going to go and hide behind this tree.

08:52.920 --> 08:55.770
I might forget the cop is there and come back and have a look.

08:55.770 --> 08:57.090
If he's there, he's still there.

08:57.090 --> 08:58.510
I'm going to go and hide again.

08:58.590 --> 09:00.670
Cop doesn't look this time.

09:00.690 --> 09:03.420
And when the robber comes back, he goes, Oh, he's not watching.

09:03.420 --> 09:04.970
I'm going to sneak up behind him.

09:04.980 --> 09:05.960
Yeah, great.

09:05.960 --> 09:08.100
So it's working quite well.

09:08.340 --> 09:15.630
So there's so many different things you can do now that you have these basic seeking behaviors implemented

09:15.630 --> 09:18.860
with these very simple little bots on the nav mesh.

09:18.870 --> 09:19.980
Have a lot of fun with them.

09:19.980 --> 09:21.330
I hope you enjoyed this section.

09:21.330 --> 09:26.550
There's obviously different dynamics in game mechanics that you can implement with this.

09:26.550 --> 09:29.400
And yeah, we're done with this section.
