Calm your (laser) jets — A quick guide to creating a weapon cool-down system in Unity
An infinite fire rate might be a fun reward in a game, but when it's unintentional and permanent the player is going to feel overpowered and get bored.
Imagine how much RSI you would develop in FPS multiplayer games if your fire rate were determined by how quick you could click the attack button. Now that is what we are going to prevent by introducing a simple cool-down system.
First of all, we need to introduce a couple of variables. The first variable will determine our fire rate and will be editable via the inspector. This will be called “fireRate”.
Our second variable will mark the next time we can shoot. This will be called “canShootTimeStamp” which we will assign to 0 to allow us to shoot from the start of the game.
We can use “Time.time” to help determine if we have waited long enough to shoot again. Time.time is the running tally of time since the application started. This value is updated every frame.
Our logic will take the time the last shot was fired and add it to our fire rate and assign this value to our CanShootTimeStamp variable. This puts our “can shoot” variable in the future compared to our current Time.time.
If we utilize an if statement stating our current Time.time needs to equal or exceed our “Can shoot” time stamp this will prevent our players from rapidly firing their weapons.
Now all that is left to do is test our function.
As we can see our logic is sound and we now have a functioning weapon cool-down system.
That’s all for now.