1
00:00:00,000 --> 00:00:03,180
Welcome back, my fellow game developers.

2
00:00:03,180 --> 00:00:06,710
In this video, we've created the broken pieces

3
00:00:06,705 --> 00:00:10,535
and we make sure that when we hit these broken pieces,

4
00:00:10,530 --> 00:00:12,780
they fly a bit apart.

5
00:00:12,780 --> 00:00:14,970
So now when I hit it, there you go.

6
00:00:14,970 --> 00:00:16,800
You can see that these spots moved a

7
00:00:16,800 --> 00:00:18,870
little bit and you can see that,

8
00:00:18,870 --> 00:00:21,180
okay, So for this wasn't the best example.

9
00:00:21,180 --> 00:00:22,800
Let's try to see when we

10
00:00:22,800 --> 00:00:25,590
have two parts that are the same.

11
00:00:25,590 --> 00:00:26,850
So there you go.

12
00:00:26,850 --> 00:00:29,040
Can you tell which one is part

13
00:00:29,040 --> 00:00:32,070
2 and the which one is also part two?

14
00:00:32,070 --> 00:00:35,430
Nope, you can't because we've added a random rotation.

15
00:00:35,429 --> 00:00:36,799
I thought it would have been

16
00:00:36,795 --> 00:00:38,435
a very nice idea when I told you

17
00:00:38,430 --> 00:00:40,270
about it in the previous video,

18
00:00:40,270 --> 00:00:43,370
I thought why not implemented right now into this game.

19
00:00:43,370 --> 00:00:46,130
So let's not waste any more time.

20
00:00:46,130 --> 00:00:48,470
And let's get started.

21
00:00:48,470 --> 00:00:50,270
All the case.

22
00:00:50,270 --> 00:00:54,440
So now when we destroy our broken bought,

23
00:00:54,440 --> 00:00:57,010
we have the broken pieces and speciated.

24
00:00:57,005 --> 00:00:58,315
But first of all,

25
00:00:58,310 --> 00:01:00,110
they are sometimes the same

26
00:01:00,110 --> 00:01:02,270
even though we choose random numbers.

27
00:01:02,270 --> 00:01:05,920
And the second thing is that they don't move apart.

28
00:01:05,915 --> 00:01:09,815
So the first thing that I want to handle is the rotation.

29
00:01:09,815 --> 00:01:12,935
So instead of just having the transform rotation,

30
00:01:12,935 --> 00:01:15,215
but I'm going to do is I'm going to create

31
00:01:15,215 --> 00:01:19,865
another integer, random rotation.

32
00:01:20,140 --> 00:01:28,330
And it's going to be equal to a random range between 04,

33
00:01:28,325 --> 00:01:30,985
Y4 because I'm going to be rotating

34
00:01:30,980 --> 00:01:34,330
them around the z-axis in four directions.

35
00:01:34,325 --> 00:01:36,275
So the way I'm going to do that is by

36
00:01:36,275 --> 00:01:39,145
multiplying in here by 90 degrees.

37
00:01:39,140 --> 00:01:41,270
And we will have four options.

38
00:01:41,270 --> 00:01:44,240
So either we rotate ninety one hundred and twenty two

39
00:01:44,240 --> 00:01:47,500
seventy or we just brought it all the way around.

40
00:01:47,495 --> 00:01:49,825
Maybe this one should be 0.

41
00:01:49,820 --> 00:01:52,280
We start off at 0123.

42
00:01:52,280 --> 00:01:55,100
Okay, so instead of the transform dot rotation,

43
00:01:55,100 --> 00:01:56,570
I'm going to use something called

44
00:01:56,570 --> 00:02:00,320
the quaternion, that ruler.

45
00:02:00,315 --> 00:02:02,715
There we go, That's ruler.

46
00:02:02,710 --> 00:02:04,930
I was looking for it and see it.

47
00:02:04,930 --> 00:02:07,180
So you can see that this translates

48
00:02:07,180 --> 00:02:09,750
the three axis and to the rotation.

49
00:02:09,745 --> 00:02:11,385
And we are going to be obviously

50
00:02:11,380 --> 00:02:14,350
rotating on the, around the z-axis.

51
00:02:14,350 --> 00:02:18,630
So 90 half times the random rotation.

52
00:02:18,625 --> 00:02:19,515
There we go.

