Welcome back, My favorite game developers.
I know this is a long video,
but we do a lot of things.
We learn a lot of nifty small tricks in here.
So when we run the game,
you'll notice that we have only
the pistil right now and the
weapons arm we should the pistol, everything works fine.
We have the shotgun on the ground.
Uw, what do you think will happen when we pick it up?
So I headed I pick it up.
There we go.
We can see that I have a shotgun
and we can see the bullet of that shot gun.
We can't switch off anything.
We can switch between the pistol and the shotgun,
but let's not worry about that.
Everything will happen.
Everything will be fixed in due time.
So let's not waste any more time with
this big long video and let's get started.
Oh, case.
So time to set up the way we pick up our weapons.
So right now, the way we have our weapons is good enough.
We are switching, we are doing everything,
but this is not how we want our game to work.
We don't want to just start off with all
of our buttons and just switch between them.
We want to have some kind of mechanics that allows us
to have them or acquired them in some way.
And one of those things is we can pick up
our weapons at certain points and the level.
So let's go ahead and do that.
The first thing that we are going to do
is we're going to go into the doom player.
We're going to remove
the weapons that we are not holding.
So I'm going to remove the rifle,
will need to open up the script.
So remove the rifle, remove the pistol,
and go back into our game. There we go.
Now one thing that we need to make sure of is that we
also remove them from the available weapons right here.
So I'm going to remove these empty slots,
save that, and let's see what we have in here.
Okay, so as you can see,
this is also something that we will
need to change and I'll tell you why.
Let's just get things out of the way.
So currently we are setting
the current gun at one and then setting the weapon UI,
but one isn't available right here.
So let's see what happens when we run our game.
We should see that we have an arrow right here,
and this error is argument out of range.
I double-click in here,
it takes me to the setting weapon.
That means the current gun is out
of range inside of the available weapons.
And of course it is because
we are setting the current gone
to one while we only have one weapon.
So the only index that we can access a 0.
I could set this to 0 if I wanted.
But then what happens
is that every time we start the game,
we have, for example,
the shotgun and the rifle,
and the rifle is on while
the current gun different than you get what I mean.
So the index will be
out of place and this is something that
we didn't take care of in the last video.
So I'll show you what happens even though
this will take a bit of time to
understand, but no worries.
I want to make sure that everything is
properly understood and why
we're doing all of these things.
So pistol, shotgun, rifle, apply the changes.
For example, let's say I turn off
the pistol and I turn off the rifle.
So now what we should have as the shotgun as we begin.
So I'm going to run this.
There we go.
We have the shotgun shooting,
but I have the pistol and the UI.
If I click it again,
then I have the shotgun that I have the rifle.
So as you can see, things are weird at the beginning.
So instead of doing that,
what we're going to do,
we're going to remove this and we
are going to create a for loop.
So four and I equals 0,
I is less than the available weapons dot count,
count because we are using lists and I plus, plus.
And in here I'm going to check if the available weapon,
the position I dot gameObject dot active in hierarchy,
that means which show up and have we actually
chosen to be available to the player?
Then we are going to set the current gun equal to I.
And that way everything should be working perfectly fine.
So if I go back in here,
I have the shotgun.
I run the game.
The weapon should be correct right here.
There we go, shotgun.
Let's test it out on the rifle.
So turn this off, run the game.
So now we should have the rifle.
And there we go, everything is working properly.
And finally, we tested on
the pistol and remove the rifle, run that.
And we should have the pistol.
Okay, great, So everything is working good.
Now, I took a bit of fun to do this.
No worries, but as long as
this is out of the way because it will
be important when we start actually picking up weapons.
So the first thing we need to do is we need to create
the pickup object that we are going to interact with.
And I've created these three sprites right here.
And the difference between
the regular sprites and the guns and
these ones is that I've added a bit of
white layer around the console.
We can see them in the world.
So I'm going to go into the sprites,
right-click in here, create.
Nope.
Create a new folder which is going to
be the weapons backup.
Hit Enter, open this up.
Or we could have added to the pickups,
but no worries about that.
Having more folders never hurt anybody.
So as you can see, there is
a white outline around the shotgun.
So if I add it in here,
set it on the player.
And I'm just going to make all of these
32 and make them point.
Apply the changes.
There we go. So as you can see,
the shotgun has this white outline
around it so we can properly see it and work.
It wasn't that big of a deal whenever we
were holding the weapon
because we have the player as the background.
But now we need to make sure that we can visibly and
easily see the shotgun or any pistol and weapon.
And if you don't like it, honestly,
it took me like, I don't know,
one hour to do this in Photoshop.
I have no clue how good or bad it is.
Maybe some of you are cringing at the work,
but personally, it was my best.
Let's continue one.
So what we're going to do in here
is let's keep this as the shotgun.
We're going to add a component.
Obviously it's going to be the collider.
So a box collider 2D.
Let's see how big that is.
And maybe we can increase
the size a bit on the x and the y.
Don't have to hit
the weapon entirely suits to pick it up.
So just a little bit and have it as a trigger.
We save that. And the next thing we're
going to do obviously is some behavior.
So I'm going to go into the scripts,
go into the pickups,
right-click in here and create a new C-sharp script
which is going to be the weapons backup.
Hit Enter weapons, pick up,
wait for it to compile to the shotgun pickup.
And let's open up the script and
see what we are going to do in here.
Okay, so the first thing that we need,
as we actually need to know
which gun this is going to be.
So the shotgun obviously will give us the shotgun gun.
So I'm going to create a serialized field in here,
which is going to be the weapon system of
type urban system is going to be
the actual weapon that we are going to pick up.
Save that.
Now, the next thing we want to do is we
want to be able to pick this object up.
And before we can actually add this object to the player,
we want to make sure that he
doesn't already have that object.
So I don't want to be carrying around 10 shotguns.
I only need a single shot gun to be available to me.
So let me first of all like this down.
Let's go to the prefab,
go to the weapons,
and let's add the shotgun here as the weapon.
So I hope you understood what I mean.
I don't want to pick up a shotgun
while I already have a shotgun.
I want to pick up a shotgun only if it is
not being held in my available weapons.
And that is your challenge to
look through the layer guns.
So first of all, you need to create an entrepreneur
Enter and check if we collided with a player.
Obviously, we don't want our skeletons to
be attacking us was shot guns.
Who then you'll need to create
a variable inside the method to
keep track of whether we found
the weapon or not on the player.
You need to go through all the guns deep layer
scurrying and obviously check if we're carrying the gun,
you've picked up using the GAN name,
so we'll need the gun name and phone to
access the gun name and to access the available weapons,
you'll need to create a couple of extra methods.
So this is a big challenge,
a very tough challenge,
but I know you're up to the task.
Take your time, play around,
do mistakes, don't worry about it.
I will be there to help you after we've tried.
So you don't lose anything.
Pause the video right now and go do reach Alan.
Kay, welcome back.
So the first thing that I'm going to
do is remove the start and update.
I'm going to create the entrepreneur enter 2D.
And here obviously we check if the
collision.com tag as the player.
And now we can continue on.
Now the next thing and something about here,
maybe we could have just immediately made
all the pickups have a certain layer and make
it only interact with
the player layer that would have been a bit smart
and maybe not have all of
these f conditions every single
time we want to use a pickup.
So just a silent,
I forgot about it.
Maybe you can do it on yourself.
With that said, let's continue on next. What do we want?
Well, obviously we want
this Boolean variable that will say
that gun on player.
And this will be set to false.
Okay, Now the next thing we need to do is we'll need to
create a foreach loop that checks.
And it's going to be checking for guns so or weapons.
So it's the weapon system.
The weapon to check.
And where are we looking for these?
Well, we are looking for
the available guns on the player.
On the player controller,
their goal, it says it right here.
So we are going to look through the available guns,
but obviously available weapons,
but obviously it's a serialized field.
We don't want to make it
public and available to everyone.
So what we'll do is down here,
we're going to Create and
there you can see the mistake that we've made.
We're going to create
a public list of type weapon system.
So I hope you tried this, excuse me.
And we're going to call it the get
available weapons
on layer.
There we go. That's a long name.
And what we're going to do is simply return
the available weapons. There we go.
So now what we can do back in here,
and because we can
immediately access it from the collision,
which we've collided with the Get component.
And because we already know it's the player,
we can access the player controller.
And from the player controller we can get
the available weapons on the player.
Great. So what we've done in here,
we've created the ghetto available weapons
by having the simple return,
which returns a list.
And because it is a list,
we can add it right here.
So we are checking the weapons to check.
Now, how are we going to know if the weapon is there?
What we'll do is we're going to create an if condition.
And in here we are going to check if the weapon that is
in here dot the name of the weapon.
So obviously if I go to the weapons system,
if it's in here somewhere,
the weapons system, we have the weapons name,
but again, it's a serialized field.
So these are okay.
So we do have the get weapons name, UI.
Okay. No worries.
That's all that we need.
Yep, That's all that we need.
So in here we are going to check get
weapons name on the weapons or no,
actually, we don't need this.
Get weapons name you are.
And if it's equal to the weapon to
check dot get weapon name UI.
So maybe UI wasn't
the perfect name in here for the method,
maybe it's better to just call it the name
because we are using it in a different way.
We can double-click create our R
and just use the getWidth and name.
Hit Enter. There we go.
Now we've changed this all over,
even in the weapon system.
Down here, you can see how cool is that.
So Control, double or,
or, or, so if it is there,
that means that the weapon is
already in the available weapons.
So we're going to set the gun on layer to true.
And after we finish looking
through all the available guns,
we are going to after this check-in here,
if V gun on player is not true,
so that means that it's still false.
We don't have the weapon, honest.
What we'll do is we're going to go ahead and instantiate
the weapon on the collision dot get component.
And it's going to be the player controller.
And why am I doing all of these steps?
Well, because I want to actually make this weapon.
If we go back in here, you can see that the weapon is
actually a child of the weapons arm.
So we need to make we
can see right here that an instantiate,
we can set the child.
So we're going to do just that and we
need a reference to the weapons on.
So if I scroll up and here
you can see that we do have a weapons arm.
So all we need to do a scroll down and again,
create a public. And what is it?
It's a transform, I believe.
Yes, say transform.
So we'll need to create
a public transform and get weapons arm.
And we're simply going to return, return the weapons.
Are.
There we go. So that was a hard challenge,
I know, but I hope you at
least try to get halfway through it.
So get weapons.
It's not showing up because it's a public transform.
Get weapons on.
So player controller dot get weapon.
Oh, so player bullet controller.
Okay, So player controller get weapons arm.
And there we go, we save that.
We go back into our game.
And let's see how that works.
So we have the histone and
we have the shotgun on the ground.
We pick up the shotgun,
the weapon available on it,
save that, run the game.
And now when we get close to the shotgun,
nothing happens because if I double-click in here,
we can see that, okay.
And other than that in here,
we get to check.
Okay. So welcome back.
For some reason, I didn't change anything.
I just run the game now and it seems to work.
There we go. You can see that we've picked
up two shotguns. Why is that?
Well, obviously because of
the two colliders that we have on us.
And also don't destroy this shotgun.
But as you can see, we are still
picking the shotgun up multiple times,
even though we can look through this and see
that we have the shotgun already honest now.
So before we continue on,
the first thing that we need to do is
create the private boolean,
just like we have in the health pickup.
So we can just copy this, paste it in here,
and said the pickup to
true whenever we actually pick up the gun.
So in here said this immediately to true.
And we also check if we haven't picked up the weapon yet.
So as you know, this is because we
have the two collider on us.
So save that back in our game.
Let's see how that works.
Now, we run the game.
We have the shotgun. Oh, and we also need
to destroy the weapon, excuse me.
And after we do pick it up,
if we do instantiate the weapon,
where is it for each?
So in here you can choose
whether you want to destroy it in any case.
I believe we should destroy it in any case.
So even if we don't actually pick it up,
it doesn't need to stay there.
So I'm just going to destroy the object as soon
as deep layer actually heads that object.
So back in here, run the game.
And there we go.
We pick up the weapon,
and now we have the shotgun and our hand.
As you can see, the shotgun shoots and we can't
switch the weapons on and out
between the shotgun or the pistol.
Why is that? Well, because we haven't
actually added the shotgun right here
into the available weapons and that is
something that we will need to fix in the next video.
That is also the reason
why we saw right here that we have
multiple shotguns being picked
up without the weapon being destroyed.
That will be fixed in the next video where we actually
start adding the picked up weapon
into the available weapons.
So that said, I hope you enjoy it.
Make sure to commit
your changes and I'll see you in the next one.