Stay Cool with a Cool-Down System

As awesome as it may be to fire lasers as fast as you can press the button, you can add difficulty, depth and a dose of reality to your game by implementing a cool-down system.

Objective: Enable time delay between laser fires.

Lasers fire as fast the space key is pressed

Starting Point: Laser fires from Player object as fast as the Space Key is pressed.

Steps:

  1. Set a time delay variable
  2. Set a time the laser can be fired next
  3. Check if laser can be fired
  4. Update time laser can be fired next

Set a time delay variable

To allow the laser to “cool-down” between shots, create a variable to hold the time that should pass before the laser can fire again.

Create a laser delay variable in the Player script

Since the Player object is instantiating the laser, the Player object will also house the logic for when the laser can fire. You can edit the time delay value in the Inspector to suit the feel of your game.

Set a time the laser can be fired next

To determine if the laser can be fired, we will need to store the next time the laser can be fired.

Create a laser ready variable in the Player script

You can initialize this variable with 0f or even -1f if you want to ensure a laser can be fired immediately at the start of game.

Check if laser can be fired

In addition to checking if the Space Key is pressed, you’ll also need to check if the time when the laser is ready has passed before firing the laser. To do this, first you will need to know how long the game has been running. In Unity, Time.time stores the current run-time for the game.

Compare Time.time to _laserReady to determine if the laser can be fired

You can compare Time.time to the _laserReady variable to only fire the laser when Time.time is greater than _laserReady.

Update time laser can be fired next

With the code so far, the laser can still be fired as fast as you can hit the Space Key. This is because Time.time increases as the game is played, but _laserReady is still set to its initial value — so runtime is always greater.

Update _laserReady with the current runtime plus the _laserDelay

To fix this, you’ll need to update _laserReady to the next time the laser can fire. To do this, assign the sum of Time.time and the _laserDelay to _laserReady.

Lasers firing with cool-down delay

Now, the laser can only be fired every 0.2 seconds, even if you spam the Space Key. Now that’s keeping your lasers cool!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Microsoft is acquiring Activision Blizzard, a well known videogame company, for $68.7

Carry on my Midnight Suns

Stygian: Reign of the Old Ones Review

Blood, Souls and tears

Clone Controversy Regarding Threes! Game

Starting a Dark Country WIKI: Card visual details

June 2021: Mainstage Gaming Company Update

Dark Country Play-2-Earn Tournament: 1000 WAX Prize Pool

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Erin Brown

Erin Brown

More from Medium

Player’s firing ability and prefabs

2D Mobile Game: Player Jump Optimized!

Why I Should Be Writing Pseudo-Code?

How to Create a 3D Prop for a Video Game