1
00:00:00,000 --> 00:00:03,240
Welcome back, My favorite game developers.

2
00:00:03,240 --> 00:00:05,550
And in this video, when we run the game,

3
00:00:05,550 --> 00:00:08,070
we are still breaking the part,

4
00:00:08,070 --> 00:00:10,460
but you'll notice that after disappears,

5
00:00:10,455 --> 00:00:12,545
we have three parts.

6
00:00:12,540 --> 00:00:15,590
Part 1 on the ground being destroyed.

7
00:00:15,585 --> 00:00:20,705
We have a part 2 and part 3 and another part 3.

8
00:00:20,700 --> 00:00:24,480
So we are instantiating and broken parts when

9
00:00:24,480 --> 00:00:26,460
an object or a breakable bond

10
00:00:26,460 --> 00:00:28,620
breakable object is destroyed.

11
00:00:28,620 --> 00:00:31,160
So let's not waste any more time.

12
00:00:31,155 --> 00:00:33,165
And let's get started.

13
00:00:33,160 --> 00:00:34,940
Oh, case.

14
00:00:34,940 --> 00:00:37,850
So time to make some broken pieces.

15
00:00:37,850 --> 00:00:40,670
The first thing that we're going to do is I'm

16
00:00:40,670 --> 00:00:43,810
going to drag this broken piece right here.

17
00:00:43,805 --> 00:00:45,715
And where is it?

18
00:00:45,710 --> 00:00:46,850
Double-click of k.

19
00:00:46,850 --> 00:00:48,170
There we are.

20
00:00:48,170 --> 00:00:51,700
And let's make sure that it's on the player layer.

21
00:00:51,695 --> 00:00:54,265
And I think we should change this to

22
00:00:54,260 --> 00:00:57,400
have the level objects and not the player lives.

23
00:00:57,395 --> 00:00:59,215
But for now I'm just going to

24
00:00:59,210 --> 00:01:01,540
keep it as it is not worth about that.

25
00:01:01,535 --> 00:01:03,235
Obviously, we should change it,

26
00:01:03,230 --> 00:01:05,590
but we're not going to do that right now.

27
00:01:05,585 --> 00:01:09,755
I'm going to add a 23.

28
00:01:09,755 --> 00:01:11,935
So now we have three pieces.

29
00:01:11,930 --> 00:01:13,520
I'm going to select all of them and

30
00:01:13,520 --> 00:01:15,400
make them all on the player.

31
00:01:15,395 --> 00:01:18,905
There you go, the PS2 and the bees.

32
00:01:18,905 --> 00:01:21,455
Three, I'm going to save that.

33
00:01:21,455 --> 00:01:24,035
Before I move on.

34
00:01:24,035 --> 00:01:33,755
Let's change this into the Baroque and part one.

35
00:01:33,755 --> 00:01:37,585
Copy that broken part

36
00:01:37,580 --> 00:01:43,960
2 and the broken part 3. There we go.

37
00:01:43,955 --> 00:01:45,895
Now, I'm going to go into

38
00:01:45,890 --> 00:01:49,670
the Prefabs folder and I'm going to right-click

39
00:01:49,670 --> 00:01:56,030
and create a new folder for the broken parts.

40
00:01:56,030 --> 00:02:00,220
Open it up. And I'm going to add them one by one.

41
00:02:00,215 --> 00:02:03,445
If we select all of them and drag we, it doesn't work.

42
00:02:03,440 --> 00:02:06,680
So I'm going to drag one after the other.

43
00:02:06,680 --> 00:02:10,460
So now we have the three broken pieces and if you want,

44
00:02:10,460 --> 00:02:12,590
you can make something even better.

45
00:02:12,590 --> 00:02:15,230
So we can add, for example, the third piece.

46
00:02:15,230 --> 00:02:17,870
We can rotate it around.

47
00:02:17,870 --> 00:02:19,670
So now we have this piece right here,

48
00:02:19,670 --> 00:02:21,340
we rotate it around.

49
00:02:21,335 --> 00:02:22,615
And this can be called,

50
00:02:22,610 --> 00:02:25,090
for example, the piece for, if you want,

51
00:02:25,085 --> 00:02:27,565
I recommend you use your imagination when

52
00:02:27,560 --> 00:02:30,400
in doubt with the pieces that we have.

53
00:02:30,395 --> 00:02:32,245
I'm going to select these three and make

54
00:02:32,240 --> 00:02:34,490
sure that their position is at 0.

