Cooldown System in Unity

Anurag Chawla
2 min readApr 3, 2021

--

What is Cool Down System in Unity what it means — delaying in Instantiation of Game Objects

Objective- When it make sense to implement this concepts in Game Objects.

As per my last article— When Player hits the space key, it fires a Laser. Now see it continuously firing Laser so rapidly that makes annoying and kills all enemies very quickly and makes no fun..

Here Laser fires.. as soon as Player hit the space key

So , current logic as below we don’t have cool down i.e, Lasers keep on Instantiation instantly.

No Logic — How to delay the Instantiate of Laser.

The delaying or cooldown of Laser Instantiation can be done easily using logic defining firing variables- such as fire rate, can fire and using Time.time Unity Scripting API.

Note:Time.time — means how long game is running i.e, if one starts the game and stops it after 10 sec, it means Time.time is 10 sec.

Logic for CoolDown System

The above code snippet — shows that when player hits the space key and it checks condition for Time.time(say 1.0 sec) > _canfire (-1.0 ) which is true

It satisfies the condition and update the _canFire=1.0.s(Time.time)+0.5s(_fireRate)=1.5s

Next time Time.time (say 1.1 sec)>_canFire (1.5) condition is false , so instantiation of Laser is delayed and it goes on till Time.time reach 1.6sec.

This way one can implement the Cool down System- when Player hits space key again and again - Laser fires with a delay as compared to earlier where it fires instantly

Cool Down System- Laser fire when Player hits the space key

(In Unity Documentation -Co-Routine) is another way of implementing logic to set the variable like bool _canFire = true for Cooldown system and run after regular interval of 5 sec using.

@GameDevHQ-Day 8…

--

--

Anurag Chawla
Anurag Chawla

Written by Anurag Chawla

Emerging Technologies - Power Virtual Agents | AI Chatbots | Multi-Cloud & DevOps | UI Technologies - React , Angular | Gaming Technolgies - Unity, XR- AR/VR

No responses yet