1
00:00:00,590 --> 00:00:08,150
Welcome back my fellow game developers to a brand new ultra exciting video.

2
00:00:08,145 --> 00:00:10,005
We've created bullets previously,

3
00:00:10,005 --> 00:00:11,385
we made them fly,

4
00:00:11,385 --> 00:00:14,295
but now using the power of brief up,

5
00:00:14,295 --> 00:00:15,755
the power of input,

6
00:00:15,750 --> 00:00:17,610
the power of instantiation.

7
00:00:17,610 --> 00:00:19,820
I mean, we've learned so much in this video,

8
00:00:19,815 --> 00:00:24,305
we can start shooting bullets in various directions.

9
00:00:24,300 --> 00:00:26,630
How cool is that?

10
00:00:26,625 --> 00:00:32,495
As you can see, we have multiple bullets shooting from our shotgun.

11
00:00:32,490 --> 00:00:34,910
So let's not waste any more time.

12
00:00:34,905 --> 00:00:37,655
And let's get started.

13
00:00:37,650 --> 00:00:43,210
Oh, okay, so time to start firing hour bullets.

14
00:00:43,205 --> 00:00:47,065
But we want multiple bullets to be fired.

15
00:00:47,060 --> 00:00:49,460
We don't just want to have one bullet.

16
00:00:49,460 --> 00:00:51,110
So what shall we do?

17
00:00:51,110 --> 00:00:54,580
Shall we duplicate this bullet multiple times?

18
00:00:54,575 --> 00:00:57,815
By the way, I'm doing this using Control D. Well,

19
00:00:57,815 --> 00:01:00,755
obviously not, this is not a great idea.

20
00:01:00,755 --> 00:01:05,485
What we need to do is we need to create something called a prefab.

21
00:01:05,480 --> 00:01:07,240
What is a prefix?

22
00:01:07,235 --> 00:01:08,755
Let me show you.

23
00:01:08,750 --> 00:01:11,410
A prefab basically means it's

24
00:01:11,405 --> 00:01:18,655
a previously fabricated game object that we can use multiple times.

25
00:01:18,650 --> 00:01:20,050
There are a lot,

26
00:01:20,045 --> 00:01:23,815
a lot of benefits to having prefabs when it

27
00:01:23,810 --> 00:01:27,770
comes to create new scenes and doing different things.

28
00:01:27,770 --> 00:01:32,140
Let's go over a couple of important things about prefabs.

29
00:01:32,135 --> 00:01:35,935
It's a template for a certain game object.

30
00:01:35,930 --> 00:01:42,350
So just like we have the bullet with its specific components at specific collider,

31
00:01:42,350 --> 00:01:47,840
we can create a prefab out of it and create multiple bullets with the same components,

32
00:01:47,840 --> 00:01:49,070
with the same sprites,

33
00:01:49,070 --> 00:01:51,910
the same collider, et cetera.

34
00:01:51,905 --> 00:01:57,295
Prefab system allows you to store a game object with all its properties.

35
00:01:57,290 --> 00:01:58,880
So just like I said,

36
00:01:58,880 --> 00:02:01,370
you can create a prefab of the bullet and then

37
00:02:01,370 --> 00:02:04,090
create multiple bullets with the same properties,

38
00:02:04,085 --> 00:02:06,395
game components, et cetera.

39
00:02:06,395 --> 00:02:08,815
What do we use it for?

40
00:02:08,810 --> 00:02:13,090
We can reuse a game object configured in a particular way.

41
00:02:13,085 --> 00:02:15,445
Hence the bullet we can create multiple, but let's see,

42
00:02:15,440 --> 00:02:20,050
we'll see how in just a bit we can edit many objects at once.

43
00:02:20,045 --> 00:02:22,985
So for example, let's say we want to make the bullets faster.

44
00:02:22,985 --> 00:02:26,585
Should we go and change all the bullets or every single time?

