WEBVTT

00:12.660 --> 00:17.760
Hoarding has got to be one of my favorite steering behaviors because it's kind of like a little bit

00:18.090 --> 00:24.180
sneaky, and you can also create things that aren't trying to just hide from a particular character,

00:24.300 --> 00:26.940
but are hiding as they're sort of sneaking up.

00:26.940 --> 00:33.360
So hiding and pursuing, which gives you really interesting dynamics in a game environment.

00:33.750 --> 00:39.360
So in order to do this is actually quite simple, but there's an awful lot of work involved.

00:39.370 --> 00:44.410
First of all, you need a whole bunch of obstacles that your agent can hide behind.

00:44.430 --> 00:47.550
So we've got our tree that's been sitting there the whole time.

00:47.730 --> 00:54.750
We're going to use that now in order to figure out where the best spot to hide is.

00:55.050 --> 01:02.910
If the robber is going to hide from the cop, then what we do is we get a vector from the cop to the

01:02.910 --> 01:10.440
tree and keep going through the center of the tree to the opposite side, which means that if you're

01:10.830 --> 01:20.370
on the exact opposite side of the tree from the cop, then you're obviously hidden from the cop, because

01:20.370 --> 01:26.670
if you tried to do a recast from the cop to the robber or the robber to the cop, they wouldn't be able

01:26.670 --> 01:31.140
to see each other because it would hit the tree and therefore you are hidden.

01:32.130 --> 01:39.790
So first of all, what we need to do is define what all the hiding spots in the actual world are.

01:41.130 --> 01:47.340
So back in our environment, if we have a look in the hierarchy, under the environment object, you'll

01:47.340 --> 01:51.810
find the tree object that's been sitting there for quite a while.

01:51.810 --> 01:57.690
And if you click on it, you'll see that it's tagged with the tag hide in lowercase.

01:57.690 --> 02:02.730
Now, if yours doesn't happen to be because of the way it imported, make sure you just create a new

02:02.730 --> 02:05.850
tag and set the tree to hide.

02:05.850 --> 02:12.400
And then we can use that hide tag for any object you want to put in the environment to hide behind.

02:12.840 --> 02:18.180
Remember also that the tree is a navigation static.

02:18.330 --> 02:26.910
OK, so that means over here we've got it set to navigation static and when we then make our navigation

02:26.940 --> 02:31.110
mesh, it's going to exclude the tree.

02:31.380 --> 02:40.200
OK, so if we just hit back and we'll do a quick break for us, we're going to find that it has been

02:40.200 --> 02:42.150
eliminated as a spot that you can stand.

02:42.160 --> 02:48.000
So if you go and add multiple trees, always come back and do this bike on your navigation.

02:48.420 --> 02:56.760
OK, so now that we've got that, we also want to make the tree a collider now, not because we want

02:56.760 --> 03:02.840
our character to collide with it, but because at some point we want to be able to cast a ride through

03:02.840 --> 03:05.130
it and see if we can see the cop.

03:05.490 --> 03:12.180
So select the tree and go to the inspector add component and I'm going to add a collider.

03:12.180 --> 03:13.740
We want to add a box collider.

03:13.740 --> 03:15.800
That's just because it's going to be simple.

03:16.320 --> 03:21.450
Now, with a tree, you will get a box collider that sits quite neatly around that tree.

03:21.450 --> 03:23.610
You'll be able to see over in the same view.

03:24.030 --> 03:31.440
If you've got some other object and it's not fitting neatly, then you may want to just adjust the center

03:31.440 --> 03:35.180
and the size of it in the inspector for that collider.

03:35.700 --> 03:41.910
And now that you've got this tree with the tag and the collider, you could essentially make a prefab

03:41.910 --> 03:46.050
of it or copy and paste or duplicate it around the environment.

03:46.050 --> 03:49.050
And then you've got lots of places to hide.

03:49.950 --> 03:50.210
Right.

03:50.310 --> 03:51.960
So we've got all these hiding places.

03:51.960 --> 03:53.310
We've got one at the moment.

03:53.310 --> 03:58.350
It's always good to test with just one thing so that we can figure out what went wrong.

03:58.350 --> 04:04.710
If it does, we need to pick up all of these trees at the very beginning of pressing play.

04:04.710 --> 04:09.420
So I'm going to create a singleton and I'm going to call that Singleton the world and it's going to

04:09.420 --> 04:13.200
hold on to all of our hard positions.

04:13.680 --> 04:20.340
OK, so let's call it world and let's go into here now.

04:20.340 --> 04:25.630
What we're going to do with this is not make it into a model behavior.

04:26.690 --> 04:27.990
Let's get rid of that.

04:28.320 --> 04:40.160
And we're going to make it a public sealed class and then we're going to create a private static read

04:40.200 --> 04:46.800
only world instance, which will equal a new world.

04:49.140 --> 04:51.870
And will also create a private.

04:53.480 --> 05:04.310
Static game object array, which will hold all of our obstacles and let's call them hiding spots.

