WEBVTT

00:06.350 --> 00:07.300
Okay.

00:07.310 --> 00:14.180
Thus far we spent a lot of time coding and not very much time being challenged except to follow the

00:14.180 --> 00:18.770
code and get it implemented, of course, but nothing where you've sort of been able to extend your

00:18.770 --> 00:19.070
knowledge.

00:19.070 --> 00:25.730
So this whole video is dedicated to a challenge that I'd like to show you that I've set up.

00:25.940 --> 00:26.510
All right.

00:26.510 --> 00:30.070
So I've got my character.

00:30.080 --> 00:36.770
Let me just move my player character away so he doesn't start shooting immediately because that just

00:36.770 --> 00:37.820
gets annoying.

00:38.070 --> 00:41.870
Okay, so let's press play and I'll show you what this does.

00:42.770 --> 00:49.300
So I have created a cube in the environment which is there.

00:49.310 --> 00:57.620
Now, this cube is a safe location and I've got it tagged with safe as a f e in capital letters.

00:58.040 --> 01:05.360
Now what happens is that ARG is still going to do exactly what it was doing before, except we now can

01:05.360 --> 01:09.200
sneak up behind him and give him a fright.

01:09.200 --> 01:17.540
So if we get within a angle, viewing angle or a negative viewing angle of 30 and a distance of two,

01:17.570 --> 01:18.940
it basically is going boo.

01:18.950 --> 01:19.640
And off he goes.

01:19.640 --> 01:27.860
He'll run over to that cube where he's now safe and then he will come back and continue on his patrol

01:27.860 --> 01:29.360
as long as you're not close enough.

01:29.360 --> 01:31.430
So let's go and see if we can scare him again.

01:31.760 --> 01:33.200
We'll come up this way.

01:34.090 --> 01:37.620
And get into their loop.

01:38.170 --> 01:38.860
Scared him off.

01:38.860 --> 01:40.840
He goes back to the cube.

01:40.840 --> 01:47.450
Now I guess if we stay here and he turns around, he's going to come and shoot at us like that.

01:47.480 --> 01:47.700
Okay.

01:47.710 --> 01:50.650
So that behavior is still there as well.

01:50.770 --> 01:51.400
All right.

01:51.400 --> 01:54.010
So that's basically what I want you to do.

01:54.010 --> 02:03.490
I want you to add in a runaway state to our code and to run away to a safe location, which is this

02:03.490 --> 02:03.820
cube.

02:03.820 --> 02:04.810
So there's the cube.

02:04.810 --> 02:08.680
And as I said before, the tag for it is safe.

02:09.160 --> 02:09.670
Right.

02:09.670 --> 02:11.530
So big challenge.

02:11.590 --> 02:13.060
Pause the video now.

02:13.090 --> 02:13.810
Go to that.

02:13.810 --> 02:17.410
And when we come back, I'll step through it with you.

02:18.590 --> 02:25.030
The first thing you need to do to get this to work is to create your cube or your safe place.

02:25.080 --> 02:26.330
Doesn't matter what it is.

02:26.330 --> 02:31.970
It just needs to be a game object that has a position in it and that position you're going to use as

02:31.970 --> 02:35.270
a set destination for your agent.

02:35.510 --> 02:40.310
Now, it's very important that you tag this with something so that you can identify it.

02:40.340 --> 02:47.240
So over in the inspector, what I did with this particular cube is I went into tags and I created one

02:47.240 --> 02:54.350
called Safe with a Capital S and ticked it to make sure that that is my safe location.

02:54.350 --> 02:57.740
And you can put that anywhere you like in the map.

02:57.770 --> 03:02.390
Now the rest of the functionality all happens within the state file.

03:02.390 --> 03:04.730
So open up this state code file.

03:04.940 --> 03:13.580
And what I've done is first of all at the top with states, our enum, I've added in one called run

03:13.580 --> 03:17.570
away because that's what we're going to essentially be doing running away.

03:18.080 --> 03:27.740
Now going back down here where we're doing a can C player and attacking in between those I've added

03:27.740 --> 03:39.500
in a new method which is going to determine if the player is behind the NPC at a distance of less than

03:39.500 --> 03:43.400
two and a angle of 30.

03:43.400 --> 03:49.130
Or at least that would be an angle of 60 because it's only calculated on a half.

03:49.430 --> 03:51.050
So we get the direction.

03:51.050 --> 03:55.040
Now the direction this time is going to be the other way around.

03:55.040 --> 04:02.570
And if you have a look down here at can attack player, the direction is from the NPC to the player.