45
00:02:26,585 --> 00:02:33,305
No, we can change only the prefab and every other form of this prefab will be changed.

46
00:02:33,305 --> 00:02:36,595
We can use the same object in different scenes,

47
00:02:36,590 --> 00:02:40,150
much easier when we start creating multiple levels.

48
00:02:40,145 --> 00:02:43,925
You'll know what I mean by this, and that's it.

49
00:02:43,925 --> 00:02:46,925
So let's go and create a prefab.

50
00:02:46,925 --> 00:02:52,525
Now, the process of creating a Prefabs is extremely, extremely hard.

51
00:02:52,520 --> 00:02:56,080
So be very, put your caps on,

52
00:02:56,075 --> 00:02:58,195
stop everything, and watch what I do.

53
00:02:58,190 --> 00:02:59,770
This is very important.

54
00:02:59,765 --> 00:03:04,775
First of all, we create a folder which is the prefabs.

55
00:03:04,775 --> 00:03:09,715
I'm going to go in here and now I'm going to create a prefab out of this bullet.

56
00:03:09,710 --> 00:03:15,260
So what we must do is we first need to drag this bullet in here.

57
00:03:15,260 --> 00:03:17,440
And that's it.

58
00:03:17,435 --> 00:03:20,285
That is how we create a prefab.

59
00:03:20,285 --> 00:03:23,845
So now what we can do is we can simply drag

60
00:03:23,840 --> 00:03:27,590
this bullet right here and create multiple bullets.

61
00:03:27,590 --> 00:03:29,530
And you can see that bullet three,

62
00:03:29,525 --> 00:03:34,055
Two bullet 1 all have the same components in the inspector.

63
00:03:34,055 --> 00:03:39,545
And you can know that a game object is a prefab by looking at its scholar.

64
00:03:39,545 --> 00:03:42,145
So you can see the background isn't a prefab,

65
00:03:42,140 --> 00:03:46,640
but the player bullet is because it is a bit blue.

66
00:03:46,640 --> 00:03:47,900
So let's say for example,

67
00:03:47,900 --> 00:03:50,770
I want to create a prefab out of the room player.

68
00:03:50,765 --> 00:03:51,595
There we go.

69
00:03:51,590 --> 00:03:57,520
So now the dump layer is a prefab and I can add another and a third one.

70
00:03:57,515 --> 00:03:59,575
And if I run the game,

71
00:03:59,570 --> 00:04:03,640
now we have three doom players walking around the a.

72
00:04:03,635 --> 00:04:05,405
How cool is that?

73
00:04:05,405 --> 00:04:08,515
There you go. We have a cool army.

74
00:04:08,510 --> 00:04:11,990
The Three Musketeers, we'll call them the three.

75
00:04:11,990 --> 00:04:14,810
I'm not sure. Okay. Musketeers, that's enough.

76
00:04:14,810 --> 00:04:19,330
Let's go ahead and delete all the bullets that we have.

77
00:04:19,325 --> 00:04:23,935
We can even delete this original one and we still can add it from our prefabs.

78
00:04:23,930 --> 00:04:28,240
And let's keep the player right here as a prefab.

79
00:04:28,235 --> 00:04:29,645
So very nice.

80
00:04:29,645 --> 00:04:36,235
Now we have everything almost set up and we can start creating, firing the bullets.

81
00:04:36,230 --> 00:04:40,670
So let's go into the player controller and start doing just that.

82
00:04:40,670 --> 00:04:46,160
Now, I'm going to be firing these bullets from the player controller.

83
00:04:46,160 --> 00:04:51,440
But keep in mind later on we will need another script to do the firing for us.

84
00:04:51,440 --> 00:04:53,800
Just a heads up from now.

85
00:04:53,795 --> 00:04:59,545
First of all, I'm going to create a serialized field which is of type game object.

86
00:04:59,540 --> 00:05:02,500
And this will be the reference to the bullet,

87
00:05:02,495 --> 00:05:04,295
or we can call it playable it.

