Day 39 | Secondary Fire: 2D Space Shooter Series 07

Ted Lim
3 min readMay 13, 2021

In this article, we’re adding a different type of weapon to our 2D Space Shooter game. We’re going to add a penetrating missile.

Missile Behavior

Players must first collect a Missile powerup like all powerups in our game to fire the missile. The Missile powerup creates missiles that will destroy any enemy in its path and can be used for a limited duration. Afterward, the player regains the normal ship lasers.

Implementation

For the missile, we’ll add a Missile prefab with a Rigidbody and Collider. We’ll add a MissileScript to it and translate the missile upward at a given speed. Once it reaches the top of the screen, we’ll destroy the missile.

We’ll create another prefab for the Missile Powerup. In our Powerup script, we’ll add another entry for the Missile Powerup. If the player collides with the powerup through an OnTriggerEnter2D event, it will trigger a function in the PlayerScript that will turn on the Missile capabilities.

In the PlayersScript, we’ll create a method to turn on Missile firing capabilities. The idea is similar to the TripleShot. Turn on a bool that allows the player to fire Missiles, and then after the powerup duration, turn off the bool.

While the bool is active, we allow the player to use the FireMissile method as long as it’s not on cooldown. If it’s not on cooldown when the player fires a missile, update the variable that manages when the player can fire another missile. Then, instantiate a missile and play the missile firing sound. If these conditions aren’t met, the player returns to shooting normal lasers.

With the missile implemented, now if there’s a line of enemy ships that somehow manage to stack up, this missile will stay alive and destroy all ships in its path.

--

--