Welcome back, my fellow game developers.
In this video, we are going to add automatic firing.
So you can see right here that we have a time between shots.
How much time do you want between shots? I can hear you.
You want to say 0.5?
Okay, sir, There you go.
We play run.
We had the Play button.
We run the gain,
and we hold down the left mouse button.
And there you go. You can see that we should
every single frame and you can see that they are very consistent.
So I'm not playing a game and here I'm holding down the button and it
keeps firing every 0.5 seconds or each have second,
we keep firing able.
So let's not waste any more time.
We're going to learn about counters.
And let's get started.
O k.
So we want to have the ability to
automatically fire bullets when the mouse button is held down.
So currently whenever we press the button,
once we can shoot,
but what we want is to have some kind of
automatic firing while we are holding down the button,
just like a rifle,
even though we are using a shotgun right here,
we'll just use our imagination and imagine that this is an automatic shotgun.
And while we're holding down the button, it keeps firing.
So how are we going to do this?
Currently, if you look right here,
you can see that this method,
the get mouse button down,
returns a true during the frame the user pressed the given mouse button.
So it only returns true once in
the frame that the player or the user has pressed the button.
What we want is we want to use something called the input dot get mouse button.
So notice this was mouse button down.
This is the get mouse button.
And if you read the description,
you can see that it returns a true whether the given mouse button is held down.
So while we are holding down,
this keeps returning true.
And this will allow us to keep instantiating bullets all the time, every single frame.
But we come up to a problem.
You've noticed that when we run the game,
if you remember, we can see the stats right here.
So we have about 800 to a thousand frames per second.
So every single frame,
while we are holding down the mouse button,
we are going to be instantiating a bullet,
which is very bad.
So we are going to have around the 1000 bullets every single second.
Now, it's fun.
It's a very OOP weapon,
but that's not what we want.
We want to have a certain time between
firing each book while we are holding down just like a real weapon.
What's the mattering here?
Okay, So no worries.
I think this will sort itself out. Anyways.
Let's start off.
The first thing we're going to do is we'll create a serialized field,
which is going to be a float,
and it's going to be the time between shots.
A second variable that we'll have as a private float and the shot counter.
And what we'll do is as soon as we start,
we are going to set the shot counter to be around 0,
or we can set it immediately in here instead of setting it and the start.
So we start off at 0.
Now if we scroll down in here,
we are going to create another condition.
And we'll say that if the input dot get mouse button,
which returns whether the given modes button is held down.
And we also use the index 0 for the left mouse button.
We are going to start using the shot counter and
removing from it the time dot delta time.
So do you remember when we use the plus equals
somewhere up here when we were using the transform,
now we don't have it.
Well, this is similar to the plus equals.
So this represents the shot counter minus Time.deltaTime.
And you know what, I'm going to duplicate this, comment this out.
And then I will make this a minuss equals.
So you'll remember that these are the same things.
And I'll even move it down next to the line right here.
You can keep this as a reminder of why we are setting minus equals,
plus equals and all of these things.
It's much easier to read and it's much cooler
and will make you much more professional
looking whenever we are showing someone your game.
So let's continue one,
what do we need in here?
While we are holding down the mouse button,
the shot counter is being counted down.
So what we want is whenever this shot counter ends or becomes 0,
we want to fire a bullet and then
refresh the shot counter back to the time between shots.
So how are we going to do this?
Instead of immediately instantiating,
I'm going to issue a challenge.
Was that a better transition than last time?
Anyways, your challenge is to check if the counter has,
and so create an if condition that checks the shot counter.
If the shot counter is less than or equal to 0,
why is it less than or equal to 0?
You might be asking why not immediately 0?
Well, because as we're counting.