88
00:05:04,295 --> 00:05:05,425
No worries. For now,

89
00:05:05,420 --> 00:05:06,970
let's keep it as bullet.

90
00:05:06,965 --> 00:05:11,105
We are going to need a reference to something called the

91
00:05:11,105 --> 00:05:17,105
transform or the transform fire position.

92
00:05:17,105 --> 00:05:18,635
And why do we need that?

93
00:05:18,635 --> 00:05:22,075
Well, where will our bullet come out from?

94
00:05:22,070 --> 00:05:25,430
So obviously it will come out from our shotgun.

95
00:05:25,430 --> 00:05:28,360
And where is the firing point of the shotgun?

96
00:05:28,355 --> 00:05:30,825
Should it come out from the middle of the shotgun,

97
00:05:30,829 --> 00:05:32,379
that wouldn't be too nice.

98
00:05:32,375 --> 00:05:35,635
What we'll do is we'll add a fire point.

99
00:05:35,630 --> 00:05:38,690
And this fire point will be an empty game object,

100
00:05:38,690 --> 00:05:41,450
which will be a child of the weapons are.

101
00:05:41,450 --> 00:05:46,090
So fire point.

102
00:05:46,085 --> 00:05:48,385
And notice what happens now,

103
00:05:48,380 --> 00:05:51,970
because they don't layer is a prefab.

104
00:05:51,965 --> 00:05:55,765
You will notice that the file bond has a little plus on it.

105
00:05:55,760 --> 00:05:57,010
What does that mean?

106
00:05:57,005 --> 00:05:59,285
If I click on the top layer here,

107
00:05:59,285 --> 00:06:02,645
I can double-click on him and open him up separately.

108
00:06:02,645 --> 00:06:04,405
You'll notice that he does not have

109
00:06:04,400 --> 00:06:08,240
the fire point because the prefab hasn't been adjusted yet.

110
00:06:08,240 --> 00:06:12,410
And you can see it from the color of the fire point and the from the little plus.

111
00:06:12,410 --> 00:06:14,560
So we can go into the do player.

112
00:06:14,555 --> 00:06:16,435
In here we have the override.

113
00:06:16,430 --> 00:06:17,720
If we click down,

114
00:06:17,720 --> 00:06:23,570
you'll notice that things that we've changed but have not applied yet to the prefab.

115
00:06:23,570 --> 00:06:28,960
And you can see that we can either apply the specific thing or we can, can apply all.

116
00:06:28,955 --> 00:06:31,315
So when we hit the apply all,

117
00:06:31,310 --> 00:06:33,740
now, the doom player,

118
00:06:33,740 --> 00:06:34,970
when we double-click on him,

119
00:06:34,970 --> 00:06:36,440
has the fire point,

120
00:06:36,440 --> 00:06:39,740
the prefab of the doom player has also the fire point.

121
00:06:39,740 --> 00:06:45,610
So we'll move the fire point here and front of the shotgun.

122
00:06:45,605 --> 00:06:49,615
But if we add another 11 of the dome players,

123
00:06:49,610 --> 00:06:52,990
you'll notice that the fire point is still in the middle,

124
00:06:52,985 --> 00:06:57,965
while the fire point on the doom player here is in front of the shotgun.

125
00:06:57,965 --> 00:07:03,295
Oh, and also, you can see when I click on the fire point, the transform,

126
00:07:03,290 --> 00:07:06,710
the position has this little blue line and this represents that

127
00:07:06,710 --> 00:07:10,460
we've changed something from the prefab.

128
00:07:10,460 --> 00:07:12,380
So I'll go into the doom player,

129
00:07:12,380 --> 00:07:14,720
click on override and apply all.

130
00:07:14,720 --> 00:07:16,960
But we don't always apply all.

131
00:07:16,955 --> 00:07:19,895
Keep that in mind. When I hit Apply all.

132
00:07:19,895 --> 00:07:22,525
If I go back to the doom player in here,