05:05.770 --> 05:12.160
That, OK, they're more critical of our constructor's so static world.

05:16.140 --> 05:21.330
And in this constructor, it's actually going to go through the world and grab all of the hiding spots,

05:21.780 --> 05:23.220
so hiding spots.

05:24.540 --> 05:34.660
Equals game object dot, find game objects with tag, make sure it's the fun game objects for the nest

05:34.660 --> 05:41.910
so that you get the whole array and we want them with tags with hyd, which are all in lowercase.

05:42.810 --> 05:47.100
OK, so then we need to finish off our singleton of private world.

05:48.100 --> 05:54.160
Which is basically empty and a public static world.

05:56.000 --> 05:56.960
Instant's.

05:59.980 --> 06:01.120
With a.

06:02.630 --> 06:07.790
Get in there, so get return instant's.

06:10.660 --> 06:18.580
Which will be a lowercase I because we're talking about this one up here, I and then we'll also have

06:18.580 --> 06:22.330
another method, public game object.

06:23.490 --> 06:29.610
Array, which gets the hiding spots, so get hiding spots.

06:31.180 --> 06:35.650
And there we're going to return hiding spots.

06:38.430 --> 06:43.710
Now, that means that this object is going to go and get all hot spots and any agents in the environment

06:43.710 --> 06:47.130
just have to ask the world for them from the world.

06:47.130 --> 06:50.260
Singleton And they don't have to keep getting them all of the time.

06:50.290 --> 06:51.450
OK, so save that.

06:52.710 --> 06:55.030
And now let's go back to Unity.

06:55.080 --> 07:02.250
The next thing we're going to do is to change our bot to now do the hide.

07:02.700 --> 07:08.250
OK, so open up your bot script and let's now add in a hide method.

07:08.260 --> 07:11.580
So down here we're going to put in void hide.

07:14.220 --> 07:24.390
Now, what Hyde needs to do is to find the best hiding place now that can be the closest hiding spot

07:24.390 --> 07:30.780
if you want them to hide really quickly or it can be the furtherest hiding spot in this case, we're

07:30.780 --> 07:35.930
going to find the closest hiding spot to the agent and go and hide there.

07:36.540 --> 07:42.330
OK, so we need a distance value to hold the distance that we're comparing things against.

07:42.330 --> 07:45.170
So initially, let's just set that to --.

07:45.180 --> 07:51.630
If dot infinity so that it's really, really large and that anything you compare it with is got to be

07:51.630 --> 08:01.800
smaller and then we need a vector three for our chosen spot and initially we'll set that to vector three

08:02.490 --> 08:03.160
zero.

08:04.500 --> 08:09.030
OK, then we're going to loop through all of the hiding spots with a full loop.

08:09.030 --> 08:21.930
So for int I equals zero or is less than worldat instance doget hiding spots.

08:23.900 --> 08:24.560
Length.

08:26.630 --> 08:27.800
I plus, plus.

08:29.170 --> 08:33.010
OK, then we're going to create a victim three, which will be our hide.

08:34.270 --> 08:42.430
Direction, and that's going to equal our tree, so our world instant's.

08:43.620 --> 08:45.750
Don't get hiding.

08:47.780 --> 09:03.270
Spots or spots, brackets I dot transformed position minus target, dot transformed position.

09:03.290 --> 09:07.070
So this is the vector from the cop to the tree.

09:07.980 --> 09:13.100
OK, so Victor three for our hide position.

09:14.420 --> 09:23.510
Is going to equal the same thing again, so I'm just going to grab this whole position of the tree.

09:26.100 --> 09:33.630
So that's the position of the tree, then what we're going to do is actually add in a little bit more

09:33.630 --> 09:35.790
room so that you actually go past the tree.

09:36.150 --> 09:43.980
So to do that, you just add in the hard direction vector to that and we want to normalize it.

09:43.980 --> 09:49.290
So the normalized version, so we know the length of one and then we can set a distance behind that

09:49.290 --> 09:50.580
tree that we want to hide.

09:51.210 --> 09:56.910
In this case, we're just going to hide at a distance of five behind the tree and you can play around

09:57.000 --> 10:00.470
with that depending on how close you want to be to that particular tree.

10:00.480 --> 10:05.870
And we will actually improve on this algorithm later on and get rid of that five.

10:05.880 --> 10:07.800
But for now, let's set it to five.

10:08.370 --> 10:09.810
OK, so we've got that.

10:09.810 --> 10:14.940
Now, what are we going to do is check if the next tree that comes along is actually closer?

10:14.940 --> 10:18.330
And if it is, we will use that as our chosen spot instead.

10:18.870 --> 10:32.670
OK, so we want if vector three don't distance this dot transform drop position, hide, pause.

10:32.700 --> 10:37.140
So this is the distance between our agent and the hiding position.

10:37.140 --> 10:42.850
If that's less than dist, then that's where we're going to go and hide.

