Welcome back my fellow game developers to a brand new video.
And when we run the game,
you can see that we have a bullet that travels through
space and when it hits a wall, it gets destroyed.
So let's not waste any more time.
And let's get started.
Okay, so let's go ahead and create some bullets.
The first thing we need to do is we need to get some bullets sprite.
Let's go ahead and do just that.
I'm going to go into the project.
And before I do those things,
I want to change a couple of things in here.
So I'm going to right-click create a folder.
I'm going to call this the skirts.
And I'm going to add the player controller.
And here I'm going to create a folder for the first time now the sprites.
And I'm going to add all of the sprites that we have into this folder.
Or even before that,
I'm going to create a folder for the player and a folder for the guns.
Guns. There we go.
So now I can open up the sprites in here.
Let's take the gun and put it here.
Let's take the price of the player and put them in the player folder. Great.
Now we have everything set up.
We have this little small error.
I don't think it's a major problem.
Okay, Great.
Now let's go ahead and create another folder in here which will be the bullets.
And let's go into the resources and get the sprites for the laser bullets.
Now, you might be wondering,
why did I choose to have laser bullets and not bullets for a regular shotgun?
Well, first of all, look at these bullets,
how cool they look.
That's 4142. I wanted something to appear easily.
So as you can see, we are,
Let's set it on the player layer,
okay, So I want something to be clearly visible to us.
I don't want to have a small little bullet that we can barely see.
I want to have a big shiny bullet that we can fire.
So if you want, we can create a sorting layer for the bullets.
Let's go ahead and do that.
A new sorting layer,
we'll call this the bullets and it should be rendering in front of the player.
I'm going to choose this.
I'm going to rename it to the player bullet.
And I just chose this by random.
You can choose any bullet that you want.
I think maybe a red one as a shotgun.
Know. Even better than this.
Look what we can do.
I can choose the player bullet and an I can change the sprite in here,
drag the number 14 over.
There we go. So it's a huge bullet.
Maybe it could be changed a little bit,
so the bullet renders in front of the player.
And I do think maybe making it 0.50.5.
No, maybe keeping it one.
Yep. That looks like a big shot gun bullet.
Okay, great.
Now we need to add a couple of things to this.
We need to, first of all add a rigid body because we are going to be,
What's that, Sorry, room.
We're going to add a rigid body because obviously this bullet will be moving around.
We are going to make sure that we freeze the rotation.
We are going to make sure that the gravity scale 0.
And there we go.
Then we are going to add a collider to this.
So I'm going to add a collider.
Let's see which one should we use?
I think the capsule collider won't work because it's inverted,
culminated in the horizontal.
Let's go ahead and use a box collider.
I think it's good enough.
So where's the box collider?
Let's make it a bit smaller.
Yep. There we go.
And I always recommend you make the box
colliders around things like bullets a bit smaller than they have to be.
So there we go.
Oh, okay. That's good enough, I believe.
And now we need to change the behavior of the bullet.
We want to make it fly.
How do we do that? We do that by adding a script to it.
So I'm going to go into our scripts in here.
I'm going to right-click and create a new script,
which is going to be the player bullet controller.
Let's wait for this to compile dot immediately open a script whenever you create one,
just wait for it to compile.
And now we can go ahead and drag the script onto the player bullet.
And we can see in the player bullet we have the sprite renderer via rigid body,
the box collider, and of course the player bullet controller script.
So let's double-click and open the script up.
So we have a brand new script.
What shall we do in here?
Well, first of all,
we want a way to control the speed of this bullet.
So I'm going to create a serialized field of type float,
which is the bullet speed.
And let's just set it by default 25 F because we never want to have a default of 0.
So 55, but the speed of the bullet.
So if it's low, but it's better than nothing.
And the next thing we want to do is we want to add velocity to this bullet.
So in order to do that,
we need a reference to the rigid body.
And I don't feel like doing this,
so I want to give it to you as a challenge,
you will need to get a reference to the rigid body.
So create a variable of type rigid body makes sure it's private.
And start get the component of the type rigid body.
If you already know how to do this,
pause the video and go do the challenge.
If not, I will give you a small hint on how to get a component.
Use get component with a type in between the two little bigger or smaller signs.
So pause the video right now and go do the challenge.
Okay, welcome back.
So in here I'm going to create a private rigid body 2D
and the bullet rigid body.
Save that.
And now, because we have
the player bullet controller on a game object that also has the rigid body.
We can immediately access it by saying that the bullets
rigid body is equal to the debt component, rigid body 2D.
And now we have a reference to it.
Now and update what are we going to do?
Well, just like we did with the player,
I'm going to go ahead and say that
the bullets rigid body velocity is equal to the new vector 2,
because velocity is a vector 2.
When we are working into the space,
I'm going to keep one for the x.
And I'm going to keep 0 F as the y.
And I'm going to multiply it by the bullet speed.
Why am I doing this?
Well, because I don't want the bullet to move in the y axis.
I don't want it to move vertically.
I want to move the bullet only horizontally for now.
So let's save that.
Okay, so let's go back into Unity and let's zoom out a
bit and let's make the speed the two so we can clearly see what's happening.
Let's move the employer and the bullet back and run our game.
So there we go,
you can see that the bullet is traveling through space and when it hits,
so all, it stops completely, so great.
Our bullet is moving perfectly. We can split it up.
We can do whatever we want.
But what happens, for example,
if the book is diagonal when we run our game?
Let's see what happens.
Yep, so that's clearly not how a bullet is supposed to be moving.
What we want to do is we want the bullet to always be
moving in the direction that we've set to it.
So how are we going to do this?
Let's say, for example,
we have our shotgun right here,
our arm, and it's rotated up.
Our bullet will also be rotated upwards.
So if we move it,
It's something similar to that.
But if we click here,
up, up here, you can see the center and the global.
This is for the rotation,
this is for the,
well, how the axes are represented.
So if I click on the local,
you can see this is the direction that the bullet is currently looking at.
So this is how the bullet is going to come out and fly.
And you can see that it's the red,
which is the x axis,
is looking up and the direction of the bullet.
So whenever we rotate our shotgun,
we want the bullet to rotate it with it also.
So we wanted to fly always in the direction of the x.
So how are we going to do this?
Well, thankfully, there is a vector that is called the transform dot, right?
And let me show you what that is.
Oops, sorry, that's for later.
So transform that right is the red axis of the transport in the world space.
So we can access this axis whenever we want.
And that's the direction that we are going to be making our bullet flying.
So back in our code and here I am going to remove the new vector two.
And I'm going to instead.
Say that the direction will be transformed dot, right?
So now every single time and the update,
when the bullet is flying,
it's flying at the speed of the bullet speed that we
determined and in the direction of the transform.py,
right, which is the red axis.
Let's test this out.
So if I run the game right now,
the bullet is moving fine.
Great.
Let's see if we save that.
And when the bullet is pointing upwards,
if I run the game, yep,
there we go, the bullet is moving up.
How cool is that?
Great.
Now, one more thing.
This is not how able it behaves.
Usually a bullet Whenever it's like this.
Let me just stretching it.
Well, it works.
Yep. There you go.
It's just loved the building and it will go on forever until eternity.
And that's a problem.
Whenever you have a bullet that does not get destroyed at some point when it hits a wall.
This is what we need to fix. That's the first thing.
The second thing, a bullet doesn't just hit a wall and stay there.
A bullet should be able to travel through a wall.
Maybe, maybe traveled through a wall.
So we are going to click on the players bullet.
We are going to go down and to the collider and we are going to make it a trigger.
So what is a trigger?
A trigger whether the collider behaves as a trigger or not,
that's not very helpful.
A trigger means that if this bullet hits a kind of surface,
it does not impact it physically.
But it gives us the ability to call something called cause,
something called That's very smart.
And on Trigger Enter to the,
so what this does,
you can read the description sent when a another object enters
a triggered collider attached to this objects to the physics only.
So what happens if this bullet is a trigger as it travels through space?
When it hits the wall in here,
which is on the background.
When it hits this wall,
a sugar enter method will be called,
which will create on the bullet players bullet controller and wariness gold,
we can go ahead and do anything we want.
And one of those things is destroy the bullet.
Let me demonstrate.
That's a lot of talking, not enough action.
Let's see what happens.
I'm going to write on Trigger,
Enter 2D, hit the Tab key.
So as you can see, it's a private void.
Don't worry if you don't know what that word is.
Later on we will explain everything and how we can create methods.
Don't worry. And then we have the Onsager enter
and we have something in here that's called a parameter.
We'll also delve deeper into it.
But all that you need to know that this collision is the object that we just hit.
So for example, if this bullet flies and hits the player,
the player is the collision right here.
But anyways, what do we want to do when the bullet hits something?
Well, we want this bullet to destroy itself.
We want this bullet to disappear.
We don't longer needed in our world.
There is a very good method of doing this,
would simply write destroy the game object, and that's it.
So we destroy what should the steroids any or removes a game object, component or asset?
And what do we want to remove?
We want to remove the actual game object.
And if you hover over it,
you can read the game object.
This component is attached to a components of a game object.
So let's save that.
Let's go back into Unity,
that set doublet right here.
And now when the bullet hits any one of the colliders,
it should disappear from our scene.
Let's run the game.
Keep your eyes on the scene view. There you go.
It gets steroid.
Did you notice in the hierarchy that we no longer have the bullet?
Excuse me, that's an annoying update.
Let's try this again without any updates.
That's tried to set the bullet right here.
Run the game, keep your eye on the hierarchy when it hits,
so all the bullet disappears.
So now when ever we enter a zone,
bullet gets destroyed because it is a trigger,
which means it triggers the Onsager
enter method whenever it actually hits any kind of wall,
you can read more about it in here in the Unity documentation.
So I hope you enjoyed creating your brand new bullet.
I hope it doesn't bother you that the bullets are shiny and very
futuristic at spot of the quirk of the game that we're creating.
And we're also using free sprites.
So we have to work around the limitations that we have.
The next video, we will actually be shooting
these bullets from the gun by learning what brief up is.
Never forget to commit your changes.
I hope you enjoy it and I'll see you in the next video.
And before I go by the way,
there is something that we do not want to stage and our game,
which are the sprites that we've just added.
So what we'll do, because this is not necessary staging, committing the sprites.
What I'm going to do is I'm going to go in here.
I'm going to right-click and I'm going to
ignore and ignore everything beneath assets, sprites.
We do not want to save anything that is inside of the sprites.
Because one, we will always have the sprites on our computer to,
we might never have deleted the sprites by mistake.
And even if we do, we can always put them back.
And 3, they take up a lot of unnecessary space.
So ignore everything under the assets sprites hit Okay,
Everything should be gone,
only the bullets and so on and so forth.
So I hope you enjoyed stage everything and I'll see you in the next video.