1
00:00:00,000 --> 00:00:03,960
Welcome back my fellow game developers and this video,

2
00:00:03,960 --> 00:00:06,080
when we were as the game,

3
00:00:06,075 --> 00:00:09,105
when we maximize that we go,

4
00:00:09,105 --> 00:00:10,905
we run the game.

5
00:00:10,905 --> 00:00:12,755
When we walk around,

6
00:00:12,750 --> 00:00:14,570
when we hit one of the parts,

7
00:00:14,565 --> 00:00:18,345
you can see that it gets destroyed and they slowly,

8
00:00:18,345 --> 00:00:20,965
slowly fade out,

9
00:00:20,969 --> 00:00:23,059
and then they disappear from the hierarchy.

10
00:00:23,055 --> 00:00:25,655
So let's not waste any more time.

11
00:00:25,650 --> 00:00:28,180
And let's get started.

12
00:00:28,180 --> 00:00:29,840
Oh, okay.

13
00:00:29,840 --> 00:00:32,590
So now it's time to finally

14
00:00:32,585 --> 00:00:34,075
finish off this and

15
00:00:34,070 --> 00:00:36,460
destroy the broken pieces on the ground.

16
00:00:36,455 --> 00:00:38,665
How are we going to do this?

17
00:00:38,660 --> 00:00:41,590
Well, we are going to combine a couple of things that we

18
00:00:41,585 --> 00:00:45,175
already know and maybe one method that we don't.

19
00:00:45,170 --> 00:00:46,670
So let's get started.

20
00:00:46,670 --> 00:00:48,760
The first thing I'm going to do is

21
00:00:48,755 --> 00:00:50,975
I'm going to go into my scripts.

22
00:00:50,975 --> 00:00:53,305
And in here, in the broken part,

23
00:00:53,300 --> 00:00:57,230
I'm going to first of all create a timer here,

24
00:00:57,230 --> 00:00:59,600
which is going to be a serialized field.

25
00:00:59,600 --> 00:01:02,530
And it's going to be a float with

26
00:01:02,525 --> 00:01:05,455
a lifetime called lifetime.

27
00:01:05,450 --> 00:01:09,050
And let's just set it at around three seconds.

28
00:01:09,049 --> 00:01:10,039
Okay?

29
00:01:10,040 --> 00:01:11,930
Maybe even more, maybe five,

30
00:01:11,930 --> 00:01:13,570
Let's set it as five.

31
00:01:13,565 --> 00:01:16,075
And then after we instantiate,

32
00:01:16,070 --> 00:01:18,590
after we start moving and the position

33
00:01:18,590 --> 00:01:21,430
and be wild or after all,

34
00:01:21,425 --> 00:01:24,935
show while we are learning the direction,

35
00:01:24,935 --> 00:01:26,795
we want to go ahead and

36
00:01:26,795 --> 00:01:29,485
go to the lifetime and make sure that every

37
00:01:29,480 --> 00:01:34,340
single frame we are removing the time, that delta time.

38
00:01:34,340 --> 00:01:36,040
Okay, great.

39
00:01:36,035 --> 00:01:41,345
Now, what happens when the lifetime, where is it?

40
00:01:41,345 --> 00:01:47,585
When the life time is less than or equal to 0,

41
00:01:47,585 --> 00:01:52,465
we are going to destroy the game object.

42
00:01:52,460 --> 00:01:55,820
Let's check this out and see if it works.

43
00:01:55,820 --> 00:02:00,170
So I'm going to go into again, let me save that.

44
00:02:00,170 --> 00:02:02,870
Yes, we did run the game.

45
00:02:02,870 --> 00:02:07,120
Had the broken pieces, 1, 2, 3.

46
00:02:07,115 --> 00:02:09,215
Okay. So it's around five.

47
00:02:09,215 --> 00:02:09,965
I guess.

48
00:02:09,965 --> 00:02:12,905
I think that was five anyways.

49
00:02:12,905 --> 00:02:15,565
But obviously we don't want to destroy this.

50
00:02:15,560 --> 00:02:18,380
We want to slowly tear it apart,

