How to Implement a Cool Down System
When creating a shooter game you do not always want the player to be able to shoot without a pause between shots. This can be done easily in Unity using Time.time.
Time.time is a record of how long the game has been running. So if your game has been running for a minute Time.time would equal 60 seconds.
To add a cool down you need to create two variables.
One variable to hold the length of the cool down and another to check if the correct amount of time has passed.
When the player presses the fire button you want to check and see if Time.time is greater than the variable that checks for time passed. When you start your game the default answer will be yes and the player will be able to shoot.
When the conditions for the if statement are met the FireLaser() class will run. This class has an equation that will add the variable that holds cool down to Time.time and set that number as the new variable to check against time passed. This will create a cool down on the players fire button and limit the fire rate.
- If cooldown is 2 seconds and the time passed is 0 seconds by default.
2. When the player shoots 2 second will be added to the current Time.time.
3. The new Time.time will be set as time passed.
4. When that variable is less than Time.time again the player will be able to shoot again.