55
00:02:34,490 --> 00:02:36,130
Because for example, if we have,

56
00:02:36,125 --> 00:02:38,855
let's say this as a six, Okay,

57
00:02:38,855 --> 00:02:41,155
We add them at six or have them all at six

58
00:02:41,150 --> 00:02:43,600
if I add them so I instantiate them.

59
00:02:43,595 --> 00:02:45,475
You can see that it starts off at

60
00:02:45,470 --> 00:02:47,830
the position 6 not on 0.

61
00:02:47,825 --> 00:02:49,225
This will cause problems.

62
00:02:49,220 --> 00:02:52,630
So I always like to keep it at 0,

63
00:02:52,625 --> 00:02:54,415
even though when we instantiate

64
00:02:54,410 --> 00:02:56,390
it at a certain transform position,

65
00:02:56,390 --> 00:02:59,540
it will take the position that we tell it.

66
00:02:59,540 --> 00:03:01,720
But anyways, let's continue on.

67
00:03:01,715 --> 00:03:03,815
I think you know what I meant?

68
00:03:03,815 --> 00:03:06,115
I'm going to delete this broken pieces.

69
00:03:06,110 --> 00:03:11,410
I'm going to open up the breakable script if I find it.

70
00:03:11,405 --> 00:03:14,495
And in here we are going to first of all,

71
00:03:14,495 --> 00:03:17,405
obviously get a reference to the broken part.

72
00:03:17,405 --> 00:03:20,875
So I'm going to create a serialized field of

73
00:03:20,870 --> 00:03:25,120
type game object and call this the broken part.

74
00:03:25,115 --> 00:03:26,405
Save that.

75
00:03:26,405 --> 00:03:29,755
And then before we destroy the game object,

76
00:03:29,750 --> 00:03:33,010
I'm going to go ahead and instantiate

77
00:03:33,005 --> 00:03:36,895
broken part at transform.py

78
00:03:36,890 --> 00:03:41,230
position and transform the rotation.

79
00:03:41,225 --> 00:03:42,865
Save that.

80
00:03:42,860 --> 00:03:45,820
Let's go back in our game.

81
00:03:45,815 --> 00:03:49,475
Right, for this, compile any day now.

82
00:03:49,475 --> 00:03:52,745
Okay, So I'm going to lock this down.

83
00:03:52,745 --> 00:03:55,145
I'm going to go into

84
00:03:55,145 --> 00:03:59,285
prefabs and select the broken pieces.

85
00:03:59,285 --> 00:04:01,475
And let's just add the first one,

86
00:04:01,475 --> 00:04:05,695
save that the game collide with the object

87
00:04:05,690 --> 00:04:07,700
there go at breaks and we should be

88
00:04:07,700 --> 00:04:09,880
left with this piece right here.

89
00:04:09,875 --> 00:04:10,945
Okay, Very nice.

90
00:04:10,940 --> 00:04:12,140
Now it's not disappearing.

91
00:04:12,140 --> 00:04:13,940
We'll handle that in just a bit.

92
00:04:13,940 --> 00:04:16,790
But for now we have the pieces breaking.

93
00:04:16,790 --> 00:04:19,040
But obviously, obviously we want to have

94
00:04:19,040 --> 00:04:21,380
multiple pieces breaking and

95
00:04:21,380 --> 00:04:23,150
appearing one after the other.

96
00:04:23,150 --> 00:04:26,210
So this is going to be your challenge,

97
00:04:26,210 --> 00:04:29,350
your challenge to instantiate all the pieces.

98
00:04:29,345 --> 00:04:30,565
So first of all,

99
00:04:30,560 --> 00:04:31,910
get all the pieces from

100
00:04:31,910 --> 00:04:34,030
the sprites we've already done that.

101
00:04:34,025 --> 00:04:37,225
Create an array of game objects for

102
00:04:37,220 --> 00:04:39,230
the broken parts looped

103
00:04:39,230 --> 00:04:41,630
through the pieces you using the for loop.

104
00:04:41,630 --> 00:04:44,750
So we've already learned how a for loop works.

105
00:04:44,750 --> 00:04:46,300
You'll need to generate

106
00:04:46,295 --> 00:04:48,755
a random number every single time.

107
00:04:48,755 --> 00:04:50,605
And we'll need to use the number to

108
00:04:50,600 --> 00:04:52,970
instantiate a broken piece.

109
00:04:52,970 --> 00:04:54,940
Now you might be thinking, well,