51
00:02:18,380 --> 00:02:22,100
so we want to make sure that it doesn't just explode.

52
00:02:22,100 --> 00:02:26,380
We want to slowly deteriorate with time.

53
00:02:26,375 --> 00:02:27,595
So to do that,

54
00:02:27,590 --> 00:02:31,370
we are going to use something called the move towards.

55
00:02:31,370 --> 00:02:33,310
But before we delve into that,

56
00:02:33,305 --> 00:02:34,805
I want to show you something.

57
00:02:34,805 --> 00:02:37,225
We are going to be accessing the colors,

58
00:02:37,220 --> 00:02:38,410
so let's use the pot.

59
00:02:38,405 --> 00:02:40,585
Okay, So on the broken pieces we

60
00:02:40,580 --> 00:02:43,300
have the same breakable pot.

61
00:02:43,295 --> 00:02:45,505
Actually know what, let me just add one of

62
00:02:45,500 --> 00:02:48,640
those broken pieces in here and demonstrate on them.

63
00:02:48,635 --> 00:02:51,115
So you can see we have the sprite renderer,

64
00:02:51,110 --> 00:02:53,170
we have the lifetime and everything.

65
00:02:53,165 --> 00:02:54,905
If I access the color,

66
00:02:54,905 --> 00:02:56,485
I have these three colors.

67
00:02:56,480 --> 00:02:58,100
So the R, You can see that I

68
00:02:58,100 --> 00:03:00,460
can change the color if I zoom in,

69
00:03:00,455 --> 00:03:02,485
you can clearly see how ugly

70
00:03:02,480 --> 00:03:05,210
this pixel art is. No, I'm kidding.

71
00:03:05,210 --> 00:03:06,200
It's amazing.

72
00:03:06,200 --> 00:03:08,710
And then we have the red.

73
00:03:08,705 --> 00:03:10,745
Finally we have the yellow,

74
00:03:10,745 --> 00:03:12,715
and we have also the fate,

75
00:03:12,710 --> 00:03:14,950
and this is what we want to access.

76
00:03:14,945 --> 00:03:17,275
Now, as I always say,

77
00:03:17,270 --> 00:03:19,700
there are easier ways of doing this,

78
00:03:19,700 --> 00:03:21,610
but I want to test everything.

79
00:03:21,605 --> 00:03:25,115
I want to explore with you as much as possible.

80
00:03:25,115 --> 00:03:27,325
So I'm going to delete this.

81
00:03:27,320 --> 00:03:29,450
I'm going to go back into Unity,

82
00:03:29,450 --> 00:03:32,570
and I'm going to access the sprite renderer.

83
00:03:32,570 --> 00:03:34,150
So I'm going to,

84
00:03:34,145 --> 00:03:37,315
first of all create a reference in here.

85
00:03:37,310 --> 00:03:40,150
And it's going to be a serialized on, you know what?

86
00:03:40,145 --> 00:03:42,445
It doesn't need to be a serialized field.

87
00:03:42,440 --> 00:03:47,990
I'm just going to get access to the right, a renderer.

88
00:03:47,990 --> 00:03:52,150
And I'm going to call the part sprite.

89
00:03:52,145 --> 00:03:54,215
And it's going to OK.

90
00:03:54,215 --> 00:03:56,225
It's going to be private.

91
00:03:56,225 --> 00:03:58,175
I forgot to make it private

92
00:03:58,175 --> 00:04:01,445
because we don't need to see it and start.

93
00:04:01,445 --> 00:04:05,575
I'm going to make sure that the part Sprite is equal

94
00:04:05,570 --> 00:04:10,480
to get component sprite render.

95
00:04:10,475 --> 00:04:11,605
Okay, there we go.

96
00:04:11,600 --> 00:04:14,290
So now we have a reference to the part Sprite.

97
00:04:14,285 --> 00:04:16,015
And in here I'm going to access

98
00:04:16,010 --> 00:04:18,260
the part Sprite dot color.

99
00:04:18,260 --> 00:04:19,820
Okay, So you notice that on

