WEBVTT

0
00:11.130 --> 00:16.320
The simplest way to create an NPC that makes decisions about what to do, based on the location of

1
00:16.320 --> 00:18.870
the player is Line Of Sight.

2
00:19.350 --> 00:24.100
Line Of Sight works by calculating the angle and distance from the NPC to the player

3
00:24.150 --> 00:27.460
and using this data to decide on what action to take.

4
00:27.570 --> 00:33.570
It's a very easy way to create a purely reactive agent whose behavior is determined by whether or not

5
00:33.570 --> 00:34.920
they can see the player.

6
00:35.040 --> 00:38.420
To implement this, the NPC is given a field of vision.

7
00:38.550 --> 00:44.670
This is a cone extending from the location of the NPC in its forward-facing direction, for the length

8
00:44.670 --> 00:51.060
of its visible range and spreading across an angle of vision. To determine if a player falls inside this

9
00:51.060 --> 00:51.450
cone,

10
00:51.450 --> 00:55.110
we calculate the vector from the NPC to the player.

11
00:55.110 --> 01:00.270
We then determine if the 'distance to the player' is less than the 'visible range' and if the 'angle to the

12
01:00.270 --> 01:06.690
player position' is less than that of the 'angle of vision'. If both conditions are true, the player is visible

13
01:06.690 --> 01:08.040
to the NPC.

14
01:08.040 --> 01:14.610
Follow along as I show you how to set up this type of Line Of Sight NPC. Download the AIRobotGuard.unitypackage

15
01:14.610 --> 01:16.360
that comes with this lecture.

16
01:16.440 --> 01:21.880
Create a new Unity 3D project and drag-and-drop that package file into your Assets.

