WEBVTT

0
00:11.050 --> 00:18.080
In this third part of the flocking workshop, we're going to create fish that can detect obstacles in their

1
00:18.080 --> 00:21.500
environment and turn away from them.

2
00:22.010 --> 00:29.300
Now to begin with, what I've created here is exactly the same program that you've had before from the previous

3
00:29.300 --> 00:37.580
one but I've added in a ground plane with some sand texture on it and this pillar, which will be our obstacle.

4
00:37.580 --> 00:40.640
You can add anything into the scene as an obstacle.

5
00:40.730 --> 00:46.420
This particular pillar I got from the Asset Store here, it's a free one, it's called Stylize Sand Pillar,

6
00:46.490 --> 00:55.670
if you want to use it. The first thing that you'll need to do is to add a collider to your object, so select

7
00:55.760 --> 01:01.010
your object and go over to the Inspector...[Add Component]...Box Collider.

8
01:01.340 --> 01:06.560
So we're going to test if the fish is about to collide with these objects.

9
01:06.560 --> 01:13.460
It's a bit different to the bounding box testers that we were doing before. To thoroughly test this,

10
01:13.460 --> 01:19.090
you'll also need to make sure that the fish are going to swim through your object.

11
01:19.160 --> 01:24.510
So to begin with, we'll go to the FlockManager and the FlockManager

12
01:24.530 --> 01:27.380
in my case here is just sitting above this pillar.

13
01:27.380 --> 01:35.600
I'm going to move it down so that it's inside of that pillar and the fish need to try and swim through

14
01:35.600 --> 01:41.900
it in order to get to the FlockManager's position as their goal position and to make sure that doesn't

15
01:41.900 --> 01:43.650
change while we're testing,

16
01:43.760 --> 01:48.670
you'll need to go to your FlockManager code and in the random part in this Update,

17
01:48.680 --> 01:56.600
let's just comment out our setting of the random goal location and just use the goal location that is

18
01:56.600 --> 01:59.570
set to the FlockManager.

19
01:59.820 --> 02:06.360
So if you just save that, go back and Play to test that the fish actually swim through your object, you

20
02:06.360 --> 02:13.080
can see them now, starting to swim towards the goal of the FlockManager and going straight through the

21
02:13.110 --> 02:13.810
collider.

22
02:14.010 --> 02:21.930
What's really important at this point is because we put a Box Collider on our pillar, we don't want a physics

23
02:21.930 --> 02:25.890
collider on the fish because we're not going to do it that way.

24
02:25.890 --> 02:28.600
So make sure that your fish don't have a collider.

25
02:28.860 --> 02:35.730
If you need your flocking objects to have a collider on them, you will have to use different physics

26
02:35.730 --> 02:43.820
layers to make sure that the fish do not collide with your objects in the scene.

27
02:43.830 --> 02:48.840
Now the way we're going to achieve the avoidance of obstacles with a physics collider on them is to do

28
02:48.840 --> 02:53.720
a physics raycast from the position of each fish along its forward vector.

29
02:54.120 --> 02:59.690
We then test if this ray has collided with something and if it has, we turn the fish away.

30
03:02.610 --> 03:08.970
We'll integrate this functionality into the Flock script, so open up your Flock script. In the Update,

31
03:09.090 --> 03:14.690
where we're determining if we're still within our bounding box (which was here),

32
03:14.790 --> 03:21.890
we'll add a second test which is our physics raycast and it's going to be an 'OR' in this if-statement.

33
03:21.900 --> 03:30.510
So if we're outside of the box OR we've hit something with our raycast. To perform a raycast, we

34
03:30.510 --> 03:40.380
need to create our RaycastHit item 'hit' and then we use Physics.Raycast, the position where

35
03:40.380 --> 03:47.010
the ray is starting from and then the direction of the ray (in this case, it's going to be the forward

36
03:47.010 --> 03:52.760
direction of your fish, multiplied by how long you want that ray to be).

37
03:52.760 --> 03:59.640
Now if you make this quite long, the fish will think it's colliding with something when it's quite a

38
03:59.640 --> 04:01.050
long way away from it.

39
04:01.140 --> 04:04.650
If you make it short, then the fish will get right up close to it before it

40
04:04.650 --> 04:06.570
decides it needs to turn.

41
04:07.020 --> 04:12.340
So this one here (the forward vector itself) is only going to be a length of 1.

42
04:12.420 --> 04:18.840
So it's already normalized and then multiplying it by...it's up to you, it's how much you want the fish

43
04:18.840 --> 04:26.220
to react based on how close they are and then we're grabbing hold of the hit point of our raycast. Now

44
04:26.220 --> 04:31.410
there is another raycast function that you can run that just tells you that you have hit something

45
04:31.650 --> 04:33.110
and doesn't return where you've hit

46
04:33.180 --> 04:36.400
but we'll be using this hit point a little bit later.

47
04:36.550 --> 04:38.490
Now that's our 'OR' part

