WEBVTT

00:13.690 --> 00:20.650
At this point, you've got a whole set of behaviors programmed into your bot, and what's really cool

00:20.650 --> 00:27.250
about this is you can start to combine them to get all different sorts of complex behaviors and effects

00:27.250 --> 00:28.750
from your agents.

00:29.170 --> 00:35.530
So what we're going to do in this video is we're going to create a little scenario where the robot is

00:35.530 --> 00:37.720
going to sneak up on the cop.

00:37.720 --> 00:41.680
But when the cop is looking towards the robot, the robot will go and hide.

00:41.690 --> 00:46.210
So it's kind of like, you know, sneaking up on someone and making sure you can hide behind things

00:46.210 --> 00:48.580
if they turn around to try and look at you.

00:49.210 --> 00:57.280
So the first thing we need to do is figure out how does the cop know the robbers there or how does the

00:57.280 --> 00:59.740
robber know the cop is looking at them?

01:00.160 --> 01:05.320
OK, so let's find our cop object, which is down here now.

01:05.560 --> 01:06.880
He's got a forward vector.

01:06.910 --> 01:10.270
OK, so there's vectors involved in this again.

01:10.270 --> 01:16.660
And I think you're getting appreciation of just how much victims do in game development as far as moving

01:16.660 --> 01:18.720
characters around and working out where things are.

01:18.910 --> 01:20.290
They are just brilliant.

01:20.290 --> 01:25.540
And you really need to have a thorough understanding of vectors because it's a powerful thing to have

01:25.540 --> 01:27.230
in your toolkit now.

01:27.900 --> 01:29.110
OK, we're facing forward.

01:29.110 --> 01:36.040
This is the forward facing vector of the cop, so let's just rotate him around.

01:36.040 --> 01:44.350
So if he was facing like in this direction and it's just the key, you can see that the forward facing

01:44.350 --> 01:46.430
vector is that way.

01:46.810 --> 01:56.500
So if he's facing that way, then you could be pretty happy in saying that he can see the robber even

01:56.500 --> 01:58.060
though he's not directly facing him.

01:58.090 --> 02:00.100
So we have these two vectors.

02:00.100 --> 02:04.690
We have the forward vector that the facing direction for your cop.

02:04.960 --> 02:08.910
And then there's also going to be the vector to the robber here.

02:09.250 --> 02:16.810
And so we're going to actually test what the angle is between the forward facing of the cop and the

02:16.810 --> 02:18.340
direction to the actual robber.

02:18.610 --> 02:22.930
Now it's within a certain range, say, like 60 degrees.

02:22.930 --> 02:27.190
We can say that the cop is looking in the direction of the robber.

02:27.730 --> 02:30.670
Therefore, the robber at that point will go and hide.

02:31.570 --> 02:38.270
If not so, the cop's face the other way and is wandering away, then the robber is going to pursue.

02:39.490 --> 02:43.930
So let's go into our bot code and start adding this in.

02:44.350 --> 02:52.780
The first thing we will need is another method that's telling the agent if the target can see them.

02:53.200 --> 02:56.350
So we'll create another little method down here that returns.

02:56.350 --> 02:57.030
True or false.

02:57.040 --> 03:03.490
So Bull and let's say Target can see me.

03:04.940 --> 03:13.460
And for this, we need to work out, Victor, three to agent.

03:15.180 --> 03:27.990
Equals this dot transform the position minus the target's position, target, dot transform.

03:30.050 --> 03:30.800
Position.

03:33.090 --> 03:43.530
Then we can work out the angle, so float looking angle equals vector three, the angle between the

03:43.530 --> 03:58.950
target dot transformed forward and the two agent and then will say if looking angle is less than 60,

03:59.460 --> 04:00.690
we will return.

04:01.230 --> 04:01.580
True.

04:01.590 --> 04:05.970
Yes, we can see who else we're going to return.

04:06.300 --> 04:06.840
False.

04:08.040 --> 04:08.490
All right.

04:08.490 --> 04:09.930
So that's the target.

04:09.930 --> 04:10.620
Can see me.

04:10.860 --> 04:20.910
What we do now down in our update is to use that in our IF statement here so can see Target and Target

04:21.690 --> 04:30.390
can see me, then we will go and hide or else we'll do a pursue.

04:34.080 --> 04:40.730
All right, so save that, let's go back into Unity and Pressplay and have a look how this works.

04:41.310 --> 04:50.670
So we're currently looking at the cop, so he's going to go and hide now if I turn around and start

04:50.670 --> 04:51.890
to wander away.

04:52.980 --> 04:55.500
He's going to chase me.

04:57.630 --> 04:59.930
Just find a safe and come back over this way.

05:01.490 --> 05:08.270
I'll go over here, and when he gets here, I'll turn around and go, Oh, I can see you, and he goes

05:08.270 --> 05:10.400
and hides behind the tree.

05:11.210 --> 05:13.900
And let me now move over here.

05:15.600 --> 05:20.040
And I'm just trying to get him to go and hide behind.

05:21.750 --> 05:27.870
They're OK now, what's going to happen if you keep looking at him, he's going to keep popping out