53
00:02:19,510 --> 00:02:22,270
Save that. You can see the instantiation right here.

54
00:02:22,270 --> 00:02:24,480
If you think this line is a bit too long,

55
00:02:24,475 --> 00:02:28,255
what you can do is hit Enter on every single line.

56
00:02:28,255 --> 00:02:31,185
And now it's much more obvious what

57
00:02:31,180 --> 00:02:34,650
this instantiation does and what the elements of it are.

58
00:02:34,645 --> 00:02:36,255
Okay, great. Now with this none,

59
00:02:36,250 --> 00:02:38,290
the next thing is we want to add

60
00:02:38,290 --> 00:02:41,580
behavior to our broken pots.

61
00:02:41,575 --> 00:02:45,755
So we are going to go into the scripts in here and

62
00:02:45,755 --> 00:02:48,805
the breakable right-click and create

63
00:02:48,800 --> 00:02:53,020
a C sharp script which will be called the broken pot.

64
00:02:53,015 --> 00:02:57,365
So broken part, hit enter.

65
00:02:57,365 --> 00:03:00,745
Okay, I don't know why this keeps happening

66
00:03:00,740 --> 00:03:03,640
and I'm going to go into the broken parts,

67
00:03:03,635 --> 00:03:05,695
select all three of them,

68
00:03:05,690 --> 00:03:07,190
and go ahead and add

69
00:03:07,190 --> 00:03:12,700
the broken part script on each one of these prefab.

70
00:03:12,695 --> 00:03:15,395
Open the script up and what do we want in here?

71
00:03:15,395 --> 00:03:18,445
So first of all, we want a variable too,

72
00:03:18,440 --> 00:03:21,040
after the movement speed of these parts.

73
00:03:21,035 --> 00:03:25,055
So it's a serialized field which is going to be a float.

74
00:03:25,055 --> 00:03:28,255
And we'll go to the move speed.

75
00:03:28,250 --> 00:03:31,280
And if you want to add a bit more variety,

76
00:03:31,280 --> 00:03:33,200
you can even make each one of them

77
00:03:33,200 --> 00:03:35,120
has a different speed and baby,

78
00:03:35,120 --> 00:03:39,140
depending on how fast dashes or whatever you want to do.

79
00:03:39,140 --> 00:03:43,090
Then we are going to create a private vector 3.

80
00:03:43,085 --> 00:03:45,815
And this will represent the movement direction.

81
00:03:45,815 --> 00:03:48,875
So movement direction.

82
00:03:48,875 --> 00:03:52,235
So in what direction will each piece be moving in?

83
00:03:52,235 --> 00:03:54,085
And finally, we will have something

84
00:03:54,080 --> 00:03:56,060
called a serialized field,

85
00:03:56,060 --> 00:03:58,040
which is going to be a float.

86
00:03:58,040 --> 00:04:02,370
And this is the halting factor.

87
00:04:02,374 --> 00:04:05,334
And it's going to be 50 of these numbers are

88
00:04:05,330 --> 00:04:08,360
arbitrary and I've already tested a couple of outsold.

89
00:04:08,360 --> 00:04:09,950
That's why I know why five-act.

90
00:04:09,950 --> 00:04:13,520
But anyways, what is this halting factor?

91
00:04:13,520 --> 00:04:16,060
Obviously, when our pieces break up,

92
00:04:16,055 --> 00:04:17,635
they will spread apart,

93
00:04:17,630 --> 00:04:19,750
but then they will stop at some point.

94
00:04:19,745 --> 00:04:22,885
So our goal is to make the salting factor,

95
00:04:22,880 --> 00:04:25,610
which will affect the pieces as

96
00:04:25,610 --> 00:04:28,400
they move away from each other and finally stopped them.

97
00:04:28,400 --> 00:04:31,700
So we will slowly be slowing down.

98
00:04:31,700 --> 00:04:33,910
And I'll show you how we'll be

99
00:04:33,905 --> 00:04:36,425
slowing down using something called lobe.

100
00:04:36,425 --> 00:04:38,635
But let's not get ahead of ourselves.

101
00:04:38,630 --> 00:04:41,270
First thing we want to do is we want to have

102
00:04:41,270 --> 00:04:45,220
a random direction in which each piece moves.

103
00:04:45,215 --> 00:04:46,895
So first of all, I'm going to

