1
00:00:03,760 --> 00:00:04,110
Oh cool.

2
00:00:04,150 --> 00:00:09,880
Gameplay is now pretty much there, but our game still feels a little bit dull because our enemies and

3
00:00:09,880 --> 00:00:14,360
our player ship just disappear and blink out of existence whenever they get destroyed.

4
00:00:14,380 --> 00:00:18,460
So in this lecture, let's add some explosions with some particle effects.

5
00:00:18,790 --> 00:00:23,620
Now, whenever I'm working with the particle system, I do like to have some kind of reference to work

6
00:00:23,620 --> 00:00:29,230
from be that from looking at other games or past projects or even just drawing something down on paper.

7
00:00:29,470 --> 00:00:35,050
Now, you don't have to follow exactly what I'm doing in this lecture, so feel free to make this explosion

8
00:00:35,050 --> 00:00:36,430
entirely your own.

9
00:00:36,430 --> 00:00:41,050
And rather than setting a challenge for this lecture, I encourage you to pause the video frequently

10
00:00:41,050 --> 00:00:45,940
to play around with this effect rather than just copying the numbers that I'm going to be using.

11
00:00:45,940 --> 00:00:50,470
Now, the first step to creating our particle system is to add it to the hierarchy.

12
00:00:50,470 --> 00:00:56,230
So in our hierarchy, let's right click, go down to effect, and then add a new particle system.

13
00:00:56,470 --> 00:00:59,800
And this currently looks nothing like our end result.

14
00:00:59,800 --> 00:01:05,050
And the first biggest thing that I'm noticing is that we're currently spitting out these fuzzy particle

15
00:01:05,050 --> 00:01:11,440
balls rather than our nice star shaped particles and the changes we need to look at changing the particles

16
00:01:11,440 --> 00:01:12,520
material.

17
00:01:12,790 --> 00:01:16,930
We haven't created this material yet, so let's go ahead and do that first.

18
00:01:16,930 --> 00:01:23,110
If we head into our asset packs and then space shooter, I've imported this explosion styles sprite

19
00:01:23,110 --> 00:01:23,800
sheet.

20
00:01:23,830 --> 00:01:29,080
If you want to follow along and use the same material as me, then this is attached as a resource to

21
00:01:29,080 --> 00:01:29,590
this lecture.

22
00:01:29,590 --> 00:01:31,750
So go ahead and download and import that.

23
00:01:31,750 --> 00:01:37,900
Now once you've got a spreadsheet you want to use, the next step is going to be to make it into a material

24
00:01:37,900 --> 00:01:39,730
that we can apply to our particles.

25
00:01:39,730 --> 00:01:43,030
And we haven't really dealt with materials too much so far.

26
00:01:43,030 --> 00:01:49,420
So to create one, let's right click in our assets folder, go up to create and then down to material.

27
00:01:49,420 --> 00:01:53,230
Let's call this material M underscore explosion.

28
00:01:53,680 --> 00:01:58,840
And whenever we create a material, it will have a shader which tells the unity how to render it.

29
00:01:58,840 --> 00:02:04,180
If we look up in the inspector, we can see under the shader dropdown that these come in as standard

30
00:02:04,180 --> 00:02:04,960
by default.

31
00:02:04,960 --> 00:02:08,289
But since this is a particle system, we're going to want to change this.

32
00:02:08,289 --> 00:02:15,190
So if we click on our dropdown, head down to particles and we're going to use the standard unlit shader

33
00:02:15,550 --> 00:02:21,640
next, we're going to want to change the render mode from opaque down to additive, and that will just

34
00:02:21,640 --> 00:02:24,640
make the particles appear correctly when we start using them.

35
00:02:24,820 --> 00:02:28,870
And now we need to attach our sprite sheet to this material.

36
00:02:28,870 --> 00:02:33,910
So let's grab our sprite sheet and drag it into the little box next to Albedo.

37
00:02:33,910 --> 00:02:38,260
Under the maps, our material is now set up for what we need.

38
00:02:38,260 --> 00:02:41,290
So let's now go into our political system itself.

39
00:02:41,290 --> 00:02:44,290
Scroll right the way to the bottom under renderer.

40
00:02:45,000 --> 00:02:48,720
And we can drag our new material into the material slot.

41
00:02:49,050 --> 00:02:54,390
We can see that this has now changed our particles to be our sprite sheet, but we don't want the entire

42
00:02:54,390 --> 00:02:55,250
sprite sheet here.

43
00:02:55,260 --> 00:03:00,540
We just want to loop through each individual cell within that sprite sheet and we can do that with some

44
00:03:00,540 --> 00:03:02,370
texture sheet animations.

45
00:03:02,370 --> 00:03:05,130
So let's enable our texture sheet animation.

46
00:03:05,400 --> 00:03:09,210
And if we open this up, we're going to keep the mode as grid.

