Welcome back, My favorite game developers
in this video and this long video,
again, we are going to be learning how to create a chest.
But this is a really cool
just because when we get near it,
it tells us that we can press the e to open chest.
If we press the E, you'll see that we get an open chest.
There we go. So this is all that we've done.
So let's not waste any more time and let's get started.
Oh, okay, welcome back.
So now we know how we can pick up weapons.
We can add them easily to the player,
we can shoot with them.
We update the UI, everything is working perfectly,
but as we've said,
we want to add a bit more flavor
to the way we actually get these guns.
And the way we'll do that is having a chests
and our levels that we can click on.
They open up, they give us the weapon,
we pick it up, we continue on with our game.
So let's go ahead and do that.
I'm going to delete the three weapons pickup.
I'm going to go into the sprites and
before obviously before you delete them,
we need to have them as prefabs.
Then we'll go into the sprites.
We are going to right-click in here,
create a folder for the chest.
Double-click in here and go into
the visit into the resources, drag the chests.
Obviously, if you can find by their chests,
I recommend that you go for it.
I'm going to make it multiple.
I'm going to make it by point.
The filter mode go to this pride, apply obviously.
And now let's go ahead and slice this up.
I believe the automatic should work perfectly fine.
There we go, apply the changes.
Which box do you want to choose?
I'll choose the golden one.
So the, this one and this one when it opens up.
So we will need to have two kinds of sprites in here,
140, open chest 14, the closed one.
I'm going to drag and add it into the world right
here and set it on the player layer.
Obviously it's extremely small.
So I'm going to change this from the treasure chest
to the weapons chest,
and let's make it a bit bigger.
So I believe 16 maybe 16 is too big.
Is too big of a chest.
Nope, I think that is good enough.
Now we're going to go to the scripts right-click or no,
do we have something pickups, level flow?
Do we have a folder suitable for the Nope,
we don't have one.
So I'm going to right-click create
a folder for the weapons.
Just copy that inside of here. Where did we go?
Weapons.
But we have sorry for that.
The weapon just okay. For some reason.
Just opened up some different folder
anyways, we create this.
It's the weapons, chest.
Hit Enter, wait for it to compile.
And any day now we add it to the actual weapons,
just and open up the script.
Okay, so what do we need to have in here?
First of all, we want this just
to give us random weapons.
So what we'll do, and this is
just a mechanic that I want to have.
Maybe you'd want to create something different,
but for now serialized field, the weapons pickup.
And it's going to be a,
an array with the potential weapons.
So the potential weapons,
then I'm going to create a private sprite renderer.
What does it, right?
Renderer.
And it's going to be the chest as R.
And you'll understand everything.
But for now, let's continue on.
The obviously the just sprite render this will be,
we need a reference to width.
You'll understand in just a bit.
Now I'm going to create another serialized field,
just going to be a sprite.
And that sprite is going to be the open just sprite.
And then we are going to have some kind of text.
And because we need to be using Unity engine Wi,
now we can access the text
and it's going to be the open key text.
Open key texts.
And I don't mean by key,
the key that will allow us to open the chest.
Well, I sort of do, but the key,
which key do we press on
the keyboard in order to open the chest?
Now that I think about,
maybe that's why they named the Key Vault maybe.
And with private, finally,
we need a Boolean to make sure that we can open.
That means that we are going to be in
the vicinity of this.
And chest.
And why do we first of all, she asked,
why do we need the chest sprite renderer and
why do we need the open chest sprite?
Well, because when we open chest, there you go.
When we see the sprite, just a second.
When we see we have two sprites,
one for the close 1,
1 for the open one.
That's why we need a reference to the sprite renderer to
change this bright when we actually open the chest.
So I'm going to go ahead and add
the open chest sprite, the potential weapons.
I'm going to add that and just lock this down
for a second to the Prefabs.
Pickups, V3 weapons.
Or even if you want you can add the helpful.
So, so maybe you get the help from
the chest and short, let's do that.
Let's add also the health just for fun.
Oh, okay.
So it's a, it's an array
of what does it the weapon system.
So or sorry, weapons
pickups or we can't add the health mice.
That something that I did not realize.
But anyways, let's continue on and start.
We need to get a reference to the sprite renderer.
So the chest as r is equal to
the debt component sprite renderer. There we go.
So now we have a reference to it.
And when we actually open the chest,
we want to change the sprite to something different.
Now before I continue one,
I remember the little hint
that I gave in the previous video.
We don't want every single time create
the if the tag is the player.
So let's go ahead and create a new layer.
Layers.
Oh, okay, so we need to remove this.
And we're going to create the pickups.
Hit Enter.
And now where we can go and set all of these pickups
to be working or all have the layer pickups.
Save that and now go to the Edit,
sorry, Edit Project Settings.
Where is it?
Physics, 2D, and the pickups,
all the interact with the player.
So I'm going to turn everything off except the PLR.
We save that, we go back.
Now we will make sure that this even the chest,
we can make sure actually
we should make it as the pickup also.
So it only interacts with a player and we don't need to
check for the tag every single time.
So what does this out also?
So now, what else do we need to do?
Well, we need to create a canvas for this chest
that will have some kind of word
right here or texts that will display.
For example, press E to
open the chest or press C or whatever.
So I'm going to right-click on the weapons,
just go down to u i and create a canvas.
Really nice. So now you can see that we
have a, another huge canvas.
What are we going to do about?
Obviously, we don't want it to be that big.
So what we can do,
we can set it as the world space right here.
And this will allow us to change the scale right here.
So I'm going to set the scale to 0.01 on the x,
0.01 on the y.
And let's just keep it at 0 or the Z at 1212 factors.
I'm going to change the position to 0.
And you can see how small the canvas is right now,
this little square right here.
And I'm going to change the x and the y to 0.
Now I double-click on the weapons just and there you go.
You can see that we have the canvas right here.
So now I'm going to add another 0 and here,
and maybe another one also on the Y.
Okay, that's too small.
And let's maybe try to okay.
That's too much.
Sorry. And maybe we just make it around 35,
okay, five on the y and on the x.
Excellent, so now we have it That's
reset its position back on to these 000,
maybe increase the height template.
And we'll call this the chest Canvas.
And as you can see now it's
a very small canvas and we need to make sure that it's
world space and now opens up.
And on the chest Canvas,
I'm going to go ahead and create a text Mesh pro.
And this text much Pro is going to say open, press key,
press E to open chest and enter this down a bit.
Make it bold, make the robot font that we have,
and make it auto size.
And let's see how that looks.
I'm going to zoom back in and make this a bit bigger.
Where is the text color?
Whereas the text anyways,
I cannot see it.
Oh, case.
So what we need to do is we also
need to change this to this sorting layer.
And there we go, we can see that the text is,
and here, and I was testing this out on the 2D.
I was wondering what was wrong.
And I thought that maybe if we are in 3D,
I could change the Z position.
But no, it turns out that on the actual just Canvas.
Have it in world space.
You can choose the sorting layer and
we'll make it on the player layer.
And I'm going to make sure that this is bold, auto sized.
And we are going to move the text up a bit,
so this is where it will appear.
And I'm going to increase the size and
we'll make sure that the max is bigger.
Okay, So increase the max as much as
possible and we can resize it to your heart's content.
How much you want to,
how big do you want to make it?
How small do you want to make it?
It comes back to you.
So I'm going to set it up here, save that.
Now the next thing is we'll go into the weapons,
just let's add a couple of colliders.
The first one will be a collider that will
just make sure that we keep things intact.
And then make sure that we don't just walk through
the chest and then we'll make a second box collider.
And this one will be a trigger.
And I'm going to change its size to be about yea big,
so able bigger than the actual box.
Or if we stand in this position,
we will be able to open up the box.
So save all of that,
make sure that it is a trigger.
Now what we're going to do is issue you a challenge.
And your challenge is to know what's in the box.
If you know this picture, you know it.
If you don't know the reference,
then it's your problem.
Then anyways, opened the box under certain conditions.
So we want to open the box.
We want to be pressing the Iike.
We need to make sure that we want to be
standing next to the box when we are opening it.
So when we are not standing inside of the box,
we don't want to have the ability to open it.
And also make sure we display
the text when we are next to the box.
So need to have two ways.
Whether we are near the box or we're not near the box,
we will be displaying the text and we can
open it if we are next to it or not.
So the pattern formation
with these conditions in your mind,
pause the video right now and go do the following.
Okay, welcome back.
So I'm going to just get started because we are have
already wasted a lot of time on simple details and ways.
We are on the Trigger Enter.
We won't be checking for the player tag for now.
And we are going to make sure that the open key text dot
GameObject dots are active is true.
So we'll be turning it off and only when we are inside of
the collider we are going to
open it up and on the arm trigger exit 3,
we are going to have
the same thing and we'll just put it in here.
Instead of true, it's going to be false.
Excellent.
Now, what is the next thing that
we want to do is we want to make sure
that we can open and it's set to true.
And if we're not,
then set can open.
Well, I just make this a parentheses.
Nope, can open z equal to true
and can open is equal to false.
There we go, we save that.
And let's just have an update in here.
We removed it and we're just going to be debugging.
Dot log can open equal to can open.
There we go. We save that.
What's the problem Boolean into an object.
For some reason, I just
forgot all the context and the world.
Okay, with that done,
let's go back and check in.
Here. We run the game. We have in our console.
It's false, false, false, false.
When we walk into the box,
there we go, we'd get an error.
Excellent. Why is that?
Because obviously we forgot to set the result goes down,
goes down the text.
How great is that?
O, and we also forgot to,
I hope some of you picked up on this DTM probe.
And this is not just any text,
it's a text Mesh parole Gui.
So save that, go back in here and
now we should see that we add the text,
save that, run the game.
Oh, and one more thing, excuse me.
We need to make sure that we turn of the text.
Just save that.
Again. Let's make this a bit bigger.
So we false, False, False can open.
Then we run in here, can open true.
We have the press Chest E.
And finally the last step we actually
want to an update in here.
Let's remove this. We know what works.
We are going to make sure that if the input
dot get key down,
we open this up key code,
the e. So when we press E and we can open,
then we are going to get the chest as our sprite.
And it's going to be equal to the open.
Just sprite.
Save that back in our game.
Now we run the game.
We stand next to the chest.
If we press the E key right here, nope,
We cannot do anything if we just Iike right here.
There we go. We can open the chest.
How cool is that?
So I hope you enjoyed
this little tutorial on how to create chests.
In the next video will be actually popping
up some weapons whenever we open the chest.
So I'll see you then makes sure to commit your changes.
And I'll see you there.