Welcome back my fellow game developers to a brand new ultra exciting video.
We've created bullets previously,
we made them fly,
but now using the power of brief up,
the power of input,
the power of instantiation.
I mean, we've learned so much in this video,
we can start shooting bullets in various directions.
How cool is that?
As you can see, we have multiple bullets shooting from our shotgun.
So let's not waste any more time.
And let's get started.
Oh, okay, so time to start firing hour bullets.
But we want multiple bullets to be fired.
We don't just want to have one bullet.
So what shall we do?
Shall we duplicate this bullet multiple times?
By the way, I'm doing this using Control D. Well,
obviously not, this is not a great idea.
What we need to do is we need to create something called a prefab.
What is a prefix?
Let me show you.
A prefab basically means it's
a previously fabricated game object that we can use multiple times.
There are a lot,
a lot of benefits to having prefabs when it
comes to create new scenes and doing different things.
Let's go over a couple of important things about prefabs.
It's a template for a certain game object.
So just like we have the bullet with its specific components at specific collider,
we can create a prefab out of it and create multiple bullets with the same components,
with the same sprites,
the same collider, et cetera.
Prefab system allows you to store a game object with all its properties.
So just like I said,
you can create a prefab of the bullet and then
create multiple bullets with the same properties,
game components, et cetera.
What do we use it for?
We can reuse a game object configured in a particular way.
Hence the bullet we can create multiple, but let's see,
we'll see how in just a bit we can edit many objects at once.
So for example, let's say we want to make the bullets faster.
Should we go and change all the bullets or every single time?
No, we can change only the prefab and every other form of this prefab will be changed.
We can use the same object in different scenes,
much easier when we start creating multiple levels.
You'll know what I mean by this, and that's it.
So let's go and create a prefab.
Now, the process of creating a Prefabs is extremely, extremely hard.
So be very, put your caps on,
stop everything, and watch what I do.
This is very important.
First of all, we create a folder which is the prefabs.
I'm going to go in here and now I'm going to create a prefab out of this bullet.
So what we must do is we first need to drag this bullet in here.
And that's it.
That is how we create a prefab.
So now what we can do is we can simply drag
this bullet right here and create multiple bullets.
And you can see that bullet three,
Two bullet 1 all have the same components in the inspector.
And you can know that a game object is a prefab by looking at its scholar.
So you can see the background isn't a prefab,
but the player bullet is because it is a bit blue.
So let's say for example,
I want to create a prefab out of the room player.
There we go.
So now the dump layer is a prefab and I can add another and a third one.
And if I run the game,
now we have three doom players walking around the a.
How cool is that?
There you go. We have a cool army.
The Three Musketeers, we'll call them the three.
I'm not sure. Okay. Musketeers, that's enough.
Let's go ahead and delete all the bullets that we have.
We can even delete this original one and we still can add it from our prefabs.
And let's keep the player right here as a prefab.
So very nice.
Now we have everything almost set up and we can start creating, firing the bullets.
So let's go into the player controller and start doing just that.
Now, I'm going to be firing these bullets from the player controller.
But keep in mind later on we will need another script to do the firing for us.
Just a heads up from now.
First of all, I'm going to create a serialized field which is of type game object.
And this will be the reference to the bullet,
or we can call it playable it.
No worries. For now,
let's keep it as bullet.
We are going to need a reference to something called the
transform or the transform fire position.
And why do we need that?
Well, where will our bullet come out from?
So obviously it will come out from our shotgun.
And where is the firing point of the shotgun?
Should it come out from the middle of the shotgun,
that wouldn't be too nice.
What we'll do is we'll add a fire point.
And this fire point will be an empty game object,
which will be a child of the weapons are.
So fire point.
And notice what happens now,
because they don't layer is a prefab.
You will notice that the file bond has a little plus on it.
What does that mean?
If I click on the top layer here,
I can double-click on him and open him up separately.
You'll notice that he does not have
the fire point because the prefab hasn't been adjusted yet.
And you can see it from the color of the fire point and the from the little plus.
So we can go into the do player.
In here we have the override.
If we click down,
you'll notice that things that we've changed but have not applied yet to the prefab.
And you can see that we can either apply the specific thing or we can, can apply all.
So when we hit the apply all,
now, the doom player,
when we double-click on him,
has the fire point,
the prefab of the doom player has also the fire point.
So we'll move the fire point here and front of the shotgun.
But if we add another 11 of the dome players,
you'll notice that the fire point is still in the middle,
while the fire point on the doom player here is in front of the shotgun.
Oh, and also, you can see when I click on the fire point, the transform,
the position has this little blue line and this represents that
we've changed something from the prefab.
So I'll go into the doom player,
click on override and apply all.
But we don't always apply all.
Keep that in mind. When I hit Apply all.
If I go back to the doom player in here,
I select the file pointer and it magically appeared on the end of the shotgun.
So you are beginning to understand how useful a prefab can be
when it comes to modifying multiple elements of prefab that we've created.
Later on. As we move through the course,
we make more complex things.
You'll see how efficient the system is.
For now we'll delete the doom player and let's continue on.
So we have our fire point,
we have our bullet.
The next thing we want to do is we want to start firing 0.
So I've added this in the wrong place, excuse me.
We were working in the player bullet controller and we
should have been working in the layer controller,
not the player bullet controller.
This is a bit confusing.
I didn't notice the player bullet. No worries.
We'll just Control X,
which will cut them from here,
and we'll paste them in here.
Sorry for the confusion.
I apologize for my lack of concentration.
Save that.
Go back.
Now, we'll see that we have a reference to the bullet,
a reference to the file position or fire point,
that should be fire point.
Save that back in our game.
Please don't waste my time and nothing sticks.
Fire point and the bullet will be the assets.
Prefabs.
Drag in the bullet.
And there we go.
We can also apply the changes and here.
So what do we need to do?
We want to, every single time when we press a button,
which will be the left mouse key,
we want to fire a bullet.
So let's do that.
We're going to create a condition and
the update where we check if we press a certain mouse button.
But how if we go into unit is documentations.
We can see that we have an input to get the mouse button down.
If we read the description,
we can see that it returns a true during the frame the user
pressed the given mouse button and we can even see an example in here.
So if input dot get boss button down 0,
we press the primary button,
which will be the left mouse button,
the one as d right mouse button.
And the two is the middle,
which is the little wheel.
So back in here,
I'm going to delete this debug dot log.
I'm going to check if the input dot get mouse button down the 0.
What are we going to do in here?
Well, we want to do something called an instantiation.
What is that? Back?
And Unity's documentation, we can see that we can instantiate an object.
Let's read the description.
It returns an object that instantiated
clothes or we create a clone of the prefab that we have.
Clones the object original and returns the clone.
This function can copy an object similar to the duplicate, etc.
You can read all about it,
and this is how we can use it.
So we can simply instantiate the object,
whatever it is without any addition.
We can instantiate an object and make it a child of a certain parent that we want,
which is very helpful.
We can instantiated in a certain world and space.
We can add position rotation and a parent.
So all of these are very important.
So now that you know how instantiation works,
it's time for a challenge.
And your challenge is going to be to instantiate the bullet prefabs.
So check the example of instantiation in Unity's documentation.
Objects should be or the original object should be the bullet prefab.
Make sure the position is at the five position.
The rotation is also at the five position.
And I'll give you a small hint if you don't need it,
you know how to create.
Go ahead and do the challenge.
If not, to access the position and rotation of the fire point,
use the top position and the rotation.
So pause the video right now.
I know you don't know how to create instantiations yet,
but this is a perfect time to test yourself,
to go beyond your limits or what you think are your limits.
So pause the video right now.
Go to the Shalon.
What we'll do is we're going to instantiate,
will open up the brackets.
And we can see right here all the options that we have.
But we already know from the documentations how to use it.
What is the original object that we'll use?
It will be the bullet that we got the reference to.
What is the position that we want to set this bullet add.
It's going to be add the fire point dot position.
And because we've added a position,
we want to also add a rotation and it's simply going to be the fire point dot rotation.
Save that.
So now when we click the mouse button non-zero,
we will instantiate the bullet,
which we got a reference to at
the fire point position that we've created with the far points rotation.
Why are we specifying the fire point rotation?
Because if I click on the far point,
you can see how it's being rotated right here.
If I rotate the arm right now because the fire point is a child of the weapons are,
you'll notice now that this is the direction,
even though the rotation is still 0.
But the direction you can see right here,
if you can see it changed this from global to local.
You'll see how the fire point is directed.
So I'm going to satisfy back to 0.
And the rotations are sometimes weird.
It's not about just 0. If we look at the position,
if we change the rotation,
you'll notice that the position also changes.
It's a bit confusing.
That's why we use usually quaternions rotations in Unity are very iffy.
Anyways, we'll save that.
We will run our game and now we can shoot our bullets.
How cool is that?
Look at how many bullets we have created.
But as you can see,
when we shoot a lot of bullets, they destroy themselves.
So that's a no-no.
So with that done,
I hope you enjoy it.
Make sure to commit the changes in the next video,
we'll make our bullets far
automatically without having to press the button every single time.
Even though this is a shotgun,
we'll make it an automatic shotgun.
So commit the changes,
and I'll see you in the next video.