“Funslingers” Devblog #3 | Creating a Cooldown System in Unity
As we saw in my last Devblog, Cubert has a bit of a trigger finger. He’s able to shoot as fast as the player is able to press the spacebar, that can cause a lot of problems in term of game-design, balancing, and the sneaky players that know how to use macros. So let’s fix that!
Last time, we used a Coroutine for creating a a destroy timer on the bullets. This time we’re actually gonna use a different timer method for our rate of fire cooldown system!
So currently, firing Cubert’s gun can look like this:
That’s no good, we don’t want the player to be able to spam it like it’s machine gun, so to make this cooldown system all we need is a little bit of time…
Time.time to be precise.
All I did was add a second parameter to our if statement, once the first fire is shot, it will grab the current Time.time of the game runtime, add the desired fire-rate variable to it, and then make it a requirement for the current Time.time to pass that new number.
We serialize the field so that I can adjust the fire-rate in the inspector for fine-tuning without needing to go back into the script.
Look at that! I can now control the speed at which the player is allowed to shoot their gun!
In the next Devblog we are gonna add enemies and use physics to shoot them! How exciting!