104
00:04:46,895 --> 00:04:49,735
make a movement direction of x.

105
00:04:49,730 --> 00:04:51,760
So why am I doing this and start?

106
00:04:51,755 --> 00:04:54,265
Because the broken part is attached

107
00:04:54,260 --> 00:04:56,890
to the broken piece, which instantiates.

108
00:04:56,885 --> 00:04:59,125
And as soon as it instantiates,

109
00:04:59,120 --> 00:05:00,950
the start is being called.

110
00:05:00,950 --> 00:05:03,730
And then start will determine what the direction is.

111
00:05:03,725 --> 00:05:07,765
So the movement direction dot x2 will be equal to

112
00:05:07,760 --> 00:05:13,340
the random dot range and what values you,

113
00:05:13,340 --> 00:05:15,860
you can choose any value you want in here.

114
00:05:15,860 --> 00:05:18,650
But just to make it a bit of fun.

115
00:05:18,650 --> 00:05:20,480
For example, you have

116
00:05:20,480 --> 00:05:23,450
the broken pieces flying off very fast.

117
00:05:23,450 --> 00:05:25,490
I think the direction,

118
00:05:25,490 --> 00:05:28,130
the randomness of the direction will

119
00:05:28,130 --> 00:05:31,540
be a bit more wacky, let's call it.

120
00:05:31,535 --> 00:05:33,445
So what we'll do is we'll use

121
00:05:33,440 --> 00:05:36,980
the minus movement speed as

122
00:05:36,980 --> 00:05:40,850
the minimum and the movement speed as the maximum.

123
00:05:40,850 --> 00:05:43,870
So now as the movement speed increases,

124
00:05:43,865 --> 00:05:45,665
if you wish to increase it,

125
00:05:45,665 --> 00:05:48,845
the randomness will also increase.

126
00:05:48,845 --> 00:05:50,605
And having a minus will make

127
00:05:50,600 --> 00:05:52,670
sure that some pieces just go.

128
00:05:52,670 --> 00:05:55,120
I don't know where some random place.

129
00:05:55,115 --> 00:05:57,995
So now with the movement acts downwards,

130
00:05:57,995 --> 00:06:00,025
that's have the movement y and

131
00:06:00,020 --> 00:06:02,270
it's going to be exactly the same.

132
00:06:02,270 --> 00:06:03,010
With that.

133
00:06:03,005 --> 00:06:04,495
Let's continue on. We want to

134
00:06:04,490 --> 00:06:06,350
start actually moving the pieces and

135
00:06:06,350 --> 00:06:09,830
we're going to use the transform dot position.

136
00:06:09,830 --> 00:06:11,960
And we're going to be increasing it by

137
00:06:11,960 --> 00:06:17,480
the movement direction times time dot delta time.

138
00:06:17,480 --> 00:06:20,450
Obviously we know plus, plus equals,

139
00:06:20,450 --> 00:06:23,690
so it moves slowly and the movement direction times

140
00:06:23,690 --> 00:06:27,100
Time.deltaTime to detach from the frame rate.

141
00:06:27,095 --> 00:06:29,875
So this would be more stable.

142
00:06:29,870 --> 00:06:30,890
Anyways.

143
00:06:30,890 --> 00:06:32,570
Now we need to make sure that we

144
00:06:32,570 --> 00:06:34,520
are halting the movement of

145
00:06:34,520 --> 00:06:36,590
each piece and not just having them move

146
00:06:36,590 --> 00:06:39,050
into infinity and going through everything.

147
00:06:39,050 --> 00:06:40,970
So the way we are going to do

148
00:06:40,970 --> 00:06:43,190
that is using something called the Learn.

149
00:06:43,190 --> 00:06:46,190
So I'm going to show you what is,

150
00:06:46,190 --> 00:06:47,830
make sure that you find it on your

151
00:06:47,825 --> 00:06:49,655
own in Unity's documentation.

152
00:06:49,655 --> 00:06:53,225
What does it do? It takes in three variables.

153
00:06:53,225 --> 00:06:55,915
The first one is the start value of

154
00:06:55,910 --> 00:06:59,320
the speed of and with its the start value.

155
00:06:59,315 --> 00:07:02,005
B is the end value and t is

156
00:07:02,000 --> 00:07:05,270
the value to interpolate between a and b.

