A Cooldown System in Unity’
Objective: How to make a cool down system in Unity.
Foremost it’s important where we implement our cooldown system in our Space Shooter. The place you wanna implement it is your player C# script because it related to our firing method. Were gonna be inside of this line of code in our C# script here in this picture below.
Now we know how to define the logic when time has passed for our firing method in this line code right here. Now we need a variable to define our cool down delay. Were gonna create a variable private float and name it fire rate. The next important thing to do will be to set our fire rate to a default of 0.5f also let’s not forget to add a seralizefield so if developers come in later to change it they can. The next step is how do I know when I hit the space key there is a delay 0.5 seconds and how do we come up with this logic. The way we can do that is through passing time called Time.time. The definition for Time.time is how the long game has been running. Next we need to make a variable for our fire rate and we already know our fire rate is 0.5 for the delay. The next crucial step is to create a variable of canFire at negative one. Once done we can now go into our line of code. One more thing to do in the inspector is adjust the fire rate to 0.15 seconds instead, it will run better in our game. Down below you will see the how we did it.
This how you make a cool down system in our Space Shooter.