Galaxy Shooter 2D - Shooting #2

Instantiating lasers, and cooling down the fire rate

Wow, the previous blog article touched on too many things at once. I need to focus on one topic and keep it simple (stupid). Let’s backtrack.

If you’ve read the “Beginning” article, you’ve seen how fast I developed the project, but you didn’t get to see most of the steps and choices I made that got me to that point.

That being said, I am stepping back to the early prototype phase, and the next articles will be focused on every implementation I made to this project until we reach the same progress point as the first article. After all, we’re both here to learn together! :)

Pew Pew Pew! Spawning Lasers

Those pesky enemy ships aren’t going to explode by themselves, you know? We need to mount a Laser gun to the Player’s ship.

First, what kind of laser does that gun fire? We don’t have many resources to buy a recent model of the gun, so, an older Laser gun model should do. It shoots blue capsule-shaped lasers!

Our gun will always shoot this laser. To make clones and Instantiate even more, we’ll need to convert that GameObject to a Prefab. Simply drag from the Hierarchy to your project’s folders.

To add basic functionality to the laser, we’ll just make it constantly move upwards (you can see my article Move it, GameObject! to learn how I moved the laser using transform.Translate()).

Now, how can we allow the player to shoot lasers from the ship? The Ship script needs a reference to our Laser Prefab, and it should ask for a player input; if the player is holding the required key, we instantiate a laser. Few lines of code, and we have shooting in our game!

Alright, while we have implemented shooting in our prototype, it has two obvious issues (going to the least important to the most):

Lasers are spawning from within the ship

We really don’t want them to spawn in the middle of the ship. When we get to the collisions topic, we’ll have a serious issue where the lasers will self-destroy the player’s ship. We need to offset the spawning position. Simply adding that offset to the player’s position will do.

Too many lasers are spawning at the same time

This is due to checking for a hold input and instancing a laser every single frame. If this game runs at a solid 60 FPS, it will spawn 60 lasers every second (and then, the game will drop FPS in consequence).

To limit that, we need to define the time it takes for the player to shoot again (fire rate cooldown), and a timer to keep track of the remaining time. To update that timer, I used Time.time, the time in seconds since the start of the game, and I add it the fire rate time.

Because the timer stores a previous value of Time.time, and Time.time keeps updating every single frame, we can limit firing depending on if Time.time is higher or equal than the timer (previous Time.time value + fire rate time). Only when it fires a laser, it updates the timer.


We have successfully implemented shooting without any bugs! And you learned one of the ways to code a cooldown / fire rate system.

Hopefully, you enjoyed this article, and if you did, there are more articles coming their way! You just have to click that follow button!

I am a recently graduated Game Designer with strong knowledge of the Unity Engine! You can check my game “Noise Hunters” on Steam!