Hey, welcome back, my fellow game developers.
In this video, we haven't changed much,
but what we did is we just
started setting up a proper weapons system.
We moved the shooting mechanic
from the doom player to the actual weapon,
which is the Sharp console.
As you can see, there is nothing new in here.
We just did a couple of changes.
This video is all about,
let's call it upgrading
your project so you get to a certain point.
You have all the basic mechanics that you want,
but now it's time to make them much more efficient.
So without any further ado,
let's not waste any more time.
And let's make the project grade again.
0 case.
So it's time to create a more
advanced and the professional shooting system.
So the first thing that we are going
to do is we are going to move
everything that is related to
shooting from the player controller
into a separate script
that I'm going to create right now.
If I go into the scripts
and maybe let's make this a little bit bigger.
So we can see them at least and two rows.
These, okay, there we go.
I'm going to go in right here.
I'm creating a folder which is going to be
called the gun or
let's call it the weapons system.
Have Enter.
And I'm going to create
a script in here which is going to
be the weapons system.
Hit Enter, wait for it to compile.
And there we go.
And a day now. Thank you.
It was just a script.
Anyways, I'm going to make
the fire point a child of the shotgun,
and it won't allow us will need to open the prefab.
There we go, make it a child, save that.
Go back into our game. There we go.
So now we have, and sometimes when
you want to add the prefabs,
make child of things and make parents.
Sometimes it will require you
to the extra step of actually changing the whole prefab.
And the next thing we're going to
do is we're going to add
this weapon system to the actual shotgun.
So now the shotgun will handle all the shooting.
First of all, let's open up the weapons system.
And if I look in the layer controller, where is it?
Let's click in here.
Whereas the player controller, okay, though.
So there we go. As you can see,
we have so many scripts right now.
But anyways, what we want to do is we want to
move and this should be a separate method.
What we want to do is we want to move
the whole player shooting mechanic
from the player controller into the weapon system.
I've maybe mentioned this before.
We don't want the player to handle everything.
So for example, we don't want
the player to handle how bullets fly,
and we don't want the player to handle how weapons work.
So with that said,
I'm going to issue you a challenge.
And your challenge is going to be to move
the firing to the proper script.
So couple of the firing method from
player controller to the gun system script.
Make sure the two copy all the proper variables with it,
and set everything in unity and test it out,
make sure it works properly.
So this challenge is just
about how you upgrade your script,
how you upgrade your project and gain.
Sometimes you start off your project.
You don't have the entire big plan for it,
so you need to make adjustments.
This will just be an exercise in
how to transfer things from one script,
the next, making it better,
I'll do it in front of you,
so don't worry about it.
We'll we'll make sure that everything works properly.
But for now, pause the video and go do the challenge.
Oh, okay, welcome back.
So what are we going to be doing in here?
We are going to go ahead and select all of this.
I'm going to cut so Control X. I'm
going to go ahead and put it in here or know what,
let's keep the update and start.
We might need them. I'm going
to paste everything in here so you can
see we have a lot that we need to handle.
So back in here,
we will need a couple of
variables which are, let me go up.
First of all, I'm going to
comment this one out, save that.
Then I'm going to check in here, what do we need?
We need the swap and automatic the bullet the far point.
Okay.
So let's see as weapon automatic time between shots,
shots countered, fire point. There he goes.
These are.
Variables and I'm going to paste them in here.
Save that.
Let's see what else do we have?
Do we have some kind of error here?
Nope, I don't think that was all the mechanics.
So back in here, what else do we have?
0. So if the player can dash, now,
obviously the dashing is part of
the player mechanics of
the player is the one that is going to dash.
So what we'll need is a way for
us to get a reference to the dash.
So is the player dashing or not?
What we can do?
If we scroll down here,
we can create a method.
And if I look up here,
the dash you can see as private.
And what we can do is we can create a method in here.
And these are things that we are going to be using
more and more as we go this type of method.
So it's a public bool and is layer dashing.
So in here, all that you are going to do
is return can dash.
And I'll add an exclamation mark
because remember how the dashes working.
And because we only return one thing,
this is how we are going to be writing it.
Now, obviously this is not something
that is like a standard.
This is more like practices.
So have some kind of
variable in some different script
that you want to access.
How do you do it?
Well, you either make the variable public,
you make it accessible to everybody.
But the problem when making it
public is that you can change.
So if I make this public,
I can then change it from a different script,
which is a very bad idea.
Trust me, you never want to be able to change
variables in different scripts from different scripts.
So you don't want to be able to
make the player dash from,
let's say, the UI Manager by
mistake, it happens sometimes.
So a better way is to just be able to
check if we are dashing so we can get the dashing,
but we cannot change whether the player is dashing.
So this is an extra layer of safety and
this is why we always use serialized fields.
We could have made them all public
and it would have been the same functions.
We could have seen them in the inspector and so on.
But making it a serialized field means that we
cannot access these variables from different scripts,
and this will pay dividends later on
as you go in your game development journey.
Making sure that you can,
not only in game development,
whenever it comes to programming,
you don't want different classes,
changing variables, other different classes.
Anyways, I'm getting ahead of myself.
What are we going to do in here?
We can either find the player object
or we can get component in parent.
And we can, let's remove this for a second.
We can access the player controller and the parent,
and we can go ahead and check.
Is player is player dashing or what,
what's this player is dashing.
What's this layer is dashing, is moving.
There is dashing 0,
so we can actually also return this.
So this method is no longer needed.
And that was really not something that I noticed.
But anyways, layer is dashing.
At least we had the opportunity.
Let's just double-check if
the current movement speed is equal to the speed.
Okay, so we return true. And there we go.
If we are dashing,
then we cannot be shooting
and will not call this the player shooting.
Let's changes to the firing bullets.
There we go. So this makes more sense right now.
And what else should we be doing?
Well, obviously, we need to be
calling it in here in the update,
making sure that we can shoot.
So I'm going to be simply calling
the firing bullets method.
And now we need to go back
into Unity and set everything up.
So I'm going to open up Unity.
You'll see that the weapon system changed.
And this was, this is so embarrassing.
I didn't even notice it was just there and
I completely forgot that we had displayed this dashing.
But anyways, at least you learn
something a different type.
Or we have this little monologue of why
we don't want access to the variables.
Now, no worries. Electron of
course we will be implementing a lot of
these methods as we move forward
and make our code a bit more professional.
So with that, who Things Done?
Let's go in to,
first of all as 70 far point of the shotgun.
We are going to make sure
that this weapon is not a romantic and we'll
make the time between shots around 1 second, 1, not 21.
We need a bullet, so we'll go into the Prefabs,
broken bullets, and the layer bullets.
Save that.
Let's go into the doom player and apply all the changes.
And now I'm going to What else do we have?
Let's run the game and see what happens.
So the player, player moves around way,
but so the player moves around and if we should,
There we go, we can shoot.
And if I hold down the mouse button,
for some reason, it's not shooting.
123.
Oh, it's not automatic.
Okay.
But the problem is so okay.
Now, even though I hold down the mouse button,
it's not automatic, but the shotgun still fires a lot.
And this is something we want to fix because we are
now talking about an advanced shooting mechanic.
It's not the usual shooting mechanic.
We need to make sure that our shooting is consistent.
So the shotgun, if we hold down,
it shoots one button,
but if we keep pushing so the shotgun shouldn't
be firing so much when shooting.
So with that out of the way,
with this whole section, Change, scripting everything.
I hope you enjoy it and I'll see you in the next video.
Make sure that you always commit your changes.
And I'm going to make this commit
move shooting mechanic from
layer two, the actual weapon.
There we go, commit the changes,
and I'll see you in the next video.