110
00:04:54,935 --> 00:05:00,535
this means that some broken pieces will be done twice.

111
00:05:00,530 --> 00:05:01,220
And that's okay.

112
00:05:01,220 --> 00:05:02,990
Honestly, I think it's not something

113
00:05:02,990 --> 00:05:04,940
very bad to worry about.

114
00:05:04,940 --> 00:05:06,950
If you want, you can go even

115
00:05:06,950 --> 00:05:09,220
deeper into this and instantiate

116
00:05:09,215 --> 00:05:12,355
a random angle for the pieces to have so

117
00:05:12,350 --> 00:05:14,120
that they may be appear

118
00:05:14,120 --> 00:05:16,070
different from one another and don't.

119
00:05:16,070 --> 00:05:17,960
Set on the same place.

120
00:05:17,960 --> 00:05:22,450
Now, of course there will be a small hindrance here.

121
00:05:22,445 --> 00:05:25,255
We'll talk about it once you do the challenge.

122
00:05:25,250 --> 00:05:29,170
So pause the video right now and go do the challenge.

123
00:05:29,165 --> 00:05:31,615
Oh, come back.

124
00:05:31,610 --> 00:05:35,380
So before we destroy the game object,

125
00:05:35,375 --> 00:05:38,285
what we're going to do is we're going to,

126
00:05:38,284 --> 00:05:40,194
and I think that this is not

127
00:05:40,190 --> 00:05:42,740
the perfect place to do this.

128
00:05:42,740 --> 00:05:46,240
So instead of instantiating in here,

129
00:05:46,235 --> 00:05:50,305
I'm going to go ahead and move this up in here.

130
00:05:50,300 --> 00:05:53,560
So after we start the animation,

131
00:05:53,555 --> 00:05:55,775
or you know what?

132
00:05:55,775 --> 00:05:57,595
This is also a problem.

133
00:05:57,590 --> 00:06:01,810
So I'm going to make this a bit bigger.

134
00:06:01,805 --> 00:06:04,165
And after we start the animation,

135
00:06:04,160 --> 00:06:06,880
I'm going to instantiate the pieces if you want.

136
00:06:06,875 --> 00:06:08,485
If you want, you can also create

137
00:06:08,480 --> 00:06:11,260
a separate method and call it using the events.

138
00:06:11,255 --> 00:06:13,555
But for now, let's go ahead

139
00:06:13,550 --> 00:06:17,140
and do the things that we wanted to do.

140
00:06:17,135 --> 00:06:18,925
So first of all, we are going to turn

141
00:06:18,920 --> 00:06:22,210
the game object into an array,

142
00:06:22,205 --> 00:06:25,445
and the broken part double-click in here.

143
00:06:25,445 --> 00:06:29,395
And this is going to be the broken parts apply that.

144
00:06:29,390 --> 00:06:31,460
I'm going to comment this out in order

145
00:06:31,460 --> 00:06:33,520
to see everything in the inspector.

146
00:06:33,515 --> 00:06:36,095
So breakable pots scroll down,

147
00:06:36,095 --> 00:06:39,535
close these off because they are annoying.

148
00:06:39,530 --> 00:06:41,510
And what do we have here?

149
00:06:41,510 --> 00:06:42,890
The broken parts.

150
00:06:42,890 --> 00:06:44,510
Lock this down, select all

151
00:06:44,510 --> 00:06:46,100
of these and drag them in here.

152
00:06:46,100 --> 00:06:50,410
So now we have the three broken parts ready for use.

153
00:06:50,405 --> 00:06:52,195
I'll go back in here.

154
00:06:52,190 --> 00:06:54,740
And what are we going to do?

155
00:06:54,740 --> 00:06:56,270
So first of all, we are going

156
00:06:56,270 --> 00:06:58,280
to loop through all the parts.

157
00:06:58,280 --> 00:07:02,150
So for I'm going to initiate at r equals 0.

158
00:07:02,150 --> 00:07:03,730
So I hope this wasn't too hard.

159
00:07:03,725 --> 00:07:04,925
Let's start again.

160
00:07:04,925 --> 00:07:07,645
We start off by creating the variable.

161
00:07:07,640 --> 00:07:09,970
We started with a 0.

162
00:07:09,965 --> 00:07:11,875
Then what we're going to check,

163
00:07:11,870 --> 00:07:13,940
or we will keep looping.

164
00:07:13,940 --> 00:07:18,710
While I is less than the broken parts dot,

165
00:07:18,710 --> 00:07:21,650
length and length because we are using arrays.