100
00:04:19,820 --> 00:04:22,060
the sprite renderer we have the color.

101
00:04:22,055 --> 00:04:24,185
So we're accessing the color in here.

102
00:04:24,185 --> 00:04:27,635
And we're saying that it's a new color.

103
00:04:27,635 --> 00:04:31,075
And what shall this color B. I'm going to open up

104
00:04:31,070 --> 00:04:33,130
the bracket right here and

105
00:04:33,125 --> 00:04:35,485
I'm going to hit Enter and start typing in.

106
00:04:35,480 --> 00:04:38,300
So we can see right here, where is it?

107
00:04:38,300 --> 00:04:40,250
Where is the new color?

108
00:04:40,250 --> 00:04:42,530
Okay, but anyways, we have to give it the

109
00:04:42,530 --> 00:04:45,190
red, blue, and green.

110
00:04:45,185 --> 00:04:47,065
And as you saw, the red,

111
00:04:47,060 --> 00:04:49,490
blue and green are not going to change.

112
00:04:49,490 --> 00:04:52,430
So the first three will be extremely easy.

113
00:04:52,430 --> 00:04:56,500
It will be the parts bright color dot r,

114
00:04:56,495 --> 00:04:58,955
comma duplicate, duplicate,

115
00:04:58,955 --> 00:05:08,275
red, green, red, green, and blue.

116
00:05:08,270 --> 00:05:10,970
And there we go. If we hover over right here,

117
00:05:10,970 --> 00:05:14,480
we can see that these are three colors that we want.

118
00:05:14,480 --> 00:05:16,820
And finally, we want to change the a.

119
00:05:16,820 --> 00:05:18,920
So the way we access this is through

120
00:05:18,920 --> 00:05:21,620
the part dot color dot a.

121
00:05:21,620 --> 00:05:24,740
But obviously this time we want to change this.

122
00:05:24,740 --> 00:05:30,400
We want this to move from what it currently is to 0.

123
00:05:30,395 --> 00:05:33,385
And this is the math out dot moved towards.

124
00:05:33,380 --> 00:05:35,780
So as you can see, it moves a value

125
00:05:35,780 --> 00:05:38,890
from the current towards the target.

126
00:05:38,885 --> 00:05:40,235
And you can see right here,

127
00:05:40,235 --> 00:05:44,245
move towards using the math library.

128
00:05:44,240 --> 00:05:46,240
We can add the current value,

129
00:05:46,235 --> 00:05:49,885
the target value that we want to get to, and the maximum.

130
00:05:49,880 --> 00:05:51,860
So it's the maximum change

131
00:05:51,860 --> 00:05:53,720
that should be applied to the value.

132
00:05:53,720 --> 00:05:55,810
So it's just like we have any alert,

133
00:05:55,805 --> 00:05:59,335
the slowdown factor or the Halting factor.

134
00:05:59,330 --> 00:06:02,680
This is also some kind of factor that you can use.

135
00:06:02,675 --> 00:06:06,575
Okay, so let's go ahead and implement this move towards.

136
00:06:06,574 --> 00:06:12,864
So in here I'm going to write method dot move towards.

137
00:06:12,860 --> 00:06:15,050
And I'm going to open this up.

138
00:06:15,050 --> 00:06:19,150
So the current target will be the current alpha.

139
00:06:19,145 --> 00:06:23,585
The target, okay, So the current,

140
00:06:23,585 --> 00:06:25,945
the current value, I meant to say,

141
00:06:25,940 --> 00:06:29,540
and the target will be obviously 0 F.

142
00:06:29,540 --> 00:06:32,840
And we are going to be moving at the fade speeds.

143
00:06:32,840 --> 00:06:35,450
So I'm going to just write faith speed

144
00:06:35,450 --> 00:06:39,440
in here, times Time.deltaTime.

145
00:06:39,440 --> 00:06:41,600
And I'm going to go ahead and

146
00:06:41,600 --> 00:06:44,920
create some kind of fade speed variable.

147
00:06:44,915 --> 00:06:47,195
Copy this, and let's go ahead.

148
00:06:47,195 --> 00:06:48,845
Where should we added?

