Cool Down System in Unity

Stephen York
2 min readMar 18, 2021
0.2 second fire rate

There are a couple ways to create a cool down system in Unity. My favorite way is to use coroutines since they are simple to set up and give multiple ways to delay the execution of your code, but we are going to talk about one that is just off time and is great to use for shooting mechanics.

Unity keeps track of the time that your game (or application) has been running and you can access that information with Time.time.

So to create a cool down system with Time.time, you will need two float variables: a fire rate and a next fire. Fire rate is the amount of time that passes before you can shoot your gun again or launch your next missile. My cube has a fire rate of 0.2 seconds so every 0.2 seconds it can shoot its next laser. The next fire variable starts off at zero but gets updated every time you launch that missile.

Fire() method handles firing functionality (how to launch missiles for you)

So to make it work, when you check for your input, or whatever trigger you use to cause that missile to launch, we also need to check if Time.time (the amount of time the game as been running) is greater than the next fire variable. At the start of the game, that condition is true since _nextFire is 0. But then _nextFire is updated to be Time.time plus your fire rate. So at the start of the game, if your fire rate is 10 seconds and you launch your first missile 15 seconds into the game, _nextFire will get updated to be 25. And if you try to launch another missile 5 seconds after the first (that’ll be at 20 seconds into the game), well 20 is not greater than 25 so the second missile will not launch. The second missile can only be launched at 26 seconds into the game and then _nextFire will be updated to 36.

And that’s how to create a quick cool down system using Time.time. Easy to get up and running and effective.

--

--

Stephen York

An Atlanta based Unity developer, who enjoys running, reading, and creating videogames!