157
00:07:05,270 --> 00:07:07,580
What does interpolation mean?

158
00:07:07,580 --> 00:07:10,270
So, linear interpolation between the two points.

159
00:07:10,265 --> 00:07:15,295
So what happens is we want to take the value a or any

160
00:07:15,290 --> 00:07:21,080
value from a to B using a factor of t Every single time.

161
00:07:21,080 --> 00:07:22,220
So this will be

162
00:07:22,220 --> 00:07:23,750
the Halting factor that

163
00:07:23,750 --> 00:07:25,700
we talked about that we are going to

164
00:07:25,700 --> 00:07:27,920
be applying and make sure that

165
00:07:27,920 --> 00:07:30,310
you understand what is happening in here.

166
00:07:30,305 --> 00:07:34,645
We are trying to move the pieces slower and slower.

167
00:07:34,640 --> 00:07:39,520
So we are going to start off with some kind of direction.

168
00:07:39,515 --> 00:07:42,155
And then we are going to be taking it

169
00:07:42,155 --> 00:07:45,335
to 0 to stop the pieces from moving.

170
00:07:45,335 --> 00:07:48,395
So I'm going to issue

171
00:07:48,395 --> 00:07:51,925
this challenge to use her to stop the movement.

172
00:07:51,920 --> 00:07:54,020
So first of all, we want to decrease

173
00:07:54,020 --> 00:07:56,350
the momentum of the broken pieces,

174
00:07:56,345 --> 00:07:57,625
of the moving pieces.

175
00:07:57,620 --> 00:08:01,360
We want to set the movement direction to learn.

176
00:08:01,355 --> 00:08:03,935
We're going to start value.

177
00:08:03,935 --> 00:08:05,905
We're going to have the start value

178
00:08:05,900 --> 00:08:08,150
be the actual movement direction,

179
00:08:08,150 --> 00:08:10,730
and the end value should be the 0.

180
00:08:10,730 --> 00:08:14,420
And finally, the speed of which we are going to

181
00:08:14,420 --> 00:08:16,190
be halting the movement is going

182
00:08:16,190 --> 00:08:18,020
to be the Halting factor.

183
00:08:18,020 --> 00:08:20,740
So basically this challenge as you

184
00:08:20,735 --> 00:08:24,085
applying, stop the movement.

185
00:08:24,080 --> 00:08:29,600
So pause the video right now and go do the challenge.

186
00:08:29,600 --> 00:08:31,610
Okay, welcome back.

187
00:08:31,610 --> 00:08:33,920
So in here, after our

188
00:08:33,920 --> 00:08:37,250
while we are moving in certain directions.

189
00:08:37,249 --> 00:08:39,919
So we have the movement direction as

190
00:08:39,920 --> 00:08:42,470
the only factor that's affecting this.

191
00:08:42,470 --> 00:08:47,030
So in here are going to set the move direction to

192
00:08:47,030 --> 00:08:53,160
be equal to a vector three bottlers.

193
00:08:53,980 --> 00:08:57,170
And in here, the initial value

194
00:08:57,170 --> 00:08:59,300
will be the movement direction.

195
00:08:59,300 --> 00:09:05,740
The final value will be vector 3 0, and the factor,

196
00:09:05,735 --> 00:09:07,285
the T will be

197
00:09:07,280 --> 00:09:09,950
the Halting factor times

198
00:09:09,950 --> 00:09:12,530
Time.deltaTime because we are

199
00:09:12,530 --> 00:09:13,730
learning every single frame,

200
00:09:13,730 --> 00:09:15,580
so we don't want it to be very fast.

201
00:09:15,575 --> 00:09:19,105
Okay, so let's go back in here and

202
00:09:19,100 --> 00:09:22,790
let's test this out and see if it works. For we do that.

203
00:09:22,790 --> 00:09:24,110
I'm going to go into the

204
00:09:24,110 --> 00:09:28,470
prefab and see what we have broken parts.

205
00:09:28,630 --> 00:09:30,980
Maybe we can add them here.

206
00:09:30,980 --> 00:09:32,270
So breakable pot.

207
00:09:32,270 --> 00:09:33,050
There we go.

208
00:09:33,050 --> 00:09:34,970
It's in the broken parts,

209
00:09:34,970 --> 00:09:36,820
maybe whatever it's called.