04:02.570 --> 04:09.830
This time I'm calculating a different vector that's from the player to the NPC so that when we get the

04:09.830 --> 04:16.400
angle, we're calculating that angle based on the back of the character, not the front.

04:16.730 --> 04:24.410
So the angle itself, the calculation for that is the same as we used before just up here.

04:24.710 --> 04:30.650
And the only difference is that we've got this direction vector facing in the other direction.

04:31.310 --> 04:38.540
Then we check for the magnitude being less than two and the angle being less than 30 and then we're

04:38.540 --> 04:39.230
going to return.

04:39.230 --> 04:45.050
True, it means we are behind the character, otherwise we will return false.

04:45.050 --> 04:49.100
So we're going to use this is player behind.

04:49.100 --> 04:53.030
Now we're going to trigger that inside of the patrol.

04:53.030 --> 05:00.800
So if we go down to where patrol is and have a little look in the update, we'll be able to see where

05:00.800 --> 05:02.570
that's added in here.

05:02.780 --> 05:11.990
So underneath the cansee player we're going to test is the player behind the NPC.

05:12.050 --> 05:20.570
If it is, it will then change the next state to become run away, which we're yet to write and then

05:20.750 --> 05:21.590
exit.

05:22.500 --> 05:24.960
So what does run away look like?

05:24.960 --> 05:27.510
Because it's the one that's doing all that functionality now.

05:27.510 --> 05:34.950
It's right down the bottom of my code and it's really quite simple and short.

05:35.340 --> 05:40.230
So the safe location, I've created a game object at the top.

05:40.230 --> 05:47.730
Now what you might have done, which probably would be slightly better than what I've done, is to put

05:47.730 --> 05:51.900
the safe location into your game environment.

05:51.900 --> 05:53.520
Singleton And set it up in there.

05:53.520 --> 05:55.650
So it's always available.

05:55.830 --> 06:04.500
Now that's going to be a less of a drain on processing than getting that safe location with the find

06:04.500 --> 06:08.160
game object with tag every time you have to run away.

06:08.670 --> 06:11.100
So do that if you like.

06:11.100 --> 06:20.160
But I've done it this way now within the set up for the actual run away in its construction, we name

06:20.160 --> 06:28.980
it, and then we set the safe location game object to find the game object that has safe on it.

06:28.980 --> 06:30.780
Now it could have been a tree.

06:30.780 --> 06:33.870
Even in that environment, it doesn't matter what it is.

06:33.900 --> 06:39.240
We only need a vector three position for our agent to run to.

06:39.720 --> 06:45.600
Now we have our enter our update and our exit and they're quite short really.

06:45.600 --> 06:49.620
Let's have a look very similar to the ones that we've had before.

06:49.710 --> 06:52.440
So the anim is set to running.

06:52.530 --> 06:54.300
I decided that he's going to run away.

06:54.300 --> 07:02.070
Should use the run animation where then going to set agent is stopped to false so that we can create

07:02.070 --> 07:03.420
a path for him.

07:03.540 --> 07:10.110
The speed I've set to six, so it's a little bit faster than he was running towards you before.

07:10.470 --> 07:20.040
And then in the same enter we're doing a set destination for our safe location, dot transform dot position.

07:20.040 --> 07:28.230
Now the reason I've put this in enter rather than inside of update, if you remember I talked about

07:28.230 --> 07:34.260
setting a destination and then the game kind of needing to go through a whole update loop before that

07:34.260 --> 07:36.150
path has been calculated.

07:36.360 --> 07:41.820
Putting it in enter is going to do that for you because it won't be until the next game loop or the

07:41.820 --> 07:49.410
next update that we come back in and do the update that is on the state, which means by then we have

07:49.410 --> 07:54.930
our path calculated and we can then test for the remaining distance.

07:54.930 --> 07:58.650
Now the remaining distance in this case is less than one.

07:58.650 --> 08:07.110
So when the NPC pretty much hits the top of that cube, we're going to go back into the idle state and

08:07.110 --> 08:14.460
remember the idle state then has your 10% chance of going into patrol, which he does quite quickly,

08:14.640 --> 08:16.710
and that's pretty much it.

08:16.710 --> 08:24.000
So now you can see the power of creating these finite state machines and the state class.

08:24.000 --> 08:31.470
And as long as you have your enter your update and your exit code, taking care of the transitions between

08:31.470 --> 08:37.590
states, it makes it very easy just to keep adding more and more behaviors for an NPC.

08:37.590 --> 08:40.290
So it's very powerful concept.
