Cool it Down! — A Cooldown System in Unity

Accurate depiction of no fire rate limits.

As fun as shooting at the speed of light can be, it can turn the player into an overpowered monster and limit the challenge of an experience. Cooldown systems exist to limit firerates of weapons, set delays on abilities, respawn timers, and more!

In our case, we want to limit the player’s ability to spam out lasers from as fast as they can press the spacebar to every half second.

We want to limit the instantiation of the lasers, so these edits will go to where we instantiate the laser; the player script.

Add two new float variables; fireRate and nextFire. You can set their values to whatever times you like, but nextFire must be lower in value than fireRate, or the system won’t work!

Now we can add to the FireLaser method from my earlier articles to get the desired behavior.

We check nextFire vs Time.time (which is how long the game has been running)

Two “&&” can be read as “and” in C#

Now, we need to set the cooldown period inside the “if” statement.

So in order to get this function to fire, nextfire has to be a lower value than Time.time, and we must press the spacebar.

When we do, the nextFire variable becomes Time.time + the fireRate value.

Therefore, we must wait the fireRate value before the “if” statment conditional is true again, and we can fire again.

The entire method.

And that’s it! this can be repurposed to make ability cooldowns, can be extended for different weapon types, etc.

--

--

--

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

Recommended from Medium

Creating an IAM User on AWS with Programmatic Access

Approaching HIPAA Compliance

5 Mental Tricks To Do Your Best When It Matters Most

graphic of a worried-looking person trapped between two spiked walls closing in

Run your containers on AWS Fargate

Setup MacOs for Go Development

My favorite Gnome shell extensions

Automated Webflow to Jamstack Converter

Ubuntu upgrade from 18.04 to 20.04 LTS: Installation Requirements for an Existing Rails Application

Ubuntu upgrade from 18.04 to 20.04 LTS

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
Jack Leavey

Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

More from Medium

Loading Scenes in Unity

Adding Scene to Build Settings

How to Switch Virtual Cameras Using Only Cinemachine and Scripting in Unity

Keeping All Objects In View in Unity

Layering Materials In Unity