149
00:06:48,845 --> 00:06:50,855
Okay, so there we go.

150
00:06:50,855 --> 00:06:54,335
This will be the fate speed and set it to five.

151
00:06:54,335 --> 00:06:56,795
Let's set it to one and see how fast that is.

152
00:06:56,795 --> 00:06:58,465
I'm not sure how fast will be.

153
00:06:58,460 --> 00:07:00,020
I really have no idea.

154
00:07:00,020 --> 00:07:01,870
So we're going to save this.

155
00:07:01,865 --> 00:07:05,105
So what happens in here is when the lifetime,

156
00:07:05,105 --> 00:07:06,145
there is an error,

157
00:07:06,140 --> 00:07:08,470
which is that the lifetime is less than 0.

158
00:07:08,465 --> 00:07:11,825
Okay? So we need to add a point and come up.

159
00:07:11,825 --> 00:07:15,535
So what happens in here when the lifetime is less than 0?

160
00:07:15,530 --> 00:07:17,690
So we've finished spreading out,

161
00:07:17,690 --> 00:07:19,150
the lifetime is done.

162
00:07:19,145 --> 00:07:22,565
We are going to go through this every single time.

163
00:07:22,565 --> 00:07:24,685
So what happens in here?

164
00:07:24,680 --> 00:07:26,270
The red, the green, the blue stay the

165
00:07:26,270 --> 00:07:28,120
same on the part Sprite.

166
00:07:28,115 --> 00:07:31,345
But the color, the Alpha moves from

167
00:07:31,340 --> 00:07:33,880
the current state that it is

168
00:07:33,875 --> 00:07:37,235
n to the 0 using some kind of fade speed.

169
00:07:37,235 --> 00:07:39,275
So let's go and test this out.

170
00:07:39,275 --> 00:07:41,885
So I'm going to run the game.

171
00:07:41,885 --> 00:07:47,675
I'm going to try to make this bigger, to see everything.

172
00:07:47,675 --> 00:07:50,245
I'm going to destroy this.

173
00:07:50,240 --> 00:07:52,070
And now we'll see that

174
00:07:52,070 --> 00:07:54,610
the broken pieces after a couple of seconds.

175
00:07:54,605 --> 00:07:56,515
Whoa, how cool is that?

176
00:07:56,510 --> 00:07:58,010
So they slowly disappeared.

177
00:07:58,010 --> 00:07:59,120
And if you noticed,

178
00:07:59,120 --> 00:08:01,360
we had the three parts being

179
00:08:01,355 --> 00:08:03,925
part 3 and they all look different.

180
00:08:03,920 --> 00:08:07,090
So we didn't have any thing that looks similar.

181
00:08:07,085 --> 00:08:08,395
But as you noticed,

182
00:08:08,390 --> 00:08:09,770
even though they fade it out,

183
00:08:09,770 --> 00:08:11,630
we still have them right here.

184
00:08:11,630 --> 00:08:14,450
We need to get rid of this and it's going to be

185
00:08:14,450 --> 00:08:17,660
a challenge for you to destroy the pieces.

186
00:08:17,660 --> 00:08:20,300
Finally, so create an if statement

187
00:08:20,300 --> 00:08:23,150
that checks if the piece alphas down to 0.

188
00:08:23,150 --> 00:08:25,160
And if it is, you'll need to go ahead and

189
00:08:25,160 --> 00:08:27,500
destroy the game object.

190
00:08:27,500 --> 00:08:33,170
So pause the video right now and go do the challenge.

191
00:08:33,170 --> 00:08:35,210
Okay, welcome back.

192
00:08:35,210 --> 00:08:39,080
So in here, after we've moved towards,

193
00:08:39,080 --> 00:08:41,170
we are going to keep checking if

194
00:08:41,165 --> 00:08:49,525
the part Sprite dot color dot alpha,

195
00:08:49,520 --> 00:08:54,230
which is the a, is equal to 0 f. Then

196
00:08:54,230 --> 00:08:59,090
we are simply going to destroy the game object.