133
00:07:22,520 --> 00:07:28,210
I select the file pointer and it magically appeared on the end of the shotgun.

134
00:07:28,205 --> 00:07:33,745
So you are beginning to understand how useful a prefab can be

135
00:07:33,740 --> 00:07:39,490
when it comes to modifying multiple elements of prefab that we've created.

136
00:07:39,485 --> 00:07:42,055
Later on. As we move through the course,

137
00:07:42,050 --> 00:07:43,580
we make more complex things.

138
00:07:43,580 --> 00:07:46,220
You'll see how efficient the system is.

139
00:07:46,220 --> 00:07:50,890
For now we'll delete the doom player and let's continue on.

140
00:07:50,885 --> 00:07:53,135
So we have our fire point,

141
00:07:53,135 --> 00:07:54,655
we have our bullet.

142
00:07:54,650 --> 00:07:58,820
The next thing we want to do is we want to start firing 0.

143
00:07:58,820 --> 00:08:02,710
So I've added this in the wrong place, excuse me.

144
00:08:02,705 --> 00:08:06,775
We were working in the player bullet controller and we

145
00:08:06,770 --> 00:08:11,070
should have been working in the layer controller,

146
00:08:11,074 --> 00:08:12,664
not the player bullet controller.

147
00:08:12,665 --> 00:08:13,915
This is a bit confusing.

148
00:08:13,910 --> 00:08:16,360
I didn't notice the player bullet. No worries.

149
00:08:16,355 --> 00:08:18,175
We'll just Control X,

150
00:08:18,170 --> 00:08:20,170
which will cut them from here,

151
00:08:20,165 --> 00:08:22,135
and we'll paste them in here.

152
00:08:22,130 --> 00:08:23,600
Sorry for the confusion.

153
00:08:23,600 --> 00:08:28,390
I apologize for my lack of concentration.

154
00:08:28,385 --> 00:08:29,645
Save that.

155
00:08:29,645 --> 00:08:30,905
Go back.

156
00:08:30,905 --> 00:08:34,195
Now, we'll see that we have a reference to the bullet,

157
00:08:34,190 --> 00:08:37,360
a reference to the file position or fire point,

158
00:08:37,355 --> 00:08:40,385
that should be fire point.

159
00:08:42,100 --> 00:08:45,190
Save that back in our game.

160
00:08:45,185 --> 00:08:48,845
Please don't waste my time and nothing sticks.

161
00:08:48,845 --> 00:08:53,195
Fire point and the bullet will be the assets.

162
00:08:53,195 --> 00:08:54,985
Prefabs.

163
00:08:54,980 --> 00:08:56,960
Drag in the bullet.

164
00:08:56,960 --> 00:08:58,220
And there we go.

165
00:08:58,220 --> 00:09:01,190
We can also apply the changes and here.

166
00:09:01,190 --> 00:09:03,470
So what do we need to do?

167
00:09:03,470 --> 00:09:09,130
We want to, every single time when we press a button,

168
00:09:09,125 --> 00:09:12,385
which will be the left mouse key,

169
00:09:12,380 --> 00:09:15,040
we want to fire a bullet.

170
00:09:15,035 --> 00:09:16,825
So let's do that.

171
00:09:16,820 --> 00:09:19,430
We're going to create a condition and

172
00:09:19,430 --> 00:09:24,470
the update where we check if we press a certain mouse button.

173
00:09:24,470 --> 00:09:27,640
But how if we go into unit is documentations.

174
00:09:27,635 --> 00:09:31,765
We can see that we have an input to get the mouse button down.

175
00:09:31,760 --> 00:09:33,350
If we read the description,

176
00:09:33,349 --> 00:09:37,699
we can see that it returns a true during the frame the user

177
00:09:37,700 --> 00:09:42,310
pressed the given mouse button and we can even see an example in here.

178
00:09:42,305 --> 00:09:46,025
So if input dot get boss button down 0,

