Create a Cool Down System in Unity

Christopher Pearl
2 min readJun 18, 2021

--

I want my space shooter to succeed against his enemy ships in my game, but I’d like it to be slightly realistic. No laser guns come with unlimited ammo, unless you’re in Star Wars. So, I added a few lines of code to my movement script in Unity to get in control of the shooting rate of the space shooter.

I needed to define and add logic to my input system in order to know when time has passed. To do this, I used the value Time.time which calculates the time that’s passed in seconds since the application has started. Adding this allows me to control when the laser is spawned into the game and allows me to place my limits.

First, I needed to create a variable that controlled my cool down delay. I made it O.5 seconds and added a Serialized Field in case I want to go back in and make it longer or shorter in Unity later.

Then, I added a variable that checked whether or not the player would be allowed to hit the space key. This variable is going to be checked in comparison to how long the game has been running. I set this to -1.

Next, I added to Input condition to check for the space bar key to be pressed and if how much time has passed is greater than the check comparison variable I created.

Finally, I added to my FireLaser() method. This statement would trigger the laser to be fired depending on the time passed in the game. This places the restrictions I want in order to limit the amount of lasers being fired from the shooter.

Now my space shooter will have some realistic fire mechanics when played in my game.

--

--

Christopher Pearl

Unity Developer, AR/VR Developer, Virtual Reality Product Manager for Micro Medical Devices Inc.