WEBVTT

0
00:11.040 --> 00:14.090
In this lecture, we will look at the concept of 'Fleeing'.

1
00:14.380 --> 00:19.410
It's a common technique used in A.I. for getting an NPC to move away from a location.

2
00:19.450 --> 00:24.580
The technique can be used on any type of NPC. In this case, we will apply it to our crowd

3
00:24.580 --> 00:31.300
we've been working on. Mathematically, fleeing works like this...an object or location is introduced into

4
00:31.300 --> 00:32.570
an environment.

5
00:32.940 --> 00:38.740
The NPC determines where the object is in relation to their own position. They then calculate a new goal

6
00:38.740 --> 00:41.560
location that is away from the object.

7
00:41.650 --> 00:45.410
In this case, we will use the exact opposite direction.

8
00:45.430 --> 00:51.280
This can be calculated using the vector towards the object and taking that away from the NPCs position.

9
00:52.150 --> 00:53.800
With the new goal location,

10
00:53.800 --> 00:59.020
the agent can turn around and start heading for it, thus being taken away from the object in question

11
00:59.250 --> 01:05.020
Other factors we can consider are the 'radius of influence of the object' and the 'cooldown period'. The radius

12
01:05.020 --> 01:10.990
determines how close an NPC must be to an object to be affected by it and the cooldown is how long the

13
01:10.990 --> 01:18.820
NPC flees for, before returning to normal behavior. To implement fleeing in your current city crowd and

14
01:18.820 --> 01:25.180
to get to the point that I'm at now, you'll need to add in a cylinder and make a prefab out of it.

15
01:25.180 --> 01:34.280
So what I've done is go to the Hierarchy and 'right-click > 
3D Object > Cylinder' and I didn't change

16
01:34.280 --> 01:41.760
the size of it but what I did do was made a green material and attached it to that cylinder.

17
01:41.780 --> 01:49.730
I then took that cylinder from the Hierarchy and drag-and-dropped that into the Assets and that will create

18
01:50.210 --> 01:55.980
a prefab of that cylinder for you. I'll just delete my second version of it.

19
01:56.060 --> 02:02.670
Now this cylinder is doing nothing but marking the location where we're going to click.

20
02:02.750 --> 02:10.490
So we need some code to position the cylinder onto the NavMesh where we click with the mouse.

21
02:10.610 --> 02:20.330
So let's create some new C# code and let's call that 'DropCylinder', then open that up and we'll

22
02:20.390 --> 02:22.270
add in this code.

23
02:22.340 --> 02:27.420
So first of all we've got a public exposed game object that we're calling 'obstacle'.

24
02:27.440 --> 02:31.350
This is where we'll pass through our prefab of the cylinder.

25
02:31.430 --> 02:38.650
We're then going to keep an array of all of the agents that are in the scene

26
02:38.900 --> 02:46.870
and so therefore in the Start, we will collect all those agents by finding the game objects that have

27
02:46.870 --> 02:48.500
the tag 'agent'.

28
02:48.580 --> 02:56.090
This means you'll have to create the tag 'agent' and add it to your characters.

29
02:56.110 --> 03:05.590
So I've got my characters. I've put them under 'Crowd' and duplicated them. I created a fair few of them. Don't

30
03:05.800 --> 03:10.900
have too many because the more you have, the slower this is going to run

31
03:11.170 --> 03:12.570
but you want just enough.

32
03:12.580 --> 03:18.550
Now under this 'Crowd', if you just click on all of them then you can add a tag of 'agent'.

33
03:18.550 --> 03:25.840
So I created a new tag called 'agent', with a lower case 'a' and just set it to all of the characters that

34
03:25.840 --> 03:27.100
are in the crowd.

35
03:27.280 --> 03:33.040
So back to the code and those agents will be sitting in this 'agents' array

36
03:33.070 --> 03:38.740
and that means that when we drop the object, we can send a message out to all of them saying that that