179
00:09:46,025 --> 00:09:48,565
we press the primary button,

180
00:09:48,560 --> 00:09:50,630
which will be the left mouse button,

181
00:09:50,630 --> 00:09:53,050
the one as d right mouse button.

182
00:09:53,045 --> 00:09:55,225
And the two is the middle,

183
00:09:55,220 --> 00:09:57,590
which is the little wheel.

184
00:09:57,590 --> 00:09:59,410
So back in here,

185
00:09:59,405 --> 00:10:02,825
I'm going to delete this debug dot log.

186
00:10:02,825 --> 00:10:14,245
I'm going to check if the input dot get mouse button down the 0.

187
00:10:14,240 --> 00:10:16,630
What are we going to do in here?

188
00:10:16,625 --> 00:10:21,205
Well, we want to do something called an instantiation.

189
00:10:21,200 --> 00:10:23,500
What is that? Back?

190
00:10:23,495 --> 00:10:29,585
And Unity's documentation, we can see that we can instantiate an object.

191
00:10:29,585 --> 00:10:31,175
Let's read the description.

192
00:10:31,175 --> 00:10:34,195
It returns an object that instantiated

193
00:10:34,190 --> 00:10:38,270
clothes or we create a clone of the prefab that we have.

194
00:10:38,270 --> 00:10:41,440
Clones the object original and returns the clone.

195
00:10:41,435 --> 00:10:45,415
This function can copy an object similar to the duplicate, etc.

196
00:10:45,410 --> 00:10:47,150
You can read all about it,

197
00:10:47,150 --> 00:10:48,980
and this is how we can use it.

198
00:10:48,980 --> 00:10:51,580
So we can simply instantiate the object,

199
00:10:51,575 --> 00:10:53,675
whatever it is without any addition.

200
00:10:53,675 --> 00:10:59,485
We can instantiate an object and make it a child of a certain parent that we want,

201
00:10:59,480 --> 00:11:01,090
which is very helpful.

202
00:11:01,085 --> 00:11:04,825
We can instantiated in a certain world and space.

203
00:11:04,820 --> 00:11:07,780
We can add position rotation and a parent.

204
00:11:07,775 --> 00:11:10,075
So all of these are very important.

205
00:11:10,070 --> 00:11:14,230
So now that you know how instantiation works,

206
00:11:14,225 --> 00:11:16,795
it's time for a challenge.

207
00:11:16,790 --> 00:11:21,130
And your challenge is going to be to instantiate the bullet prefabs.

208
00:11:21,125 --> 00:11:26,335
So check the example of instantiation in Unity's documentation.

209
00:11:26,330 --> 00:11:32,440
Objects should be or the original object should be the bullet prefab.

210
00:11:32,435 --> 00:11:36,295
Make sure the position is at the five position.

211
00:11:36,290 --> 00:11:40,340
The rotation is also at the five position.

212
00:11:40,340 --> 00:11:43,100
And I'll give you a small hint if you don't need it,

213
00:11:43,100 --> 00:11:44,480
you know how to create.

214
00:11:44,480 --> 00:11:46,090
Go ahead and do the challenge.

215
00:11:46,085 --> 00:11:51,475
If not, to access the position and rotation of the fire point,

216
00:11:51,470 --> 00:11:54,680
use the top position and the rotation.

217
00:11:54,680 --> 00:11:56,780
So pause the video right now.

218
00:11:56,780 --> 00:11:59,840
I know you don't know how to create instantiations yet,

219
00:11:59,840 --> 00:12:03,040
but this is a perfect time to test yourself,

220
00:12:03,035 --> 00:12:07,025
to go beyond your limits or what you think are your limits.

221
00:12:07,025 --> 00:12:08,795
So pause the video right now.

222
00:12:08,795 --> 00:12:11,645
Go to the Shalon.

223
00:12:11,645 --> 00:12:17,005
What we'll do is we're going to instantiate,

224
00:12:17,000 --> 00:12:18,950
will open up the brackets.