48
04:38.520 --> 04:40.080
and then we go to

49
04:40.110 --> 04:43.470
'turning = true' if our raycast has hit something.

50
04:43.500 --> 04:49.530
Now, what I'm just going to do under here is put in a little debug line (and you might like to do this

51
04:49.530 --> 04:54.630
too) because when you're trying to debug A.I. it's an absolute headache

52
04:54.690 --> 05:00.360
if you're sitting there looking at the screen and trying to figure out what your characters are doing and

53
05:00.360 --> 05:07.770
whether they're doing the right thing. In this case, we want to know WHEN the fish is turning and we want

54
05:07.770 --> 05:09.430
to know WHY the fish is turning.

55
05:09.630 --> 05:20.400
So I'm going to draw the actual ray that the fish is projecting out when it's in turning mode and that's

56
05:20.400 --> 05:21.540
going to help us a lot.

57
05:21.540 --> 05:27.070
So just save that once you've done it and then we'll go back into Unity and press Play.

58
05:27.180 --> 05:27.490
Ok.

59
05:27.510 --> 05:30.390
Now you can see a lot of red lines going on. These are

60
05:30.450 --> 05:31.150
your raycasts

61
05:31.160 --> 05:39.390
of fish when they're turning, so if they're facing towards the pillar, then their raycast when

62
05:39.390 --> 05:44.880
it hits the pillar, it will show up as red and it will indicate that they're going into turning mode

63
05:44.910 --> 05:45.600
as well.

64
05:45.600 --> 05:49.950
Now you can see, in this case, they're circling around it, they've gotten into this sort of rhythm where they're

65
05:49.950 --> 05:51.690
not turning into here.

66
05:51.750 --> 05:57.980
We could go into the FlockManager and make their rotational speed stronger, so they will turn.

67
05:57.980 --> 06:04.650
You can see there. Now they're all turning into that pillar but they don't appear to be turning away from

68
06:04.710 --> 06:06.320
the pillar.

69
06:06.370 --> 06:12.450
The reason for that is the way we've coded where they're going to turn TO.

70
06:12.700 --> 06:14.060
If you remember

71
06:14.410 --> 06:24.100
(let's go back into the code) when we're turning, we're going to turn towards the center of the

72
06:24.140 --> 06:27.850
FlockManager and the center of the FlockManager is also the goal

73
06:27.960 --> 06:33.560
and we've currently got it sitting right in the middle of our pillar.

74
06:33.570 --> 06:41.540
So this isn't going to work, in this case because every time our ray collides

75
06:41.550 --> 06:43.270
with something, it's going to say

76
06:43.410 --> 06:43.860
"Ok,

77
06:43.920 --> 06:47.700
I have to turn around but I have to turn around towards the center of my manager"

78
06:47.730 --> 06:53.120
and therefore the thing we're trying to turn away is conflicting with where we're going to turn to.

79
06:53.220 --> 06:59.430
So we have to come up with a different location and a different goal for our fish 'on turning'

80
06:59.730 --> 07:03.650
and for that, we'll take a little bit of inspiration from a game of billiards.

81
07:03.720 --> 07:08.600
When the fish's raycast hits the obstacle, it does so at a particular angle.

82
07:08.610 --> 07:13.860
Now if the fish were a ball, you could predict how it would bounce off the surface of the obstacle just

83
07:13.860 --> 07:19.950
like a billiard ball bounces off the side of a table. From physics, we know that the angle of incidence (a)

84
07:20.100 --> 07:22.910
equals the angle of reflection (b).

85
07:23.010 --> 07:29.850
Therefore, we can calculate a new goal location for our fish using the vector traveling away from the

86
07:29.940 --> 07:31.190
obstacle.

87
07:31.200 --> 07:36.600
In other words, we want to align the fish's forward vector with the reflection vector. To calculate the

88
07:36.600 --> 07:42.660
reflection vector, we make use of the hit point that the raycast generates and the 'reflect' method available

89
07:42.660 --> 07:49.470
in Unity. The reflect method requires the incoming vector and the normal of the surface that was hit and

90
07:49.470 --> 07:55.170
returns the outgoing vector. To put this into our code, we go back into the Flock.cs

91
07:55.180 --> 08:03.280
script, in the Update method and what we're going to do is recalculate this direction based on what's

92
08:03.280 --> 08:03.700
happening.

93
08:03.700 --> 08:09.910
So if you've hit the side of the container (you've come outside of the bounding box) then we want the direction

94
08:09.910 --> 08:12.130
to be back to the center of that box.

95
08:12.130 --> 08:17.460
Otherwise if you've hit something, then you want to turn around according to the reflection vector.

96
08:17.470 --> 08:20.850
So let's begin up here. After the RaycastHit,

97
08:20.860 --> 08:27.280
notice I've also just added in this new RaycastHit here, so that I don't get an undefined hit when

98
08:27.280 --> 08:28.450
I go to use it later on.

