Adding fire rate to control our laser shooting

Create a cooldown system for laser shooting

Unless we want to shoot lasers at a full fire rate we should add a cooldown system to control how many times we can shoot in one second.

A cooldown system is a great way to control anything. For gun or just a simple trigger.

We create this cooldown system for our cube which shoots lasers for more information about this cube and Instantiating lasers you can read it from my previous article! :)

Here you can see a cube shooting without the cooldown delay.
We can shoot how much we want.

No cooldown

Cooldown Delay

Firstly we need a variable that will store our cooldown delay.

Fire Rate variable

Then we need to use Time.time in our code to know when our fire rate, 0.5 seconds has passed. :)

But before we can use Time.time, we need another variable to know if we can shoot again. And that is called _canFire

Can Fire variable

So inside our Player script where we shoot, we use this Time.time and our two variables that we just created.
We need to check if the time is greater than _canFire variable, then we can shoot.

Now we need to assign our Time.time again so it makes sense that we can shoot a laser every 0.5 seconds. And that we have to do before we shoot.

And now our shooting looks like this with our 0.5-second delay! :)

And since this is a our delay variable is SerializeFiled, we can edit our delay easily in the inspector.

Awesome! This looks pretty good now. Now we just need some enemies to shoot and watch out. That we will be doing in the next ones.

Thank you for reading this, much love & see you in the next one! 👍




From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Concurrency Control With Locking (Optimistic and Pessimistic With HANDS ON)

How To Use Pathfinding Algorithms With Satellite Images (Part 2)

Garmin Fenix 5 settings/alarm disappearing bug — updated Dec 11, STILL BROKEN!

My Side Hustle Journey

What are API Keys and Why are they so important?

The Sandwich

This AI backend engineer

Taking the lid off NEAR — 3

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
Janika Suhonen

Janika Suhonen

From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

More from Medium

What is a Control Track in Timeline

Day 105: Working with Animation Events in Unity

Cleaning up the Homing Missile code

Captivating with Sound

Create Audio_Manager Game Object