225
00:12:18,950 --> 00:12:22,180
And we can see right here all the options that we have.

226
00:12:22,175 --> 00:12:25,255
But we already know from the documentations how to use it.

227
00:12:25,250 --> 00:12:28,040
What is the original object that we'll use?

228
00:12:28,040 --> 00:12:31,670
It will be the bullet that we got the reference to.

229
00:12:31,670 --> 00:12:34,960
What is the position that we want to set this bullet add.

230
00:12:34,955 --> 00:12:39,475
It's going to be add the fire point dot position.

231
00:12:39,470 --> 00:12:42,020
And because we've added a position,

232
00:12:42,020 --> 00:12:49,030
we want to also add a rotation and it's simply going to be the fire point dot rotation.

233
00:12:49,025 --> 00:12:50,305
Save that.

234
00:12:50,300 --> 00:12:54,680
So now when we click the mouse button non-zero,

235
00:12:54,680 --> 00:12:57,650
we will instantiate the bullet,

236
00:12:57,650 --> 00:13:00,740
which we got a reference to at

237
00:13:00,740 --> 00:13:06,130
the fire point position that we've created with the far points rotation.

238
00:13:06,125 --> 00:13:09,595
Why are we specifying the fire point rotation?

239
00:13:09,590 --> 00:13:12,320
Because if I click on the far point,

240
00:13:12,320 --> 00:13:15,220
you can see how it's being rotated right here.

241
00:13:15,215 --> 00:13:23,245
If I rotate the arm right now because the fire point is a child of the weapons are,

242
00:13:23,240 --> 00:13:26,050
you'll notice now that this is the direction,

243
00:13:26,045 --> 00:13:28,195
even though the rotation is still 0.

244
00:13:28,190 --> 00:13:31,070
But the direction you can see right here,

245
00:13:31,070 --> 00:13:34,120
if you can see it changed this from global to local.

246
00:13:34,115 --> 00:13:38,005
You'll see how the fire point is directed.

247
00:13:38,000 --> 00:13:41,870
So I'm going to satisfy back to 0.

248
00:13:41,870 --> 00:13:45,220
And the rotations are sometimes weird.

249
00:13:45,215 --> 00:13:48,065
It's not about just 0. If we look at the position,

250
00:13:48,065 --> 00:13:50,425
if we change the rotation,

251
00:13:50,420 --> 00:13:52,730
you'll notice that the position also changes.

252
00:13:52,725 --> 00:13:54,255
It's a bit confusing.

253
00:13:54,250 --> 00:13:58,780
That's why we use usually quaternions rotations in Unity are very iffy.

254
00:13:58,780 --> 00:14:00,520
Anyways, we'll save that.

255
00:14:00,520 --> 00:14:06,600
We will run our game and now we can shoot our bullets.

256
00:14:06,595 --> 00:14:09,045
How cool is that?

257
00:14:09,040 --> 00:14:11,760
Look at how many bullets we have created.

258
00:14:11,755 --> 00:14:12,915
But as you can see,

259
00:14:12,910 --> 00:14:16,140
when we shoot a lot of bullets, they destroy themselves.

260
00:14:16,135 --> 00:14:18,235
So that's a no-no.

261
00:14:18,235 --> 00:14:19,855
So with that done,

262
00:14:19,855 --> 00:14:20,995
I hope you enjoy it.

263
00:14:20,995 --> 00:14:25,335
Make sure to commit the changes in the next video,

264
00:14:25,330 --> 00:14:27,340
we'll make our bullets far

265
00:14:27,340 --> 00:14:31,290
automatically without having to press the button every single time.

266
00:14:31,285 --> 00:14:32,785
Even though this is a shotgun,

267
00:14:32,785 --> 00:14:35,165
we'll make it an automatic shotgun.

268
00:14:35,160 --> 00:14:36,730
So commit the changes,

269
00:14:36,725 --> 00:14:40,235
and I'll see you in the next video.

