Welcome back guys.
Our here from Magic coating and this with you we're going to starts shooting some bullets.
So if you remember in the last video we aimed.
Now we're going to shoot.
So to do that we will have to create something that is called a prefab out of the bullet.
So let's first fall create the bullet itself and then we're going to add a prefab set of good sprites.
Then here we have a bullet.
We can just drag it on.
Make the player.
And where is it.
It's up here.
And this is not our bullet.
So just delete this.
Delete it may have been the gun.
Nope.
OK.
This will be our circle so circle sir.
Girl I can at this but this is too much.
So what are we going to do.
We're going to see point one by point one and then I can leave it at one.
This will be our bullet.
Now our bullet will be black color.
Then we're going to add a circle Collider which will be a tiny bit bigger than it is just a tiny bit.
So it doesn't actually passes through something that it should not pass.
It's going to have a rigid body.
It's going to have a rigid body 2D dynamic mass one gravity scale will be zero.
So we will not be affected by gravity.
But it can be applied some force.
We can apply some force to it.
It's going to have a travel render that we're going to add later but that's for now.
This this should this should do it.
What's a prefab.
Let's say for example I want this bullet to be added inside my player.
It can say I can just add it.
Yeah.
That's cool.
But once this bullet disappears I have no stored bullets.
And that is what a prefab does.
If I create here follow college prefabs we're not going to have much prefab so I can just add it here.
I can delete this and if I go ahead and again tried to create the bullet I would have to add everything
one more time.
So rigid by the Sprite Renderer circa collider and so on.
What a prefab does it stores all of the values everything that you had inside of it.
And then I can just bam put it here and then I have my circle with scale.
Point one point one with the circle Collider and the Richard body.
That is why and why we need precepts.
So let's go ahead to the player and the bullet as you can see right now it's missing because we delete
that I can just add my bullet and open up my player controller here inside of it.
We're going to go to the shoot function first of all as you can see here we're going to see that our
line dot renderer enabled equals false after death we're going to say.
Game Object be standing for bullet evil teams tense shape.
And we're going to instantiate our lit bullet at r 4 at our fire position one top position and then
here we are going to say what turning in that identity.
And then add some color.
After death we're going to say if our transform DOD local scale low color scale is greater than zero
then we're we're going to see it or be DOD get component we're getting region body 2D or close parentheses
dot add force.
Now here it's giving an ever because I have to say that X not the local scale add force and here we're
adding five position dot right multiplied by the bullet speed and we're going to multiply that multiply
but we're going to see force move to keep that impulse and add assembly column to it now.
Else if it's smaller than zero we're going to copy this and add and negative.
Right.
So negative right means left after death.
We're going to destroy our bullet after two seconds add the semicolon and that'll be it for now that
will be it for now for the shoot.
So what are in it.
We're setting our game object B would sense for bullet.
We're creating in temporary bullet which will be our bullet where we're going to see point at five position
one position.
Then we are checking the temporary bullet if our transfer is done locals care is greater than zero.
If it is we're firing 10 bullets off of the right fired position.
If we are not we are firing firing it off on the left side and we're going and destroyed it after two
seconds because nobody wants the game objects that you're not using inside of the game because later
on if you have too many of those objects it can hurt the performance of the game and nobody nobody wants
that.
If I go here and click on the player they're left armed and fire position.
So no matter where I move my left arm my fire position will be right to decide but actually inside when
I start shooting that means just going to the right which means forward to if I'm aiming this right
here.
Then we're going There's the bullet will go this way.
And if my player is at minus one it's great that way.
So that is why we're taking the locals key local scale because if we do not change the local scale it's
going to go in some weird ways.
If I hold down I get to Bam it goes and it's too quick.
As you can see here it's too quick and it's too big.
So first fault.
Let's go ahead and change the player speed.
I mean the bullet speed.
That's a 20.
And save it and hit play again and then bam.
Yeah.
That looks more better now I can release it.
Hold on a pause and I did not released on time so at least pause we can see that our bullet is kinda
too big for our gun so we're going to actually kill it now.
Let's say zero point zero five and then zero point zero five.
Now this is too tiny.
So let's maybe add point eight.
So with this we're getting tested out of point eight.
That looks good.
Let's remove it.
Go to the prefab go to bullet here zero point eight zero point eight.
Save the prefab.
Click play and then bam.
What what what what.
Okay.
Zero point zero like my zero point zero eight and ten zero point zero eight.
Once again I was like What is this.
Pull it.
Yep this works right now.
This works.
This can work.
I mean it is still kind of bigger but as long as we say that our gun is in front of our bullet that
should not matter that much.
So let me check one time then it's going.
Is it like right now we're checking everything we're checking.
Where is it's pointing.
So let me check here.
It's pointing correctly I guess.
They make this at 90 and then you have to aim it at 90.
Can I get up up up up up up up.
We're just testing right now and hit.
Okay I messed up so right now I'm just trying to pause the game while having my trajectory on.
Why.
Well right now we can see that our bullet is going at the middle meaning that it's pointing at the right
position and it's going right where it needs to go.
So we are pretty much done with our bullets.
If I check it right here it's pointing right in the middle.
Now as I said if you want you can scale it down a bit more so maybe point seven point seven.
So it looks more feasible for the eye.
But I will not bother that much.
Twenty eight point eight should do it.
Now one more thing is we do not have any colitis for the ground so we might as well added so box collider
2D and collider make it here.
Then bam here hold on.
Turn off key and then add it like this.
Now what's the problem is that our bullet is not bouncing.
So if I added it sub boxing it just going and sliding off to add the bounce of the ball as we will have
to create a separate material to create a physics material not a regular one says it continues to D
friction will be zero and the ball says will be one and then I can go to my bullet hole called his bones
and go to the prefab go to the bullet and add the bones to the bullet and right now I press play.
You can see that my bullet will start will start bouncing off bam.
And then then then it looks good.
One more thing is that our left arm should be at number two should be in front of the gun.
Not behind a gun.
And the last thing that we will add will be the trail for our bullet.
So let me go ahead to the bullet open it up as here a trail renderer.
I'm gonna make this as low as it can be so this is not our trail run there looks like the material.
It will be a default line.
It will be white.
I'm going to scale it down as much as I can about here.
Maybe let's add here what's the number for it.
Zero point zero six starts at year zero point as 0 1 I guess no zero point you want should be zero point
one because right now here I can easily choose zero points 3 or what ever we had so this I can add one
more to it and make it go down a bit.
Something like this again low down low go lower down and let me check right now.
This is what we have for the time.
I'm going to say point 3 because R bullet is too fast it's really too fast so this will be its trail
renderer some not liking it that much so I might as well change a bit and be here let me see how I did
it.
Okay.
That way I did was like this.
I think it was like this.
Let me check it one more time.
And yeah yeah that should do it.
Maybe a bit maybe here and then again.
Ticket.
Yeah.
They should do it.
No like particularly.
What should I do onto this.
But this looks great.
I like it and I'm going to keep it.
As for the color we're going to make it fade away so the color would be white.
But here let's see here for example we are going to have the same color but up here it's actually gonna
fade away a bit by a bit here is gonna be completely faded but over here it's gonna be maybe not so
much tiny bit one on one and then do we go.
So right now when I move it This is what we have.
So again zero zero save it quickly.
And now we can check and it's not working I save it all right.
I shoot down the bullet.
I pause it.
C'mon shoot on the bullet was it bullet.
It has a trailer render but it's not working.
And that's because it's behind everything we have to add the sprite.
Where is it.
The surprise surprise surprise sprite
the sprite.
I mean sorting layer and layer.
And while we had it so let's go ahead back fact the bullet here.
Choose the player at play and then bam bam bam bam bam bam zero point three it's kind of too much for
the time of zero point maybe 15 hit play once again Bam Bam yeah looks better looks better point you
should do it and we can also lower it slowed down a bit our bullet maybe 17 so we can actually see it
better.
So a tiny bit better yep.
I like it.
I like the way it looks so yeah.
Guys they'll be it for our bullet.
Hope you'll like it and I will see you in the next video.