47
00:03:09,240 --> 00:03:16,140
And for the tiles, we're going to set this to three by one because our sprite sheet is three wide and

48
00:03:16,140 --> 00:03:17,070
one tall.

49
00:03:17,760 --> 00:03:22,380
The changing is quite slow though, so let's bump up our cycles to maybe two.

50
00:03:22,380 --> 00:03:27,300
So this one I'll loop through the sprite sheet twice and rather than always starting on exactly the

51
00:03:27,300 --> 00:03:32,960
same particle, what I'm going to do is I'm going to select a random one to start with in the dropdown,

52
00:03:32,970 --> 00:03:38,100
let's pick a random between two constants and start to anywhere from the first to the third.

53
00:03:38,100 --> 00:03:39,840
So between zero and two.

54
00:03:39,900 --> 00:03:44,190
And now we can see we're getting a little bit more variation with that set.

55
00:03:44,190 --> 00:03:49,380
The next thing I want to do is to amend the colors because they're all white for their entire lifetime

56
00:03:49,380 --> 00:03:52,110
and it's not very fiery or exclusionary.

57
00:03:52,110 --> 00:03:56,790
So let's go up to color over lifetime and then open up our color picker.

58
00:03:56,790 --> 00:04:02,370
And I've actually already got a nice explosion type effect that I like to use, but you can go ahead

59
00:04:02,370 --> 00:04:04,620
and create whatever kind of explosion you like.

60
00:04:04,620 --> 00:04:10,380
You might have purple explosions or green explosions or really anything that you want to show you how

61
00:04:10,380 --> 00:04:11,640
this one is set up.

62
00:04:11,640 --> 00:04:17,070
Along the top, we have various levels of alpha and this will decrease the alpha over time as it goes

63
00:04:17,070 --> 00:04:22,620
through its fire and smoke cycles until eventually petering out entirely along the colors.

64
00:04:22,620 --> 00:04:28,590
We start off with a nice hot yellow color before we go into oranges, orange reds, and then we go into

65
00:04:28,590 --> 00:04:30,570
a reddish gray color for our smoke.

66
00:04:30,840 --> 00:04:36,240
This is now starting to look a bit more like fire, but it's very much the wrong shape and going in

67
00:04:36,240 --> 00:04:37,380
the wrong direction.

68
00:04:37,380 --> 00:04:39,870
So let's look at our shape next.

69
00:04:40,380 --> 00:04:45,480
It's currently set up to be a cone, but we want it to explode out in a circle.

70
00:04:45,480 --> 00:04:48,270
So for our shape, let's click on circle.

71
00:04:48,270 --> 00:04:50,850
And this doesn't really do what we want.

72
00:04:50,880 --> 00:04:54,540
It's now spreading out on a line, but this is a circle.

73
00:04:54,540 --> 00:04:58,830
It's just being rendered in three D and the rotation is incorrect.

74
00:04:58,830 --> 00:05:04,860
So at the very top of our political system, let's zero out the rotation and now we have it generating

75
00:05:04,860 --> 00:05:05,760
in a circle.

76
00:05:05,760 --> 00:05:10,830
While we're here, let's also zero out the transform because that really should be zeroed if we're going

77
00:05:10,830 --> 00:05:12,420
to prefab this later on.

78
00:05:12,450 --> 00:05:18,510
Next, let's try and constrain this explosion so it doesn't spread out quite so far for this.

79
00:05:18,510 --> 00:05:24,420
The first thing I want to do is change the start lifetime and explosions are quite quick, so let's

80
00:05:24,420 --> 00:05:32,130
do a random between two constants and have our particles die out between 0.1 and 0.5.

81
00:05:32,130 --> 00:05:37,200
And with that small change, we can now get a better sense of that hot fire from the explosion and then

82
00:05:37,200 --> 00:05:38,760
the smoke spreading out.

83
00:05:38,850 --> 00:05:41,970
Our particles are all oriented in exactly the same way, though.

84
00:05:41,970 --> 00:05:44,220
So now let's look at giving it some rotation.

85
00:05:44,220 --> 00:05:50,820
So under our start rotation, let's again go with a random between two constants and go from zero degrees

86
00:05:50,820 --> 00:05:53,040
to 359 degrees.

87
00:05:53,040 --> 00:05:54,360
So a full circle.

88
00:05:54,450 --> 00:06:00,090
And we're also going to come down to rotation over lifetime and give it an angular velocity of around

89
00:06:00,090 --> 00:06:01,830
180 degrees.

90
00:06:01,830 --> 00:06:06,630
This allows our particles not only to be instantiated at a random angle, but also it gives it some

91
00:06:06,630 --> 00:06:08,340
rotation as they travel.

92
00:06:08,400 --> 00:06:13,530
So this is looking okay, but it is very sporadic and not very explosion yet.