05:27.870 --> 05:36.330
around the corner because he's hiding and obviously you can't see him well, you're facing in that direction,

05:37.080 --> 05:39.810
but you can't do that.

05:39.810 --> 05:44.460
Can see me think that's going to cause the hide to happen.

05:44.820 --> 05:47.190
Therefore, he's just going to keep popping out, checking.

05:47.640 --> 05:48.760
Is it okay to come out here?

05:48.780 --> 05:50.030
Is it okay to come out yet?

05:50.220 --> 05:57.700
Now, once I turn around, then he's going to come and pursue, which is fine.

05:57.770 --> 06:04.560
OK, now you might actually want him to complete the hiding.

06:04.560 --> 06:07.140
So if we're like out here.

06:09.430 --> 06:15.670
And I turn around and see him, and then I turn back again, he will come and chase me.

06:17.800 --> 06:24.040
Because there's no sort of delay or cool down in your action and you might want that, you might want

06:24.040 --> 06:25.210
him to actually do that.

06:30.860 --> 06:38.510
So let's now put that into the code where we're actually going to finish or not finish, but cool down

06:38.690 --> 06:46.610
a particular behavior, which means that if he has seen you, then you're just going to run and hide

06:47.120 --> 06:50.750
regardless of whether he turns back immediately or not.

06:51.240 --> 06:57.380
So it will put something in like, you know, for five seconds, just continue to go and hide or, you

06:57.380 --> 07:02.510
know, just continue to pursue without changing back and forth, because these will flip back and forth

07:02.510 --> 07:05.210
between these behaviors based on the true or false.

07:05.780 --> 07:12.050
And if they do, it actually doesn't give you any time to sort of complete any particular behavior.

07:12.050 --> 07:13.880
And you might want that to happen.

07:13.910 --> 07:20.510
OK, so what we're going to do is we're going to, first of all, add in a new little bool value, which

07:20.510 --> 07:27.080
is going to be called cool down, and we'll set that initially to false.

07:28.070 --> 07:37.940
And then down in our update, what we're going to do here is put if we're not cooling down, then we

07:37.940 --> 07:40.330
can choose a behavior.

07:41.570 --> 07:44.430
So that's just these.

07:45.380 --> 07:47.720
Now, when does cool down become true?

07:47.720 --> 07:52.890
We'll cool down is going to become true when we are doing this hide.

07:52.940 --> 07:57.800
So that will actually spend some time hiding before coming out of hiding.

07:58.250 --> 08:03.680
So in here with this clever hide, after you've executed it once, we're going to say cool down.

08:05.390 --> 08:07.800
Equals true like that.

08:07.820 --> 08:14.120
Now, that's going to stop this coming back in here again, but at some point we've got to set cool

08:14.120 --> 08:18.820
down back to false so that we can come in and choose a new behavior.

08:19.100 --> 08:25.900
So I'm going to have and invoked method and so will invoke under here behavior.

08:26.510 --> 08:27.350
Cool down.

08:29.030 --> 08:34.870
And we will do that in five seconds, and that means we actually need to right this method, so just

08:34.880 --> 08:39.740
up here will have a void behavior cool down.

08:40.860 --> 08:43.530
And all that's going to do is go cool down.

08:45.440 --> 08:52.430
Equals false and set it back to false after these five seconds for us now, of course, you could have

08:52.880 --> 08:55.370
behavior cooldowns for the different behavior.

08:55.410 --> 08:57.290
This is just a generic one for everything.

08:57.650 --> 09:03.410
And you could have other behaviors that you're doing in here with different it invokes of the cool down

09:03.410 --> 09:04.280
to complete them.

09:04.280 --> 09:10.550
But we're going to do five seconds here, which means that once he does go and trigger hiding, if the

09:10.550 --> 09:16.610
cop turns around or not, he's still going to complete that hiding and he will hide for five seconds

09:16.610 --> 09:18.680
before he even comes out to check again.

09:19.070 --> 09:19.340
All right.

09:19.350 --> 09:23.800
So let's press play and we will be facing him initially.

09:23.820 --> 09:25.100
Okay, so he goes and hides.

09:25.100 --> 09:29.250
Now, after five seconds, he's going to come back and check if the coast is clear.

09:30.230 --> 09:33.450
Now, see how that's just a little bit better than him bouncing back and forth.

09:33.950 --> 09:39.850
So if I turn around when he comes out, he's going to think it's OK.

09:39.910 --> 09:40.690
No, no.

09:40.850 --> 09:41.540
You can see me.

09:41.540 --> 09:43.150
I'm going to go and hide again.

09:47.290 --> 09:49.070
And I'll be looking when it comes around the corner.

09:49.210 --> 09:54.750
There he goes, and he's going to go and hide and he'll come back again.

10:01.470 --> 10:10.920
OK, so that is a really simple way of adding some really sort of complex behaviors into our system

10:11.700 --> 10:19.320
just by putting in a few statements of the conditions when you're going to run them and executing the

10:19.320 --> 10:23.670
behaviors we've already written and just combining them to get different effects.