17
01:21.990 --> 01:28.860
When you do, you'll get these 2 items. You'll get a Scene called RobotGuard (which you can open up and

18
01:28.860 --> 01:34.110
you'll see the robot guard standing in the scene) and you'll get a folder of its artwork.

19
01:34.170 --> 01:36.780
The other items, you're going to add.

20
01:37.110 --> 01:43.560
So once you've got that ready to go, we're going to add in a first-person controller, so that we can walk

21
01:43.560 --> 01:47.700
around and test our proximity to the NPC.

22
01:47.700 --> 01:54.680
Go into 'Assets > Import Package' and bring in the 'Characters' package. That's the one with the first-person,

23
01:54.690 --> 02:01.710
third-person etc. things in it, so bring all of those items in and that will give you a

24
02:01.800 --> 02:06.100
Standard Assets folder in which you'll find Characters

25
02:06.540 --> 02:11.800
and then we want the first person 'Prefabs' and the FPSController.

26
02:11.820 --> 02:13.690
So grab hold of that,

27
02:13.740 --> 02:18.900
drag it into the scene and just lift it up so that it's above the ground.

28
02:19.470 --> 02:23.010
Now once you've done that, it will have its own camera on it.

29
02:23.310 --> 02:28.370
So the camera that's in the scene, you can delete (or just turn that off).

30
02:28.410 --> 02:33.430
Now to test this is working, just press Play and you should be able to wander around the environment...

31
02:37.100 --> 02:38.860
like that.

32
02:40.080 --> 02:40.540
Ok.

33
02:40.700 --> 02:45.120
Press the Esc key to get your mouse back and stop playing.

34
02:45.410 --> 02:45.680
Ok.

35
02:45.680 --> 02:49.950
So back in Assets, we're going to create an AIController script.

36
02:50.000 --> 02:57.290
I've already got one here but you want to 'Create > C# Script' and call it AIController. Inside

37
02:57.290 --> 02:58.190
that script,

38
02:58.220 --> 03:00.380
you'll add this.

39
03:00.800 --> 03:05.440
So this is the basic script to get our Line Of Sight working.

40
03:05.510 --> 03:11.300
First of all, we need to know where the player is, so we're going to have a public transform at the top

41
03:11.300 --> 03:14.550
to pass through the player.

42
03:14.840 --> 03:21.380
Next we have a bunch of variables about the behavior for the NPC itself.

43
03:21.380 --> 03:29.510
So we have the usual 'rotationSpeed' and 'speed' to turn and move the character and then we have our

44
03:29.510 --> 03:30.230
Line Of Sight values.

45
03:30.230 --> 03:37.870
So we've got a visible distance (so this is how far away from the character it can detect the player,

46
03:37.910 --> 03:40.360
in this case it's 20),

47
03:40.550 --> 03:51.940
the visible angle is set to 30 (so it can see you at a distance of 20, within an angle of 30 degrees to

48
03:52.090 --> 03:53.410
either side of it.

49
03:53.410 --> 04:00.610
So this angle is like a half of the complete field of view which would be 60).

50
04:00.760 --> 04:09.370
Then we have a shoot distance and the shoot distance is how close the character has to be in order to

51
04:09.370 --> 04:10.770
start shooting.

52
04:10.780 --> 04:16.630
So if it's not close enough and it has detected you, it can run up to you until it's close enough to

53
04:16.630 --> 04:18.070
take a shot.

54
04:18.070 --> 04:27.310
Next we're going to store a state and this state will help us determine what animations to run a little

55
04:27.310 --> 04:34.150
bit later on. So the NPC will have 3 states, it'll have IDLE, it'll have RUNNING and it'll have SHOOTING

56
04:34.330 --> 04:36.930
and you'll see those come into play in the Update.

57
04:36.940 --> 04:41.420
Now down in the Update is where all of the work is happening.

58
04:41.770 --> 04:51.040
The first thing we do is calculate the direction from the NPC to the player. Ok, so remember, to do that

59
04:51.070 --> 04:57.940
it's the NPC taken away from the player position and that will give you your direction vector.

60
04:57.970 --> 05:06.340
Then we work out the angle between that direction vector and the forward-facing vector of the NPC and

61
05:06.340 --> 05:09.700
we can do that with our Vector3.Angle method.

62
05:09.700 --> 05:12.150
So that gives us an angle in degrees.

63
05:12.160 --> 05:20.260
After that, we're going to do an if-statement to test if the length of that direction vector is inside

64
05:20.260 --> 05:29.470
of our visible range and our angle is less than our visible angle, then we're good to go.

65
05:29.800 --> 05:35.430
After that, I'm setting the direction vectors 'y' to 0.

66
05:35.620 --> 05:40.190
So the direction vector is going to be in 3 dimensions.

67
05:40.360 --> 05:49.600
If your character is higher or lower than the robot, then having a different y-value will make it tilt

68
05:49.600 --> 05:55.390
and if you don't want it to tilt up or down to look at your character, you want it to stay grounded with

69
05:55.390 --> 05:58.620
its y-axis always facing directly up,

70
05:58.750 --> 06:07.570
then it's a good idea to set this change in the 'y' always back to 0 after you've done the calculation.

71
06:07.570 --> 06:11.230
Trust me, it's just going to keep things all on the horizontal.

72
06:11.230 --> 06:22.240
We then set the rotation of the NPC with a slerp (like we've done in the past) to slerp gradually towards

73
06:22.630 --> 06:26.240
the direction vector using our our rotationSpeed.

74
06:26.410 --> 06:34.170
So with this code, if the player is within the visible range of the NPC, it will always be turning towards

75
06:34.170 --> 06:34.460
it.

76
06:34.600 --> 06:40.480
Then we just have to decide whether we're going to push the NPC forward, towards the player.

77
06:40.480 --> 06:41.700
So down here,

78
06:41.770 --> 06:43.970
after that, we've got

79
06:44.130 --> 06:49.960
'if(direction.magnitude > shootDist)'

80
06:49.960 --> 06:57.910
so that means we're out of range for shooting but we're still within range to run after the character

81
06:57.910 --> 07:01.190
(because this is inside this if-statement up here),

82
07:01.360 --> 07:06.920
so if we can't shoot, then we're going to run towards the player,

83
07:07.330 --> 07:10.150
otherwise we're going to shoot

84
07:10.270 --> 07:12.920
and so we're setting this 'state' value.

85
07:13.120 --> 07:20.020
If the player is outside of our range, then we just set the state to IDLE with this 'else' here,

86
07:20.020 --> 07:24.000
so make sure that you've got this if-statement correct,

87
07:24.010 --> 07:26.530
otherwise these will go wrong for you.

88
07:26.530 --> 07:29.820
Then after that (and outside of this if-else statement,

89
07:29.830 --> 07:39.260
We have an 'if(state == "RUNNING")' then push the character forward, on this line here.

90
07:39.290 --> 07:43.870
So this is the only time that you want to Translate the character, when it's in the RUNNING state,

91
07:44.200 --> 07:48.050
otherwise it's standing still (IDLE) or SHOOTING.

92
07:48.170 --> 07:51.310
Right, so save that, go back to Unity

93
07:51.440 --> 07:56.110
and we're going to add that code (drag-and-drop it onto your robot),

94
07:57.270 --> 08:02.390
select the robot, find where the code is and where 'Player' is exposed,

95
08:02.390 --> 08:09.450
drag-and-drop your FPSController into there, so the A.I. knows about it.

96
08:09.450 --> 08:09.660
All right.

97
08:09.660 --> 08:10.770
Let's test this out.

98
08:10.850 --> 08:14.620
Press Play and if we look around and find the robot...

99
08:14.630 --> 08:15.850
there he is.

100
08:15.920 --> 08:19.900
Now his animations aren't going to be working at this point.

101
08:19.910 --> 08:22.780
We just want to check, if we move away,

102
08:22.790 --> 08:24.770
notice how he's following us.

103
08:24.770 --> 08:27.360
This is him chasing us

104
08:27.830 --> 08:31.910
and when he gets close enough, he'll stop and then he'll go into the shooting

105
08:32.090 --> 08:37.420
and if we turn, he'll also keep turning towards us.

106
08:37.680 --> 08:43.710
So there should be a point (if we can get outside of his range) that he will actually stop moving altogether

107
08:44.280 --> 08:46.230
which is about there

108
08:46.560 --> 08:56.640
and then if we stay outside the range and walk around behind him (outside of his visible angle) we should be

109
08:56.640 --> 09:03.040
able to sneak up from the side and he won't detect us.

110
09:03.250 --> 09:13.280
So he's not detecting us because he's not turning towards us but if we move around within range (let's come out here) there, he detected

111
09:13.280 --> 09:17.400
us, turned towards us and started to move within shooting range of us.

112
09:17.420 --> 09:23.040
Ok, so that is the basic logic to this Line Of Sight.

113
09:23.210 --> 09:27.280
Now that the logic for the Line Of Sight is there, we can add in the animations.

114
09:27.320 --> 09:31.980
Now the robot I've given you has animations on it and set up ready to go.

115
09:32.180 --> 09:36.740
If you click on the robot, you'll find that it has an Animator in the Inspector.

116
09:36.920 --> 09:42.710
If you double-click on the RobotGuard, it will open up your Animator window for you and you'll be able to see

117
09:42.710 --> 09:43.600
what's on there.

118
09:43.760 --> 09:44.930
It's very simple.

119
09:44.930 --> 09:51.930
It's set up really similar to the stuff we've used before and basically it's the animation states,

120
09:52.370 --> 09:53.950
'idle', 'run' and 'shoot'

121
09:53.960 --> 10:00.600
and they're all linked by transitions and triggers on those transitions.

122
10:00.620 --> 10:05.860
So I've set those up, there's an 'isRunning' an 'isShooting' and an 'isIdle'

123
10:06.080 --> 10:11.780
and they all link between these animations states.

124
10:11.990 --> 10:16.660
So there's nothing you have to do for these, as far as setting that up

125
10:16.670 --> 10:26.810
but what we're going to do is trigger them in the code based on the state that the NPC is in. Just by

126
10:26.810 --> 10:33.050
the way of interest, this particular robot model came with a Unity project many, many years ago and

127
10:33.050 --> 10:35.330
I can't even remember which version it was.

128
10:35.330 --> 10:40.730
Maybe one of you can remember. Maybe it was version 2 or something because these animations were legacy

129
10:40.730 --> 10:45.530
animations, so I've updated those for the mecanim system.

130
10:45.530 --> 10:53.690
So let's go back to the code, back to our scene on there...Assets and open up the code.

131
10:53.690 --> 10:58.280
First thing we need to do (as always) is grab hold of his animator.

132
10:58.700 --> 11:07.750
So we set that up at the top and then in the Start, we grab hold of it, so 'anim = this.GetComponent'

133
11:09.920 --> 11:12.770
for the Animator, like that

134
11:12.840 --> 11:18.300
and then we have to set the triggers along with each state.

135
11:18.300 --> 11:23.480
So down in the Update, let's go to the first one for RUNNING.

136
11:24.300 --> 11:35.250
Now in here, we can put 'anim.SetTrigger("isRunning");' - remember to spell your triggers and capitalize

137
11:35.250 --> 11:40.690
exactly the same way as they're set up in the parameters in the Animator.

138
11:40.920 --> 11:47.460
Now that will set the trigger to cause the 'isRunning' animation to work.

139
11:47.460 --> 11:55.080
The only issue with doing it this way is, if the character is already running it's going to re-trigger

140
11:55.110 --> 11:56.770
and re-trigger and re-trigger and you can

141
11:56.820 --> 12:01.560
end up with weird things happening with multiple triggers going off.

142
12:01.710 --> 12:14.450
So we can actually test...'if(state != "RUNNING")' then we will set the state to RUNNING and set

143
12:14.450 --> 12:16.460
the trigger to 'isRunning'.

144
12:16.460 --> 12:20.150
So this says if you're not running, then start running

145
12:20.270 --> 12:24.430
and if you're already running, there's no need to start running again.

146
12:24.470 --> 12:30.460
The same thing for all of these other ones, so we're going to do the same with shooting. So it's

147
12:30.460 --> 12:33.350
'if(state != "SHOOTING")', 'state = "SHOOTING";

148
12:33.350 --> 12:36.530
and 'anim.SetTrigger("isShooting");'

149
12:36.740 --> 12:39.320
and once again, for idle.

150
12:39.530 --> 12:47.150
So now this will set our animations based on the state and it'll ensure that they don't get set twice

151
12:47.390 --> 12:51.620
in a row and then right at the bottom, outside,

152
12:51.620 --> 12:55.850
if we're running them we'll still perform our forward pushing

153
12:56.060 --> 12:57.890
Translation.

154
12:57.980 --> 12:59.770
All right, so save that

155
13:00.620 --> 13:03.240
and let's go back and try it out.

156
13:03.250 --> 13:10.430
So, press Play...he's going to start shooting (which is because we're right next to him)

157
13:10.610 --> 13:14.270
and now if we get out of range of shooting, he's going to run after us

158
13:15.210 --> 13:23.520
and then start shooting (I'm going to make my gameplay window a bit bigger) and if I manage to run out of range, he's

159
13:23.520 --> 13:26.950
going to lose interest and he'll go back into his idle state that

160
13:26.950 --> 13:28.350
you can see there

161
13:28.890 --> 13:30.570
and we should be able to...

162
13:35.310 --> 13:40.320
sneak up and because we're not inside of that range,

163
13:41.080 --> 13:48.760
he's taking no notice of us and I can turn around...get back into range and he'll start shooting and chasing

164
13:48.760 --> 13:56.980
us and if we turn while we're in range, he will turn.

165
13:57.560 --> 14:00.740
So that is very simple

166
14:00.840 --> 14:09.140
A.I. mechanics called Line Of Sight, to get your NPC to chase after the player based on a range

167
14:09.140 --> 14:10.190
of visibility.