93
00:06:13,530 --> 00:06:19,440
So now let's try and amp up the number of particles we've got and really condense them into a quick

94
00:06:19,440 --> 00:06:20,280
explosion.

95
00:06:21,060 --> 00:06:27,990
Up in our emissions, let's increase the rate over lifetime to say about 100, which is definitely giving

96
00:06:27,990 --> 00:06:32,190
us a more sparkly effect and to tighten up the radius a little bit.

97
00:06:32,220 --> 00:06:37,290
Let's go down to our shape and change the radius of our circle to maybe around 0.5.

98
00:06:37,650 --> 00:06:42,450
This has brought us a good step closer, and the next thing I'm noticing compared to our reference is

99
00:06:42,450 --> 00:06:45,360
that our particles are always exactly the same size.

100
00:06:45,360 --> 00:06:48,930
So down in size over a lifetime, let's enable that.

101
00:06:48,930 --> 00:06:54,810
And in our curve, let's flip this line so we start off big and eventually peter out to zero.

102
00:06:55,050 --> 00:06:57,930
This has drastically changed the look of our political system.

103
00:06:57,930 --> 00:07:02,130
So we now have a hot fire in the middle and these wispy smoke pieces around the edge.

104
00:07:02,460 --> 00:07:07,890
But to get it looking a little bit better, let's change the start speed so all of our particles aren't

105
00:07:07,890 --> 00:07:09,870
moving at exactly the same rate.

106
00:07:10,080 --> 00:07:14,400
Doing this will allow for a little bit of overlap in our speed and we'll have some bigger pieces of

107
00:07:14,400 --> 00:07:16,440
smoke fly out to start with.

108
00:07:16,440 --> 00:07:22,410
Let's try a random between two constants and maybe try somewhere between zero and two.

109
00:07:22,680 --> 00:07:26,760
And again, this has made quite a big difference, but it isn't quite what we want.

110
00:07:26,760 --> 00:07:32,070
And this is because when we do a random between two constants, the actual random numbers that get generated

111
00:07:32,070 --> 00:07:35,250
are evenly distributed between the upper and lower bound.

112
00:07:35,250 --> 00:07:39,180
But we really want more of our particles to be on the quicker end of things.

113
00:07:39,180 --> 00:07:41,190
To do this we can use a curve.

114
00:07:41,190 --> 00:07:42,600
So let's select that.

115
00:07:42,600 --> 00:07:47,670
And for this curve I'm going to want a nice ramp between zero and two.

116
00:07:47,700 --> 00:07:53,340
This means that we'll have more particles being created with a speed closer to two, and as we head

117
00:07:53,340 --> 00:07:57,240
down the curve, we'll end up with less particles at the slower speed.

118
00:07:57,270 --> 00:08:02,520
The final step now is going to be to change the duration of our particle system, because if we turn

119
00:08:02,520 --> 00:08:08,700
off looping and then restart our particle, it's existing for a very long time and explosion should

120
00:08:08,700 --> 00:08:10,500
be very short and sharp.

121
00:08:10,500 --> 00:08:16,560
So with looping turned off, let's set our duration down something nice and snappy, maybe around 0.2.

122
00:08:16,920 --> 00:08:21,090
And if we restart our explosion now, we get a nice short, sharp explosion.

123
00:08:21,630 --> 00:08:25,860
And with that final change, I'm pretty happy with the end result for this particle system.

124
00:08:25,860 --> 00:08:30,840
For now, it's pretty close to our initial reference and it looks quite nice.

125
00:08:31,080 --> 00:08:34,919
So with that set, let's go ahead and prefab our political system.

126
00:08:35,490 --> 00:08:39,480
And rather than calling it the particle system, let's call this the explosion effect.

127
00:08:41,309 --> 00:08:43,710
And we can delete that out of our hierarchy.

128
00:08:44,280 --> 00:08:48,450
Next, we want to instantiate this explosion whenever one of the ships is hit.

129
00:08:48,750 --> 00:08:54,000
So if we take a look at our script and think about where we could do this, the damage is being taken

130
00:08:54,000 --> 00:08:55,410
in our health script.

131
00:08:55,410 --> 00:08:59,550
So it'd be a good idea to have our health and our particle system linked in some way.

132
00:08:59,880 --> 00:09:03,360
Now, for this game, we're only going to have the one explosion effect.

133
00:09:03,360 --> 00:09:07,680
So I'm actually going to instantiate it directly into this health script.

134
00:09:07,680 --> 00:09:13,380
But if you decide to expand on this game later on and add more effect in other parts of your game,

135
00:09:13,380 --> 00:09:18,840
then it may be worth creating a brand new script that holds all of these special effects and then link

136
00:09:18,840 --> 00:09:21,330
that to the various scripts that need to call them.