37
03:38.830 --> 03:40.720
object is there. It's like

38
03:40.720 --> 03:48.850
the Agent Manager that we've had in previous lectures. Now in the Update is where the mouse button will

39
03:48.850 --> 03:57.250
be tested for being down and then we'll do a RaycastHit into the world from the screen position

40
03:57.250 --> 04:01.090
using the mouse position on the main camera.

41
04:01.090 --> 04:05.700
That means make sure that your camera is tagged as

42
04:05.700 --> 04:07.250
'MainCamera'.

43
04:07.430 --> 04:09.420
So just remember where that is...go over to

44
04:09.440 --> 04:14.660
camera and the tag for it should be 'MainCamera' here so that it picks that up.

45
04:14.660 --> 04:21.110
Now, we're doing our physics Raycast into the world, finding out where it hits...where it hits, we're

46
04:21.110 --> 04:24.200
going to instantiate a copy of our cylinder.

47
04:24.200 --> 04:30.300
So that's the obstacle, we're going to put it at the hit location and then we'll use the prefab's default

48
04:30.320 --> 04:34.090
rotation for it because it's really not that important.

49
04:34.430 --> 04:40.040
Then we've got a 'foreach' statement which is just going to loop through all of the agents in the scene

50
04:40.460 --> 04:47.840
and call a method on them (which we haven't written yet) but it will be called 'DetectNewObstacle' and we'll

51
04:47.840 --> 04:53.990
pass it the hitInfo.point, which is the location of where we're placing the cylinder.

52
04:53.990 --> 05:00.830
So this is essentially telling our agent that there's an obstacle that's being placed and now it's your

53
05:00.830 --> 05:07.520
job to flee from it. So save that...it will give you a little error because we haven't written this

54
05:07.580 --> 05:08.570
DetectNewObstacle yet

55
05:08.590 --> 05:10.410
but that's what we're going to do next.

56
05:10.550 --> 05:17.930
Open up the AIControl script and this is the script you should have attached to every single agent

57
05:18.020 --> 05:19.280
in the scene,

58
05:19.280 --> 05:24.020
so it's always a good idea to create one and then duplicate it so that you don't have to go through adding

59
05:24.020 --> 05:30.740
this on. My AIControl code is a little bit different from what I left off with in the last lecture

60
05:30.740 --> 05:34.490
so I'll quickly go through it and show you the changes.

61
05:34.490 --> 05:41.660
First of all, I've created this 'speedMult' up the top here. It was embedded in the code further down, as a local

62
05:41.660 --> 05:42.340
variable

63
05:42.380 --> 05:45.740
but now we're going to use it in a couple of different places.

64
05:45.770 --> 05:51.140
So the goal now is to have 2 different types of animations and 2 different types of speed. We have

65
05:51.140 --> 05:51.960
the walking

66
05:52.160 --> 05:57.830
and we'll also have the running, so that when the obstacle is dropped and the character is fleeing then

67
05:57.830 --> 06:00.530
we want them to be running away.

68
06:00.620 --> 06:02.270
So put that at the top. Next,

69
06:02.300 --> 06:12.140
I've created a function called 'ResetAgent' because I was going to be resetting the agent's speed

70
06:12.140 --> 06:16.850
here-and-there, depending on whether you're fleeing or whether you're just wandering around and therefore it's

71
06:16.850 --> 06:22.080
good to have all of this in the one spot so that it's easy to change and easy to update.

72
06:22.100 --> 06:30.640
Now here you can see in ResetAgent, when it happens it's going to set the speed multiplier to a random value.

73
06:30.800 --> 06:36.160
Before, it was doing this and the Start function so that the agent always had the same speed.

74
06:36.170 --> 06:39.170
Now it's going to change the speed of the agent

75
06:39.380 --> 06:42.430
every time there is a reset.

76
06:42.440 --> 06:48.560
So that's a nice way to make your characters a little bit more randomized in their movements

77
06:48.560 --> 06:53.290
so that they're not always just at the same single speed for the same character.

78
06:53.420 --> 06:57.700
Now next we have the agent's speed being set here.

79
06:57.800 --> 07:05.220
Remember that it's 2 as a default on the characters, multiplied by the speedMult value.

80
07:05.270 --> 07:09.470
So that's just making sure that it's set back to 2

81
07:09.710 --> 07:12.780
and I'm also setting the angularSpeed.

82
07:12.890 --> 07:18.620
If you have a look at the default angularSpeed in the NavMesh Agent on one of your characters, you'll see

83
07:18.620 --> 07:21.320
that it is 120.

84
07:21.320 --> 07:25.880
The angular speed is how quickly the character will turn around.

85
07:25.880 --> 07:31.050
Now what I want is to ramp that up when the character has to flee.

86
07:31.100 --> 07:36.020
So you want them to see something you have to flee from and turn around really quickly, so that

87
07:36.020 --> 07:41.480
means you've got to turn up the angular speed and that also means when they go back to walking that

88
07:41.480 --> 07:43.010
you have to turn it back down again.

89
07:43.010 --> 07:47.600
So I'm putting it back down here to 120. In the ResetAgent,

90
07:47.630 --> 07:56.540
when it does reset, it will set the speedMult value that's in the animation system to speedMult and

91
07:56.540 --> 07:57.440
it will set

92
07:57.440 --> 08:06.890
the walking animation back again and then I'm doing a ResetPath here which clears out the current path

93
08:07.010 --> 08:13.910
that the agent has calculated and basically sets it back to 0 again so it can be ready to generate

94
08:14.270 --> 08:18.670
another path in the NavMesh system.

95
08:18.680 --> 08:24.800
Now, the Start function...what it does is pretty much what we had before.

96
08:24.800 --> 08:28.420
So it grabs all of the goal locations in the world,

97
08:28.550 --> 08:33.920
the agents NavMesh, sets the destination to a random value

98
08:34.140 --> 08:39.410
(this code is exactly the same code as before, I'll just make it bigger so you can see it),  random

99
08:39.410 --> 08:47.150
location out of the goalLocations array, grabs hold of the animator that's on the character and

100
08:47.150 --> 08:55.210
then also sets the walking offset (so that they're not all walking in time with each other)

101
08:55.470 --> 09:02.630
to a value between here and then we run the ResetAgent (which will go ahead and set all these values

102
09:02.780 --> 09:04.580
that are up here for us).

103
09:04.730 --> 09:12.900
Now in the Update, it's the same thing as we had before except we're doing a ResetAgent before we set the

104
09:12.900 --> 09:15.490
next destination.

105
09:15.540 --> 09:22.110
What that will mean is that if we're running to a location because we're fleeing from it, then once we've

106
09:22.110 --> 09:27.480
reached it, we're going to reset back to walking and go and grab another goal location and just keep

107
09:27.480 --> 09:34.350
wandering around. The new things that we'll add for this code to facilitate the fleeing...

108
09:34.410 --> 09:44.510
first of all, we need a float, which will be the 'detectionRadius' and that's the distance from 'where the

109
09:44.510 --> 09:52.100
object is dropped out' to 'how far that any agents nearby have to be in order to hear it'.

110
09:52.100 --> 09:58.280
Now if you have that quite large, then all of the agents in the area will respond to it. If it's smaller,

111
09:58.370 --> 10:03.770
then only the ones that are in the 'within 5 of it' will respond, which is nice because you can have

112
10:03.770 --> 10:09.170
a small fleeing distraction or you can have something that's quite large as a fleeing distraction.

113
10:09.170 --> 10:13.380
Then we also need a float for the 'fleeRadius'

114
10:13.430 --> 10:20.480
and this is how far you want the characters to run away from the object before they forget about it

115
10:20.480 --> 10:24.290
and start going back and looking for a new goal location.

116
10:24.320 --> 10:27.810
So let's set that to 10, in this case.

117
10:28.040 --> 10:34.630
Right, so now with that what we're going to do is create that method that I talked about before

118
10:34.760 --> 10:42.530
(DetectNewObstacle) that is being called from the dropping of the objects in the other clicking script.

119
10:42.530 --> 10:45.630
So the new method will look like this.

120
10:45.650 --> 10:51.830
So it's going to be public because it's being called from elsewhere and we're passing through the position

121
10:51.920 --> 10:55.900
of the obstacle that's being dropped into the scene.

122
10:55.980 --> 11:03.470
We're then going to do a distance test of the 'position of the object' and the 'agent's position'.

123
11:03.470 --> 11:09.480
Now, if this is inside the detection radius, then the agent will react to it.

124
11:09.560 --> 11:15.490
If the agent is going to flee, it needs to determine in which direction it's going to flee which is

125
11:15.490 --> 11:24.290
away from the position of the obstacle and to determine that, it's the position of the agent minus the

126
11:24.290 --> 11:33.620
position of the obstacle. Notice I'm then normalizing it so that I have a length vector of 1 and I'm

127
11:33.620 --> 11:41.360
going to use that as a 1 to multiply it by the fleeRadius, which you can see here.

128
11:41.480 --> 11:51.830
So it's the fleeDirection for a length of fleeRadius, added to the current position of the agent

129
11:51.830 --> 11:56.140
and that's going to be where the new goal is.

130
11:56.190 --> 12:01.860
Now with that new goal information, we can set the destination of the agent

131
12:01.960 --> 12:07.290
but beforehand, we're going to see if we can make it to that location.

132
12:07.290 --> 12:14.180
So you could just have agent.setDestination newgoal

133
12:14.400 --> 12:23.520
but in this case, it's possible that you can calculate a location that is off of the agent's NavMesh

134
12:23.580 --> 12:32.070
altogether or it's not quite accessible and that means that you still want the agent to try and get

135
12:32.070 --> 12:34.530
to it, if possible.

136
12:34.530 --> 12:39.220
So this is a different way of working out the path.

137
12:39.270 --> 12:48.780
What we do is we create a NavMesh path object and this path object can be calculated with

138
12:48.780 --> 12:49.770
CalculatePath.

139
12:49.770 --> 12:57.270
So we create this value for path and we pass it through to calculate path, given the new goal position.

140
12:57.270 --> 13:02.780
Now this path will hold the path that the agent's going to follow to get to the goal

141
13:02.850 --> 13:09.060
but if it can only make it part of the way along or it can't make it at all, you can test the

142
13:09.060 --> 13:11.170
path.status.

143
13:11.370 --> 13:20.700
In this case, if the path.status is not equal to an invalid path, then it will set the destination

144
13:21.180 --> 13:33.270
to the end of the path that it's calculated. So 'path' itself ends up with an array called 'corners' and that

145
13:33.390 --> 13:40.000
is an array of all the waypoints, essentially, that the character's going to follow as it gets from

146
13:40.000 --> 13:42.180
where it is to its goal location.

147
13:42.180 --> 13:49.560
Now if it can't get all the way to the goal location, you'll still have this corners array but it

148
13:49.560 --> 13:55.890
will only take you part of the way, which means if you get the last value in the corners array

149
13:55.890 --> 14:02.640
(which is this one here) then you'll have made it to the end of the path that was calculated, even if you haven't 

150
14:02.640 --> 14:11.940
made it to the exact goal. So you'll get the agent to move at least some way away from where you've placed

151
14:11.940 --> 14:13.140
the obstacle.

152
14:13.410 --> 14:22.140
At that point, we set the animation to running and then we speed up the agent with agent.speed and we turn

153
14:22.140 --> 14:27.210
up the angularSpeed quite high because we want the agent to turn around very quickly and then head

154
14:27.210 --> 14:30.360
off in the complete opposite direction.

155
14:30.780 --> 14:39.710
All right, so save that and then we'll just go back over into Unity. What you'll need to do to test this

156
14:39.720 --> 14:39.880
out

157
14:39.900 --> 14:43.370
(you can see I've removed all the buildings, duplicated my agents)

158
14:43.470 --> 14:49.770
I've got four goals and I've just placed them in a square around the area, so the agents when I press

159
14:49.770 --> 14:55.510
Play are just going to wander around at random between those goals.

160
14:56.820 --> 15:04.380
Now to set up the clicking for the cylinder, we've got our DropCylinder script here. I'm going to just

161
15:04.560 --> 15:08.040
add it to the camera (drag-and-drop it onto the camera).

162
15:08.040 --> 15:15.990
It just needs to be in the scene so that it runs and then that DropCylinder script requires our prefab

163
15:15.990 --> 15:17.750
Cylinder that we created before.

164
15:17.750 --> 15:27.860
So grab that and drop it into there. Now when we press Play, if we drop a cylinder

165
15:28.290 --> 15:35.720
you'll see that the agents flee from it and then they come back because they only flee out as far as

166
15:35.720 --> 15:40.890
they need to go to find their goal location and then they will just start wandering back and looking for

167
15:40.890 --> 15:47.310
other goals. So they're still randomly moving around those 4 different goals which are in these 4

168
15:47.310 --> 15:52.650
corners where they tend to be grouping.

169
15:52.710 --> 16:00.240
Right, so if you want to change this up so that they all react, if we go back into our AIControl, go

170
16:00.240 --> 16:05.880
to the top here and if we change this detectionRadius to 20, that should probably cover

171
16:05.910 --> 16:09.210
everybody in that scene. Save it.

172
16:09.390 --> 16:17.170
Go back and press Play. When we drop this in here, everybody will flee.

173
16:17.240 --> 16:23.990
Now they're still not fleeing very far because you've still only got that radius of 10 (or whatever we had

174
16:23.990 --> 16:28.830
in there) and you can change that if you want to push them right out to the edges of the NavMesh.

175
16:28.910 --> 16:39.200
Now chances are, when you're doing this, there will be the odd agent who will seemingly be ignoring the

176
16:39.380 --> 16:49.040
obstacle and that will be because in your AIControl script just down here, where you're calculating the path,

177
16:49.040 --> 16:59.060
it can be possible if you've got a very, very large fleeRadius to be trying to calculate a new goal that

178
16:59.060 --> 17:07.400
is so far away from the current NavMesh that it's just going to return an invalid path

179
17:07.640 --> 17:11.210
and there's really nothing you can do about it.

180
17:11.240 --> 17:14.060
So you have to live with it.

181
17:14.060 --> 17:21.680
The other alternative is you'd have to manually program as an 'else' part to this here,

182
17:21.770 --> 17:28.070
the agent turning around and going off in the opposite direction, which means you then lose control

183
17:28.070 --> 17:34.150
of the agent and the agent would no longer be navigating via the NavMesh

184
17:34.190 --> 17:36.710
and that could cause you issues.

185
17:36.860 --> 17:40.390
So it's up to you whether you want to do that or not or

186
17:40.460 --> 17:44.350
you're just happy to put up with the fact that that doesn't work.

187
17:44.390 --> 17:52.910
Otherwise, you really do need to make sure that your fleeRadius is going to be inside the NavMesh or at

188
17:52.910 --> 17:59.220
least not ridiculously far outside the NavMesh that it won't calculate a valid path for you.

189
18:00.340 --> 18:05.060
So that is how to get your NPCs to flee from a space.

190
18:05.070 --> 18:12.520
This is using the NavMesh  method but the calculation of the vector to flee along and the new goal location

191
18:12.940 --> 18:18.050
is exactly the same if you want to implement this in your own manual waypoint system.

192
18:18.070 --> 18:25.460
You would just create a new waypoint on the fly and give it that new goal position, with that vector math

193
18:25.540 --> 18:28.000
that's in this particular program.