210
00:09:36,815 --> 00:09:39,275
Okay, broken pots, run the game.

211
00:09:39,275 --> 00:09:40,805
Let's see what happens.

212
00:09:40,805 --> 00:09:42,605
Maybe this doesn't work at all then.

213
00:09:42,605 --> 00:09:43,795
Oh, nice.

214
00:09:43,790 --> 00:09:45,290
So as you can see,

215
00:09:45,290 --> 00:09:49,640
each part went our separate ways and you can see we have

216
00:09:49,640 --> 00:09:52,040
the part one which was exactly on

217
00:09:52,040 --> 00:09:54,890
top of the part one or the other part 1.

218
00:09:54,890 --> 00:09:56,800
So they had the same rotation.

219
00:09:56,795 --> 00:09:58,595
Let's test this out again.

220
00:09:58,595 --> 00:10:00,665
There we go, we destroy it.

221
00:10:00,665 --> 00:10:02,605
What do we have? Part 2.

222
00:10:02,600 --> 00:10:06,710
2. And so can you figure out which part the two was?

223
00:10:06,710 --> 00:10:08,650
So if I move this right,

224
00:10:08,645 --> 00:10:11,945
also still having the same rotation for some reason,

225
00:10:11,945 --> 00:10:14,245
did we do something not completely right,

226
00:10:14,240 --> 00:10:19,160
so I'm going to break the pot and let's see.

227
00:10:19,160 --> 00:10:21,080
Okay, so I feel that

228
00:10:21,080 --> 00:10:23,720
the rotation thing isn't working if I

229
00:10:23,720 --> 00:10:29,330
go back to the breakable random rotation.

230
00:10:29,330 --> 00:10:30,860
Okay, welcome back.

231
00:10:30,860 --> 00:10:33,110
So I was worried that there was something

232
00:10:33,110 --> 00:10:35,600
wrong with the code, but no, apparently,

233
00:10:35,600 --> 00:10:37,700
we are either the luckiest people on

234
00:10:37,700 --> 00:10:40,010
the world or the unlucky asked people on the world,

235
00:10:40,010 --> 00:10:41,360
depending on how you look at it.

236
00:10:41,360 --> 00:10:43,880
But every single time the rotations were the same.

237
00:10:43,880 --> 00:10:45,100
So you can see right here,

238
00:10:45,095 --> 00:10:50,635
part two is minus 180 and the other part 2 is minus 90.

239
00:10:50,630 --> 00:10:53,150
So again, let's test this out.

240
00:10:53,150 --> 00:10:55,010
And let's see, there we go.

241
00:10:55,010 --> 00:10:55,780
We break it.

242
00:10:55,775 --> 00:10:58,015
And you can see we have Part 1 and Part one.

243
00:10:58,010 --> 00:11:01,420
But can you tell the difference between these two parts?

244
00:11:01,415 --> 00:11:04,195
So you can see that this one is 90 degrees

245
00:11:04,190 --> 00:11:07,030
rotated and this one is the same.

246
00:11:07,025 --> 00:11:10,255
Wrote it. So there is a big difference between them.

247
00:11:10,250 --> 00:11:13,070
So that done, I hope you can

248
00:11:13,070 --> 00:11:16,730
see and appreciate how random rotations work.

249
00:11:16,730 --> 00:11:18,560
I hope you can see and appreciate how

250
00:11:18,560 --> 00:11:21,590
the small detail of adding a bit of velocity to

251
00:11:21,590 --> 00:11:23,780
the broken pieces makes everything look

252
00:11:23,780 --> 00:11:27,230
more and let's say natural and better.

253
00:11:27,230 --> 00:11:29,620
So let's make sure that we stay

254
00:11:29,615 --> 00:11:32,125
all of our changes and I hope you're

255
00:11:32,120 --> 00:11:34,100
enjoying and I'll see you in the next video

256
00:11:34,100 --> 00:11:36,380
where we actually will be getting rid of

257
00:11:36,380 --> 00:11:38,290
these part that fell apart

258
00:11:38,285 --> 00:11:41,105
using something similar to learn.

259
00:11:41,105 --> 00:11:45,055
But we'll be applying it to the broken parts to make

260
00:11:45,050 --> 00:11:49,070
them slowly disappear and not just an instant go away.

261
00:11:49,070 --> 00:11:51,030
So I'll see you.

