Welcome back, My favorite game developers in this video,
when we set the time between shots to 0.1 and we start shooting our birds.
You'll notice that even though they are on top of each other,
they do not destroy themselves,
but they still get destroyed when they hit any wall.
Not only that, if I have a lot of bullets being fired and walk the player through them,
you'll notice that the player also,
or the bullets and the player not interact with each other and
they don't get destroyed upon hitting the layer,
which is a good mechanic.
We don't want the player to use the bullet to be able to kill themselves.
This is a game mechanic.
So with that said,
let's not waste any more time,
and let's get started by learning layer and physics 2D matrix k.
So now when we run our game,
you'll see that we can fire our books.
But the problem is if we try to press it multiple
times or if we set the time between shots,
for example, 0.1, you'll see that we don't see any bullets flying away.
Why?
Because notice on the left,
we have the bullets firing and destroying
themselves because they are interacting with each other.
And we don't want that.
We want to fix this problem.
How are we going to do this?
Now before we continue on,
maybe it's something that you do want to keep in your game.
But honestly for me,
it's kind of annoying having your bullets be destroyed.
So again, how are we going to fix this?
Well, if we look right here on the right and the inspector of any game object,
we can see that we have something called the layer.
And I've showed you this previously when we were talking about sorting layers,
but we haven't delved deep into it.
So if I go back to layers,
you can see that I have tags which we will talk about In the future.
We have the sorting layers which determine which Sprite
or which layer renders in front of which other layer.
And we also have the layers.
And you can see that we already have the default
transparent affects, ignore re-cast water.
You are all of these different layers that are already present in Unity.
So what we're going to do is we are going to set the bullets on a certain layer.
You might be asking, but why should we be setting these bullets on a certain layer?
Well, let me show you by first creating the player bullets layer.
Now, we want to go into the projects.
Go into our where are the prefabs, the pleasurable it?
And let's just set the player bullet on the player bullets layer.
Okay, So this was the prefab.
So all subsequent creation or
clones of this prefab will also be on the player bullet layer.
But still, how is this helping us?
Well, if we go into our edit,
into our project settings,
and if we go into the physics 2D and scroll down,
we can see right here this layer collision matrix.
And if you, sorry,
if you tilt your head,
you can see, let me maximize this.
You can see that we have the default transparent re-cast player bullets,
water UI, and on this side also.
So how does this matrix work?
Let's say we have the default layer,
which was previously the bullets layer.
And we can see that it also interacts right here with the default layer on this side.
So what we can do is we can actually turn this off.
So now the default layer does not interact with the other default layers.
But obviously we don't want that to happen because whenever you create a game object,
you will have it as default.
And maybe we do want some of the default
game objects interacting with other default game objects.
And that's why we created the player bullet.
What we can do is we can say that the player bullet,
which is right here,
does not interact with the player bullet.
So we turn this off.
And now the player bullets should not destroy
each other because they will not be able to interact with each other.
Let's test this out.
I'm going to go back.
I'm going to run the game.
And I'm going to go to the DOM player and okay,
so the time between shots will be 0.1.
And let's try to hold down the mouse button.
Now when I hold down the mouse button, there you go.
You can see that the bullets,
even though they are on top of each other,
are not destroying themselves,
but they are still getting destroyed when they hit a wall.
So you can see that clearly when we shoot towards the wall,
but they are on top of each other and they do not destroy themselves.
How cool is that?
We have this powerful tool in our arsenal now where we can set
different objects to different layers
and make sure that they don't interact with each other.
Now, another thing that we might want to change is the player bullets hitting the player.
Again.
I tell you, if you do want to keep this mechanic in your game,
feel free to do it.
If you don't, we are going to change the way bullets interact with the player.
Now, currently, the player capsule collider is very small.
What we can do is we can add another capsule collider.
We will have to later on because what happens if one of the enemies shoots a bullet
at our head and we don't have a collider
there that does not register that we've been shot.
We will fix that.
But for now, let's just keep things simple and try to hit the player on his legs.
So I'm going to run the game.
I'm going to shoot a bullet.
And if I stop there,
you can see that it hits the player and can kill him. Again.
You want to keep those mechanics.
You want the player bullets to help the player.
That's okay.
But you will have to this challenge.
You'll need to make sure that the player and the bullets won't interact.
So create a layer, layer.
Assign the player to that layer,
and make sure that the player and the player bullets
do not interact through the physics matrix.
So that said, pause the video right now,
go create the player layer.
Make sure the bullets don't kill our player.
Okay, welcome back.
So how did you get on with that?
Before I continue on,
I just want to do something.
I told you I wouldn't want to do it,
but let's do it for now.
I'm going to add a box collider to the player.
This will not be where the player interacts.
This will be just a box collider to determine that the player is being hit.
So I'm going to make it around the size of the player and I'm going to make it
a trigger because I don't want this box collider to interact with the world physically.
I want to keep this box collider as a registered to if
someone has deep layer or some kind of layer.
So I'm going to save this.
I'm going to run the game.
Obviously, we'll need to override the prefab.
I'm going to shoot this and if I walked towards the bullet,
you can see that it hits the player.
So let's go ahead into the layers appear.
At layers, open the layers.
I am going to add the player layer.
And now I'm going to go into the doom player.
I'm going to go into the Layers and I'm going to set this layer to the player layer.
And you can see that when you change the layer of a parent, you get the option.
Do you want to set the player,
the player to player for all the child objects as well.
Would you want to do that or not?
I think it wouldn't harm if we set the children also.
So I'm going to set this,
I'm going to save this.
I'm going to go into the project settings, physics,
and I'm going to make sure that the player bullets and the player do not interact.
So you can see where there is an intersection between the player bullets.
The player. I'm going to make sure that there is no sign in here. There we go.
Now if we run the game,
the bullets should travel through the player.
And yep, there we go.
So we can shoot the bullets as many times as we want,
and they do not hit the player.
They keep floating until they hit some kind of enemy.
So I hope you enjoy,
I hope you like this new tool that you have.
We can also apply all of our changes to the prefab that we have of the dome player.
And in the next video,
you'll notice that we have these bullets,
okay, they are firing.
It's, everything is nice,
but when they hit a wall,
there is nothing that's happening.
The bullet is just disappearing and that's not very exciting.
We want to have some kind of impact,
some kind of effects,
or we can feel the weight of those bullets hitting a wall.
And that's exactly what we'll do in the next video by adding
the special effects to the bullet upon impact.
So don't forget to commit your changes.
And I'll see you in the next video.