99
08:28.450 --> 08:32.630
So we set it to at least something and initialize it. Under here,

100
08:32.650 --> 08:37.350
I'm going to move this 'direction' from down in 'turning'. Let's take that out of there

101
08:38.280 --> 08:40.980
and put it up here.

102
08:41.160 --> 08:47.130
Now you don't have to set it to anything here and you could set it to 0 because it's going to be

103
08:47.160 --> 08:50.040
defined in the next two if-statements.

104
08:50.040 --> 08:55.940
So after that (let me put a space to make it clearer) we'll do 2 'if' tests.

105
08:55.950 --> 08:58.010
We won't use this 'OR' any more.

106
08:58.010 --> 09:04.780
So we'll get rid of the 'OR', we'll make that a self-contained if-statement.

107
09:06.040 --> 09:14.420
So if the position is outside the bounding box, then we're going to put 'turning = true'

108
09:14.600 --> 09:19.010
and at this point is where we want direction to equal THAT.

109
09:19.010 --> 09:24.530
Now it's already set to that, so if it's set to that you don't need to set it to that again. If you set

110
09:24.530 --> 09:29.630
that to 0 up there, then you could take THIS, put it down here

111
09:30.050 --> 09:36.020
and then just set this to Vector3.zero;

112
09:36.440 --> 09:39.600
Now the next if-statement will be an 'else if'

113
09:42.410 --> 09:53.540
on the physics raycast and in this case, if it has hit something, then turning again is true, we'll remove our

114
09:53.570 --> 10:02.620
debug line because we don't need that anymore and instead 'direction = Vector3.Reflect'.

115
10:02.630 --> 10:10.820
This is the Reflect method in Unity that we can use and the Reflect happens given an incoming vector (which

116
10:10.820 --> 10:16.640
is our forward vector on our fish) and the normal to the hit (which will be hit.normal).

117
10:16.670 --> 10:22.400
So the RaycastHit contains all sorts of information about the position that was hit,

118
10:22.430 --> 10:27.550
it's normal and a whole bunch of other stuff that you can look up in the Unity manual.

119
10:27.560 --> 10:33.650
This will now create 'direction' to be our reflection vector which will get used down here

120
10:33.860 --> 10:40.360
IF we are turning so direction's still down here, we haven't made any changes to that whatsoever.

121
10:40.520 --> 10:45.110
So save that and now we can go and see what we've got.

122
10:45.110 --> 10:51.190
Press Play and we will now find that our fish do not swim through the pillar.

123
10:51.350 --> 10:59.030
They might cut the edge and they might have parts of them that go through the pillar because remember

124
10:59.030 --> 11:03.710
we're doing a raycast, so that's a 'single line' out to check whether we've hit something. We're not checking

125
11:03.710 --> 11:06.890
for their entire body having hit something.

126
11:07.310 --> 11:12.350
You could put a collider on it and have the fish turn around when it hits. You'd have to make

127
11:12.350 --> 11:19.240
that collider a little bit longer than the fish, so it knew when it hit something and then start to turn

128
11:19.290 --> 11:24.280
but this is a very cheap way (processor-wise) to do it

129
11:24.410 --> 11:29.540
and you can see here, now that we've got these fish (and if I go back to my flock manager and just increase

130
11:29.540 --> 11:33.380
their rotation speed to get them getting closer to each other)

131
11:33.380 --> 11:35.370
they are avoiding the column.

132
11:35.390 --> 11:44.450
Let's go back to the FlockManager code and put back our random locations for our goalPos in the Update

133
11:44.990 --> 11:50.880
that we took out before. Let's save that and now we can also add in another flock.

134
11:50.900 --> 11:57.590
So what I'll do is grab that FlockManager, we'll move it outside the column and then do a Ctrl-D

135
11:57.590 --> 12:01.550
to duplicate it and might just move it over there

136
12:01.730 --> 12:05.450
and the prefabs, in this case, I'll make the pink fish.

137
12:05.760 --> 12:07.000
So the pink fish

138
12:07.100 --> 12:13.410
you should already have included and it will have the Flock script attached to it.

139
12:13.640 --> 12:19.430
Ok, so let's press Play and now we have 2 separate flocks of fish

140
12:22.150 --> 12:30.740
and you can change the behaviors of each flock just by setting their Rotation Speed, their Neighbour Distance, their

141
12:30.740 --> 12:33.400
Maximum Speed...from here,

142
12:33.440 --> 12:35.270
the possibilities are pretty much endless.

143
12:35.270 --> 12:39.390
You can have a really nice aquarium set up with this flocking

144
12:39.560 --> 12:45.560
and you could also replace the fish with birds if you wanted something in the sea as well.

145
12:45.600 --> 12:51.640
So I hope you enjoyed that because flocking is really my favorite A.I. algorithm

146
12:51.800 --> 12:57.050
and I just find it absolutely mesmerizing, the results you get from such simple rules.