Day 10 — Implementing a cooldown system in Unity

Hey and welcome!

You may have noticed from the end product in day 9 that we can fire off the lasers as fast as we can press the space key so we’ll want to add in a way to manage the fire rate of our laser. Firstly though let’s fix the behaviour of our laser so that it’s heading towards the top of the screen towards our future enemies.

You’ll have noticed that our laser object currently fall down into the void and that’s because of our rigidbody component we added before that automatically simulates physics and is used to check for collisions. To stop the capsule from falling we simply need to take away it’s gravity by unchecking it in the inspector.

Now for our laser behaviour. Let’s create a new C# script and call it LaserBehaviour or whatever you prefer and then attach it to our laser prefab. Then we put in the following code into our update method which should look familiar to you:

transform.Translate(new Vector3(0, 1, 0) * _laserSpeed * Time.deltaTime);if (transform.position.y > 10)

It’s the same transform.Translate we used for our player movement except we’re not needing to check for horizontal or vertical Input as we just want the laser to move up along the y axis continuously. You can also make use of Vector3.up instead of new Vector3(0, 1, 0) as they’re both the same and it’s a more shorthand way of typing it. Remember that this script is attached to our laser prefab and not the player object so the transform.position.y refers to the laser’s position on the y axis as opposed to the player that we’ve been used to before.

Lastly the Destroy(this.gameObject) just removes the instance of the laser prefab that’s instantiated when our if statement’s condition is true. This stops our game from being cluttered with unneeded objects.

You should have something like the above, theproblem is it’s still firing as fast as we can press the spacebar which would be fine in a bullet hell game but that’s not our goal here so let’s impose a fire rate/cooldown system on our laser fire now.

Best part is that it isn’t actually too bad to implement. Head back on over to your player script and add in two new variables.

private float _fireRate = 0.5f;
private float _nextFire;

Then alter your if statement for instantiating your laser prefab a bit and add in a new line of code inside it:

if (Input.GetKeyDown(KeyCode.Space) && Time.time > _nextFire)
_nextFire = Time.time + _fireRate;

The important thing to talk about here is the Time.time which just keeps track of how long the game has been running for. So if the game has been running for 40 seconds then the value of Time.time would be 40f. Using that logic the nextFire variable is being assigned 40.5f as we’ve set the fire rate to 0.5f.

So our Time.time > _nextFire is checking if 40 seconds is greater than 40.5 seconds which it isn’t so the condition here is false and will mean that the laser prefab prefab won’t instantiate until the game has been running 40.6 seconds.

That means we have a fire rate of roughly half a second so your game should be looking like this now:

That’s it from me here, pretty fascinating stuff with Time.time along with a cooldown system you could also use it to start a timer in your game or to cause an event to happen after a certain amount of time has passed.