197
00:08:59,090 --> 00:09:01,700
Save that back in our game.

198
00:09:01,700 --> 00:09:03,520
Let's maximize on play.

199
00:09:03,515 --> 00:09:06,755
Our game is looking fantastic.

200
00:09:06,755 --> 00:09:09,295
I mean, except for the blue lines that you just saw.

201
00:09:09,290 --> 00:09:13,000
I mean, we have the enemies shooting at us, following us.

202
00:09:12,995 --> 00:09:15,245
We have weapons shooting properly.

203
00:09:15,245 --> 00:09:16,675
We have different effects

204
00:09:16,670 --> 00:09:18,530
for different weapons hitting things.

205
00:09:18,530 --> 00:09:20,000
You can see maybe

206
00:09:20,000 --> 00:09:23,750
the salt hitting could use a bit of help.

207
00:09:23,750 --> 00:09:25,470
We have blood everywhere.

208
00:09:25,474 --> 00:09:27,594
We have blood when we interact.

209
00:09:27,590 --> 00:09:28,790
Well, what did you see that?

210
00:09:28,790 --> 00:09:30,670
So when we should wait,

211
00:09:30,665 --> 00:09:32,275
when we shoot one of the enemies,

212
00:09:32,270 --> 00:09:34,270
you can see the blood coming out of them,

213
00:09:34,265 --> 00:09:36,865
which is really nice. There you go.

214
00:09:36,860 --> 00:09:39,380
You can see the blood. We have random blood we have

215
00:09:39,380 --> 00:09:42,230
now also the structural objects if you want.

216
00:09:42,230 --> 00:09:43,580
We can also implement

217
00:09:43,580 --> 00:09:45,110
their destruction when we shoot them.

218
00:09:45,110 --> 00:09:46,910
But anyways, when we crush them,

219
00:09:46,910 --> 00:09:48,670
these are the pieces.

220
00:09:48,665 --> 00:09:52,405
And they disappear slowly,

221
00:09:52,400 --> 00:09:54,340
which is a very nice addition.

222
00:09:54,335 --> 00:09:56,725
And I always forget too,

223
00:09:56,720 --> 00:09:58,510
I wanted to, okay?

224
00:09:58,505 --> 00:10:00,865
Okay, so remove the maximization. There you go.

225
00:10:00,860 --> 00:10:03,860
We can see that we no longer have any parts,

226
00:10:03,860 --> 00:10:05,780
but we still have the blood,

227
00:10:05,780 --> 00:10:07,330
which you know what,

228
00:10:07,325 --> 00:10:08,635
I'm going to give it to you as

229
00:10:08,630 --> 00:10:11,390
a challenge from I don't know.

230
00:10:11,390 --> 00:10:13,420
I'm not going to deal with this right now.

231
00:10:13,415 --> 00:10:15,325
Obviously at some point I will,

232
00:10:15,320 --> 00:10:17,480
but I pretty sure you know what to do.

233
00:10:17,480 --> 00:10:20,200
You can use whatever method we've learned.

234
00:10:20,195 --> 00:10:21,445
There are a lot of things,

235
00:10:21,440 --> 00:10:23,150
so I hope you enjoyed this section.

236
00:10:23,150 --> 00:10:24,950
I think it really adds to

237
00:10:24,950 --> 00:10:26,960
the whole atmosphere of the game,

238
00:10:26,960 --> 00:10:29,200
the moving, slowly destroying.

239
00:10:29,195 --> 00:10:31,385
We learned a lot in this section also.

240
00:10:31,385 --> 00:10:34,855
So make sure that you are always committing your changes.

241
00:10:34,850 --> 00:10:36,440
And if you haven't already,

242
00:10:36,440 --> 00:10:37,640
I know I bored you,

243
00:10:37,640 --> 00:10:41,200
but please leave me a review helps me out a lot.

244
00:10:41,195 --> 00:10:43,105
Of course, if you're enjoying the course,

245
00:10:43,100 --> 00:10:46,340
if not, not, until you begin enjoying the course.

246
00:10:46,340 --> 00:10:50,490
Anyways, I'll see you in the next one.