137
00:09:21,660 --> 00:09:26,490
So in our Health script, let's create a serialized field for our particle system, and we're going

138
00:09:26,490 --> 00:09:28,500
to call this the hit effect.

139
00:09:29,280 --> 00:09:33,040
And we want to instantiate this particle effect whenever an object takes damage.

140
00:09:33,060 --> 00:09:38,610
Now, we could place this in our take damage method, but rather than muddying that up, let's create

141
00:09:38,610 --> 00:09:41,670
a new method called play hit effect.

142
00:09:43,380 --> 00:09:50,430
In here, we're going to say that if our hit effect does not equal null, so if we've attached a hit

143
00:09:50,430 --> 00:09:55,710
effect that we want to play, then we want to instantiate that effect so that we can destroy it later.

144
00:09:55,710 --> 00:09:57,720
Let's store this in a temporary variable.

145
00:09:57,720 --> 00:10:03,060
So for this one, we're going to need a particle system called instance, and then we can go ahead and

146
00:10:03,060 --> 00:10:08,520
instantiate our hit effect at the transform dot position.

147
00:10:08,520 --> 00:10:13,680
So at the position of the thing being hit and because we've included a position, we need to include

148
00:10:13,680 --> 00:10:17,730
a rotation of Quaternions identity for no rotation.

149
00:10:18,060 --> 00:10:26,010
Then to destroy the particles, we can say destroy the instance game object because the instance is

150
00:10:26,010 --> 00:10:32,310
currently just the particle system attached to a game object and for the duration we can get the duration

151
00:10:32,310 --> 00:10:39,420
of the particle system by saying instance dot main dot duration, and that will be the duration that

152
00:10:39,420 --> 00:10:41,370
the particles are being created.

153
00:10:41,370 --> 00:10:49,410
But we also want to include the lifetime of the particles so we can add the instance dot main dot start,

154
00:10:49,410 --> 00:10:55,290
lifetime dot constant max and you'll see a few different options here.

155
00:10:55,290 --> 00:10:58,890
And the one you select depends on how you set up the particle system.

156
00:10:58,890 --> 00:11:03,810
So we've got a minimum and a maximum value, but if you've set up a curve, you'll have to worry about

157
00:11:03,810 --> 00:11:05,760
animation curves and things like that.

158
00:11:05,970 --> 00:11:08,850
For this one, though, constant Max will do the job.

159
00:11:09,120 --> 00:11:13,890
Finally, we need to call this method from somewhere, so let's take our hit effect.

160
00:11:13,890 --> 00:11:17,670
And if we've dealt damage, we're going to take damage.

161
00:11:17,670 --> 00:11:23,430
We're going to play our hit effect and then we're going to tell our damage data to processes hit method.

162
00:11:23,430 --> 00:11:26,610
So let's save this up and test our immunity.

163
00:11:27,670 --> 00:11:31,910
Let's first of all, link our explosion to both our enemy and our player.

164
00:11:31,930 --> 00:11:34,520
So let's click both of those in our asset folder.

165
00:11:34,540 --> 00:11:38,950
Scroll down to our health script and then drag over our explosion effect.

166
00:11:39,160 --> 00:11:40,990
And then we can go ahead and hit play.

167
00:11:41,230 --> 00:11:43,870
And our particle systems are now being created.

168
00:11:44,230 --> 00:11:48,060
But if we notice, the particles are currently behind our ships.

169
00:11:48,070 --> 00:11:51,250
So have a quick think about how we might go and fix this.

170
00:11:52,860 --> 00:11:53,250
Okay.

171
00:11:53,250 --> 00:11:54,530
I'm not going to give you too long.

172
00:11:54,540 --> 00:12:00,470
If we jump into our explosion effect, scroll right down to the bottom where we have our renderer,

173
00:12:00,510 --> 00:12:02,970
because this is a problem with the sorting order.

174
00:12:03,030 --> 00:12:08,130
If you recall, we're currently working on a rough system of having our projectiles on layers zero,

175
00:12:08,340 --> 00:12:10,260
our ships on layer one.

176
00:12:10,260 --> 00:12:14,760
So for our particles, let's change our sort order in layer two to.

177
00:12:15,420 --> 00:12:18,180
If we save our prefab and then test again.

178
00:12:19,000 --> 00:12:24,220
We can see that the particles are now appearing on top of our ships, and when ships get destroyed,

179
00:12:24,220 --> 00:12:26,680
they go out in a bit more of a blaze of glory.

180
00:12:26,860 --> 00:12:30,430
So congratulations on creating your explosion particles.

181
00:12:30,430 --> 00:12:35,020
And in the next lecture, we're going to juice our game even more by adding some camera shake when the

182
00:12:35,020 --> 00:12:36,240
player gets hit.

183
00:12:36,250 --> 00:12:37,810
So I'll see you there.