166
00:07:21,650 --> 00:07:23,540
If we're using lists,

167
00:07:23,540 --> 00:07:28,130
we use count and every single time we increase i by one.

168
00:07:28,130 --> 00:07:30,740
So we'll need to generate three pieces,

169
00:07:30,740 --> 00:07:32,720
and we want these pieces to be random.

170
00:07:32,720 --> 00:07:36,490
So we have a variety when breaking up pots.

171
00:07:36,485 --> 00:07:39,455
So int, it's an integer.

172
00:07:39,455 --> 00:07:44,635
We're going to call it the random broken part.

173
00:07:44,630 --> 00:07:49,960
And it's going to be equal to random dot range.

174
00:07:49,955 --> 00:07:51,655
And what's the range?

175
00:07:51,650 --> 00:07:58,040
The range is from 0 into the broken parts dot length.

176
00:07:58,040 --> 00:07:59,570
There we go.

177
00:07:59,570 --> 00:08:01,130
Now after we do that,

178
00:08:01,130 --> 00:08:04,980
we are going to move the instantiation up.

179
00:08:07,840 --> 00:08:10,220
And inside of here,

180
00:08:10,220 --> 00:08:11,810
instead of simply the parts,

181
00:08:11,810 --> 00:08:16,300
we are going to use the part with the random broken part.

182
00:08:16,295 --> 00:08:18,365
So I'm going to save all of that.

183
00:08:18,365 --> 00:08:21,985
Okay, so let's go ahead and test this code out.

184
00:08:21,980 --> 00:08:23,810
So I'm going to go back in here.

185
00:08:23,810 --> 00:08:28,100
I'm going to see what else do we need to do?

186
00:08:28,100 --> 00:08:29,530
I think everything is good.

187
00:08:29,525 --> 00:08:34,915
Run the game. Now we walk up to the bot, does not break.

188
00:08:34,910 --> 00:08:36,730
We tried to destroy it.

189
00:08:36,725 --> 00:08:39,595
There we go, it gets destroyed and there we go.

190
00:08:39,590 --> 00:08:42,290
We have some pieces are no,

191
00:08:42,290 --> 00:08:44,980
maybe I should not have maximize on play.

192
00:08:44,980 --> 00:08:47,140
Again, stop, maximize on play.

193
00:08:47,135 --> 00:08:48,385
Let's make this bigger.

194
00:08:48,380 --> 00:08:49,640
We want to see the hierarchy.

195
00:08:49,640 --> 00:08:51,610
We want to know what's happening.

196
00:08:51,605 --> 00:08:53,425
Okay, so I run into

197
00:08:53,420 --> 00:08:55,430
the part that gets broken up. So there we go.

198
00:08:55,430 --> 00:08:57,620
We have broken part two, broken part 1,

199
00:08:57,620 --> 00:09:00,280
and broken part two.

200
00:09:00,275 --> 00:09:02,305
So I can move this just a little

201
00:09:02,300 --> 00:09:05,060
bit and the broken parts are on top of each other.

202
00:09:05,060 --> 00:09:06,950
So this is why I recommended

203
00:09:06,950 --> 00:09:09,220
that we create a bit of rotation.

204
00:09:09,215 --> 00:09:11,185
So with that done,

205
00:09:11,180 --> 00:09:14,830
let's make sure that we stage everything in here.

206
00:09:14,825 --> 00:09:16,315
We stage of that.

207
00:09:16,310 --> 00:09:19,150
And I'm going to call this, now,

208
00:09:19,145 --> 00:09:25,055
we instantiate broken parts.

209
00:09:25,055 --> 00:09:30,695
When we instantiate broken parts,

210
00:09:30,695 --> 00:09:32,015
one pots are the stroke.

211
00:09:32,015 --> 00:09:35,605
When bots are destroyed,

212
00:09:35,600 --> 00:09:38,330
save that, commit the changes.

213
00:09:38,330 --> 00:09:40,030
And I hope you are enjoying,

214
00:09:40,025 --> 00:09:42,865
I hope you are understanding everything.

215
00:09:42,860 --> 00:09:45,290
And in the next video we are going to make sure

216
00:09:45,290 --> 00:09:47,690
that the broken pieces don't just stay in place.

217
00:09:47,690 --> 00:09:50,750
They spread out as if this

218
00:09:50,750 --> 00:09:53,870
has been destroyed and we have some kind of impact.

219
00:09:53,870 --> 00:09:55,950
So I'll see you then.

