Adding fire rate to control our laser shooting
Create a cooldown system for laser shooting
Unless we want to shoot lasers at a full fire rate we should add a cooldown system to control how many times we can shoot in one second.
A cooldown system is a great way to control anything. For gun or just a simple trigger.
We create this cooldown system for our cube which shoots lasers for more information about this cube and Instantiating lasers you can read it from my previous article! :)
Here you can see a cube shooting without the cooldown delay.
We can shoot how much we want.
Firstly we need a variable that will store our cooldown delay.
Then we need to use Time.time in our code to know when our fire rate, 0.5 seconds has passed. :)
But before we can use Time.time, we need another variable to know if we can shoot again. And that is called _canFire
So inside our Player script where we shoot, we use this Time.time and our two variables that we just created.
We need to check if the time is greater than _canFire variable, then we can shoot.
Now we need to assign our Time.time again so it makes sense that we can shoot a laser every 0.5 seconds. And that we have to do before we shoot.
And now our shooting looks like this with our 0.5-second delay! :)
And since this is a our delay variable is SerializeFiled, we can edit our delay easily in the inspector.
Awesome! This looks pretty good now. Now we just need some enemies to shoot and watch out. That we will be doing in the next ones.
Thank you for reading this, much love & see you in the next one! 👍