10:42.900 --> 10:47.970
So in here we'll put chosen spot equals hide.

10:50.400 --> 10:51.240
Paw's.

10:52.900 --> 11:01.660
And then we'll update distance, so distance is going to equal the victor through distance here that

11:01.660 --> 11:05.610
we just tested, so grab that and put it down there.

11:05.620 --> 11:11.450
So it's going to now be the distance to the closest trade that we've found so far.

11:12.130 --> 11:15.370
OK, and then we'll finish our for loop.

11:17.190 --> 11:21.810
This he should be dissed, not what it's updated to so destroy.

11:24.060 --> 11:33.900
And now that we've got that, we can actually go Agent Dot Sitt destination to the chosen spot.

11:36.610 --> 11:43.780
And if we really want to be consistent rather than doing that, because that's what Seke is doing anyway,

11:43.780 --> 11:47.450
what just you see in case we do something different to seek.

11:47.980 --> 11:49.990
OK, so that's our hide.

11:50.440 --> 11:53.590
Quite straightforward when you think about it.

11:54.220 --> 11:54.880
Save that.

11:55.090 --> 12:00.790
Now, what we're going to do is in our update will will sit this to be hiding.

12:01.910 --> 12:09.020
OK, so let's save it now, let's go back into unity, and we're pretty much ready to play and test

12:09.020 --> 12:09.530
this out.

12:09.530 --> 12:14.680
So press play and off our agent goes and hides behind the tree.

12:15.230 --> 12:15.420
Right.

12:15.530 --> 12:18.410
So now if I move over here.

12:20.290 --> 12:25.870
He's going to go over there and if I move again, he's going to go there, so he's constantly hiding.

12:27.410 --> 12:35.690
Behind the tree, which is not great for us, but if you think about the processing that's going on

12:35.690 --> 12:39.520
now, the hide algorithm is running every update.

12:40.130 --> 12:47.880
If he's already hidden from the cop, then we don't need to keep running that until a point comes where

12:47.880 --> 12:49.180
he's not hidden anymore.

12:49.400 --> 12:52.100
And we will fix that in the next video.

12:52.280 --> 12:56.080
But for now, you probably want to add a few more trees to see how this works.

12:56.090 --> 13:01.490
So let's just go into the environment and grab the tree that you've got.

13:01.490 --> 13:03.720
And I going to go controlled.

13:04.580 --> 13:14.630
I hit the key and then just make a new tree and move it around, control D and move it around.

13:15.020 --> 13:18.110
Drill D there.

13:18.110 --> 13:19.490
And what did I say?

13:19.490 --> 13:20.150
You have to remember.

13:20.180 --> 13:26.750
Do you have to go into your navigation and you have to remember to bike to make sure that you've done

13:26.750 --> 13:29.810
that for the new NAV Meshach.

13:29.840 --> 13:36.680
So Pressplay now he's going to go and hide behind that tree because it is actually the closest and you

13:36.680 --> 13:37.850
can actually see that.

13:38.420 --> 13:43.550
And now because of the way he's currently programmed, you might not be able to.

13:46.490 --> 13:50.690
For seemed to go to a different tree, depending on whether you can find months closer.

13:52.620 --> 13:59.460
No, now, one way we can do that is if we go back into the code and remember this little five we put

13:59.460 --> 14:05.190
about how far away we are from the tree, if we hide behind a tree at a certain distance from that tree

14:05.190 --> 14:09.340
and then that distance ends up being closer to another tree, later on, he will move.

14:09.360 --> 14:13.920
So this update this to, let's say 10, which I think is probably going to be stretching things a bit

14:13.920 --> 14:15.000
as far as the distance.

14:15.780 --> 14:19.590
But let's just press play and.

14:20.680 --> 14:24.430
It's going to hide behind that tree because it's the closest, but it's going to go to a distance of

14:24.430 --> 14:24.850
10.

14:24.860 --> 14:26.680
So let's just bring this down.

14:26.690 --> 14:28.480
You can see that he's now over here.

14:29.810 --> 14:33.410
So I'm going to go and chase him up with my cop.

14:35.180 --> 14:42.230
And hopefully you push him now he goes and hides behind that tree at a distance of 10.

14:43.660 --> 14:47.590
And so if I come back over here and force him around the other side.

14:48.970 --> 14:53.200
At a distance of 10, oh, I don't know which one to be closer to.

14:53.380 --> 14:54.160
Yeah, that one there.

14:54.280 --> 14:56.140
OK, so it's gone over there.

14:57.110 --> 14:58.140
OK, great.

14:58.870 --> 15:03.430
And you can see that a distance of ten of hiding behind a tree looks a little bit unnatural because

15:03.430 --> 15:06.900
if you were to hide behind a tree, you'd be right up against the tree.

15:06.940 --> 15:10.240
And as I said, we will get around to fixing that.

15:10.630 --> 15:14.070
OK, so that's the basic hyd method.

15:14.530 --> 15:20.410
We're going to come back in the next video in part two and actually improve on that.
