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

You Ain’t Gonna Need It — The YAGNI Principle

Software / System Integration: Tips and Best Practices

Development Boards : A Guide

Code Smells (Part one)

Co-Founder Series: Sridatt Bhamidipati

Data modeling in NoSQL

Nested Pagination

Running Networking Go App In Alpine-based Container

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

Day 17 of my Developer Journey: Creating the Speed Boost Powerup

Unity Features 101: Assembly Definitions

Week 11 — Open Source [2021/12/27]

Making Moving Platforms In A Unity Platformer