Welcome back, my fellow game developers.
In this video, we have learned everything about sorting layers.
We've also learned about parent and child relationship,
not as in regular parent-child,
as in the game object parent-child relationship.
As you can see right now,
the shotgun is rendered in front of the arm or behind the arm,
but they are all rendered in front of the player,
which is rendered in front of the background.
And he also has a small shadow.
So let's not waste any more time and let's get started.
Oh, case.
So let's get started.
The first thing we want to do is we want to create a player game object.
So we don't want to have this either one for now, we'll change it.
We don't want to have the sprite as the main object.
We want to be using a player game object that
has a body which is in turn the sprite render.
So I'm going to change this from idle to the body.
And I'm going to right-click in here,
create a new game object.
And I'm going to reset its position because currently you can see we created
this game object and its position is based on Where we are looking in the scene.
So you can see it has a certain acts,
a certain way, a certain Z.
What we can do is we can click on the X,
put it to 0, we can click on the Y,
but if the zeros at 200,
but that is a bit cumbersome at works.
But there is a much smoother and professional way.
By the way, I clicked controls that three times to revert all of these,
we can click on the three little buttons right here,
or three little points.
We can scroll down and we can see that we can reset
the position or we can reset the entire transport.
So for example, let's say I set a rotation of a 134.
I click in here, I can reset the position or reset the rotation,
or I can reset the entire game object.
So these are just little clues.
You can even see that the name of the game object has been reset.
So didn't week or week,
we didn't even name it.
I think.
Anyways, we're going to call this the doom layer,
which is also Ginny.
And we're going to move the body and set it on there, the doom player.
Okay, So what happened right there?
Let me just redo that.
Let's look at the body.
What is its position?
Its minus 5.60.89.
When we set it under the dome player,
we are making it a child.
So now you can see that if we move V,
don't play or left and right,
notice we are not moving the body.
We are moving the doom player game object.
You can see that the body,
which is this one right here, is moving also.
And you can see that we can change its position.
And the cool thing is,
let's look at the body again,
recently position of the body.
So the body is,
let's say add 0, 0, okay?
If we move the DOM player,
what will be the position of the body while it's still 00?
So now the position and rotation and scale and
everything is relative to the main game object,
which is the doom player.
But main game object is not the correct word.
It's the parent game object and the body becomes the child game object.
What does a parent-child relationship like?
Now, this is not a therapy session.
It's actually what's called a parent-child relationship in Unity.
So child will move,
rotate, and scale with the parent game object,
as we saw right now,
used for creating a body of APT objects that move together.
We'll see what I mean by a body of objects.
I mean not just the body of the player.
We will add an arm,
we will add a gun,
we will add a shadow.
And all of those will move or need to move with the player along as we play our game.
Children can also be parents of other objects.
You'll notice later on that we can have a child of a child.
So the child becomes a parent,
but it's still a child relative to the main parent.
So it's like a kind of a grandparent if you want, call it that.
Finally, the child transform properties are relative to the parent.
That means that as you saw when we change the position of the main parent game object,
the position of the body stays the same,
0, 0, 0 case.
So let's get back into Unity.
So now we have the body.
Next thing we're going to do is we're going to add a gun and the shadow.
So if we go into the resources.
The player right here,
you can see that we have an R and we have a shadow.
And this ONE belongs to the other doom player, not this one,
the one we are currently using to the other one that I've provided, you can use both.
I'm going to use this arm specifically
just so we always know the difference between the two.
It won't look as pretty,
but it's good for learning.
So I'm going to select the arm and the shadow,
and I'm going to drag them into the acids right here.
I'm going to put the shadow and you can a,
try doing this yourself.
So if I move the shadow under the dome player,
you'll notice that just by moving it as
a child of the dune player now we have the shadow on a different position.
I can reset it and I can put it right here,
but it's not there.
And even if I make it a bit bigger,
so I'm going to make this maybe eight instead of 100.
There you go. You can see the shadow is now in front.
But why does it sometimes have to be in front,
sometimes in the back?
What gives, how are we going to fix that?
We'll discuss this in just a bit.
Just let me add the arm right here.
There we go. Now we have the arm.
Let's set it as the child of the dome players at the shadow under it.
We're going to reset the position of the arm.
We're going to make the armed 32 apply.
So whereas the arm,
so you can see the arm.
There you go.
It's behind the player.
That's not good at all.
How are we going to fix this?
We're going to fix this using something called the sorting layers.
So currently, we can see that the body has a sprite renderer,
the arm has a sprite renderer,
and the shadow also has a sprite renderer.
And you can see right here in the sprite render
there is something called the additional settings,
which has something called the sorting layer and the order in layers.
Now, what does all of this do?
You can see that everything currently the background is on the default.
The body, the R and the shadow are all on the default,
and all are ordered in 0.
This is how unity determines which sprite should be in front of which other sprite,
especially when it comes to 2D games.
So currently you can see that the shadow is at 0 and it's behind the player.
But look closely right here.
Or let's do the arm.
If I get the R, it's still on the default as is the body.
But if I put it on the one, there we go.
Now it is in front of our player.
If I put it, Let's move it to the side.
If I put it on minus1,
you can see it disappears behind the background.
So let me set it back to one and let me set it on the plane.
Now, this is great,
everything is working good.
I can set the background to minus1 and assure that every time I add something,
it is in front or in the bank and the bank.
But there is a problem.
What happens when I have 100 objects or 200 objects of 5000 objects?
Do I have to set all of them?
Minus1 plus 1, 2,
3, 4 minus 100.
Obviously not.
What we need to do is we need to create a sorting layer.
And if I click right here on the background,
and if I click on the default,
you can see that I can add a sorting layer to our gain.
So let's click Add sorting layer.
We open up the layer.
As you can see, we have tags,
we have sorting layers and we have layers.
We'll delve into those in just a bit.
Let's focus on the sorting layer.
We can hit the little plus right here.
And we can create a new layer that will call the background.
So now you can see that background is rendered in front or behind the default.
So if I go back into the background,
this will make sense in just a bit.
I will set the sorting layer as the background.
And there you go.
You can see that it disappears.
Because let me go back.
The way we go back, we go to Layers,
the layers you can see here.
So layers, added layers.
You will get this inspector.
If I move the background behind the default,
you can see right now that the,
everything that was on the default layer, so the shadow,
the arm and the body are now rendered in front of it.
So back to layer.
At that layer you can see that this is how the hierarchy works.
And this is very important right here.
But as you can see, it makes life so much easier.
Now you have a challenge and your challenge
is going to be to create a layer sorting layer.
So we're going to create a new sorting layer called player.
Assign all the elements of the player to that layer.
Make sure that the player is rendered in front of
the background as we saw by changing the hierarchy in the sorting layer.
And finally, as an extra challenge,
if you're feeling up to it,
go to the Resources,
add a gun from their,
set it on the player,
and make sure that it's rendered between the player and the arm.
We want the arm to be in front of the gun,
but we want to gun to be also in front of the player,
but behind the gun.
So you'll need to play around with that.
If you need to create an extra layer, go for it.
You can create as many sorting layers as you want,
but make sure the true keep things so you don't
need to create a special layer for the arm and the shadow.
It's all the time on the player.
So don't worry about that.
With that said, pause the video right now and go do the challenge.
Okay, welcome back.
So how did you get almost that?
I hope you tried your best to do the challenge.
Always do your best with a challenge, even if it's hard,
even if you're not sure how to do it,
you can always go back and undo what you did.
You learn from the mistakes you've made.
Anyways, player layer, I'm going to select the body,
hold Control, select the arm and the shadow.
And I can change all of those.
And to the player layer,
I'm going to move the shadow to be right under the player right here.
Let's see.
There we go.
I can make the order layer.
Let's keep it at 0.
The body will be at one and the arm will be at two.
So the arm will always be rendered and front of the body of player.
Let's save the scene and let's go into our resources and get our guns.
So you can see right here in our resources we have the guns that's going here.
Let's go to the PNG.
I'm going to select the shotgun,
going to drag it into the assets right here.
I'm going to what end?
The dome player.
I'm going to change this through the shot gun.
It's extremely small.
I'm going to make sure that this is a ramp third,
to maybe apply the changes.
Whereas the shotgun 0 or who plucks big.
So I'm going to change this to maybe the gun That's create a sorting layer for the gun.
So all the weapons, weapons.
And it's going to be set that as the weapons. Okay.
It's a bit bigger than I would like.
Or we know because we are using only the guns on the player we are not going to create.
So there we go.
We will remove this.
We will keep the shotgun on the player layer.
And because the arm is on the two,
we'll set it on the three and the shotgun will be on the two right now.
So there you go.
But obviously, let's make this a child of the player.
And obviously we'll need to make the shotgun just a little bit smaller.
So 32, let's make it 50.
See how that looks.
Maybe too small.
Let's try. 40. Thanks. Forty will be good.
Still at that big.
Maybe let's make it 40 to 40 to apply that.
Yep, I think that's shotgun is looking good enough.
So there we go,
save all of that, run the game.
And there we go.
So now we have the background, the player,
we have a shadow which is a very good touch by the way,
it gives the game able of depth.
And we have a shotgun that is rendered in front of the player.
Now I think the shotgun would benefit from a bit of change in position,
but for now, I hope you enjoy it.
Let's go and never forget too.
Metal changes.
So you can see we are going to stage all of this,
but we do have a couple of things that we would not want to change.
So for example, some of the PNGs or the images that take up a lot of space.
For now we will keep them,
but later on you'll see that we don't have to stage the images.
We can just remove them.
They take up a lot of space, unnecessary space.
So we are going for now,
we'll stage all of that, commit the changes.
What did we do?
We created a player and learned sorting layers.
It's very important to always name your commits to
something that you might remember later on.
So for example, now we know this was the initial commit and
the second commit we created a player and learned sorting layer.
We could have added a gun,
change the sorting layer of the guns, etc, etc.
So I hope you enjoy it and I'll see you in the next video where we will be
actually adding a script to the player and start changing his behavior.
So exciting, I'll see you then.