11-Cool Down System for Laser Shooting

YShu
Unity2D Game-Galaxy Shooter Dev Log
2 min readApr 4, 2021

Objective: Shoot the laser at a certain interval (i.e., with a certain delay), where the delay can be defined as follows

The first question to ask is where to implement this function? Since it controls the laser shooting, it means that before instantiating the laser game object, we need to check against some time threshold.

This time threshold should be able to check if enough time has passed (Time.time) after the previous laser instantiation event:

// Time.time > nextFire

It should be noted that this nextFire variable has to be defined and initiated with a value equal to or less than 0, such that we can fire our first laser. After each laser instantiating, the nextFire should be reset to the current Time.time plus the firing delay, so that the player has to wait for the given delay time before being able to instantiating the next laser.

// nextFire = Time.time + _firingDelay

Now the firing rate can be controlled by modifying the _fireDelay variable.

This cool down system can be easily extended to many other scenarios where a waiting time is required for a repeated action to happen.

--

--