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

LD2L Season 3 Draft Thoughts

Lasting Life Lessons I Learned from Playing MMO’s

Blizzard hits a wall with pushbacks

Gameing in the Year Two-Thousand and Eighteen

A Solo Guide for OSRS’ Chambers of Xeric: Part 2

Toy Blast Hack Online — Toy Blast Cheat Tool (2019)

Immersed in Witcher fantasy: Sword of Destiny

Nintendo Switch Starter Kit

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

Why is Debian not telling the truth about its security fixes?

Why does Japan still use outdated technology?

japanese retro tech

Developing A Game Engine with Perl : Part 4 — UEFI vs OpenSuSE Installer

End to End Encryption, Is It Secure?