Nerd For Tech
Published in

Nerd For Tech

Unity Guide

Implementing space shooter game features - Multi-direction shot

A quick review of new features added to a space shooter game in Unity

Objective: Implement a new type of shot for a space shooter game with Unity.

In the previous post I implemented a camera shake effect in my space shooter game with Unity. Now it’s time to implement a new type of shot for the player spaceship.

New Assets

To start, let’s get the new sprites to implement the new shot. If you don’t have any edition software to create them you can always use a free one like the one I use:

Also, if you don’t know how to create shots, power-ups or animations you can check these older posts to get a reference:

New shot

This is the new shot sprite, which will turn into pellet shots to keep destroying enemies around when it hits an enemy:

Make a prefab to instantiate it from the Player script.

Pellet shot

This is the pellet of the new shot, which will be instantiated to go in another direction when the new shot hits an enemy:

Pellets prefab

This is a prefab that includes the respective pellets of the new shot, which will go to 8 different directions to hit other enemies when instantiated:

Power-up item

This is the new power-up item that will enable the multidirectional new shot for 5 seconds when it gets collected by the player:

Implementing the new shot

Now, in order to implement the new shot behavior. let’s create 3 new scripts:

  • Shot
  • ForwardShot
  • MDShot

Shot class

This class won’t be inherited from MonoBehaviour. It will work as the main class to contain all the properties a shot could have in the game, just like:

  • Speed
  • Property (player or enemy shot)
  • Color animation
  • Fire rate
  • Offset to be instantiated
  • Space bounds to be instantiated

To be able to modify the properties of each shot in the inspector, let’s use [System.Serializable] above the class declaration:

Forward shot class

This class will handle the behavior of the shots that move throughout a single line in the space, just like the lasers and the multidirectional shot to be included.

We need the class to inherit from MonoBehaviour to be able to modify the gameobject that this script is attached to. Then, we can use the Shot class to modify and use the properties of the shot. And finally, if we want the shot to be animated with different colors, we need to modify the sprite color through the Sprite Renderer component of the gameobject.

If the shot is set to be animated, we’ll need to initialize the respective Sprite Renderer and the delay to change color while traveling in the space. Then we need to call the respective coroutine to start changing colors:

Then to handle the movement in a single line, depending on the shot’s property, let’s move the respective shot in the Update method:

The shot will be destroyed if it moves out of the space bounds declared in the Shot class.

Multidirectional shot class

Now, to handle the pellets instance with the new shot, let’s inherit from the ForwardShot class to have the same behavior but let’s instantiate the pellets when the shot with this script attached collides with an enemy:

Note: I didn’t use virtual methods or overriding as the ForwardShot class doesn’t use the OnTriggerEnter2D method in this case.

If I attach the MDShot script to the new shot, the pellets will be instantiated when I use it in the game:

Enabling the new shot

Now, in order to enable the multidirectional shot for only 5 seconds, let’s modify the respective scripts:

Player class

Let’s create a new array to store the different type of shots that the player can have. Also, let’s keep a reference to the current shot, so that we can instantiate it when needed:

Then, let’s create some methods to handle the selection of the shot and it’s respective behaviors:

With the next method, we’ll make sure that the fire rate included in the Shot class affects the player:

Then, to enable the respective shot when collecting the power-up, let’s call one of the methods from above including the number of the shot in the array as parameter:

When disabling the shot in the coroutine, the default shot (laser) will be selected again.

Now, when the player shoots, we’ll instantiate the selected shot:

Power-up class

Also, in order to spawn the power-ups according to the rarity, let’s use an enum to select the rarity from the inspector:

Spawn Manager class

Let’s divide the different power-ups by rarity by creating new lists:

In the Start method, let’s call the next method to initialize the lists and fill them with the respective power-ups. To choose the right list, we can get the Rarity from the PowerUp script component attached in the prefab of the power-up items:

Now, to spawn a random power-up depending on the probability of it’s rarity, let’s create a new method to return the respective item prefab:

Finally, let’s call the method when instantiating a new power-up in the respective coroutine:

Now, if we run the game in Unity, we’ll be able to use the multidirectional shot without any trouble:

And that’s it, we implemented a new shot! :D. I’ll see you in the next post, where I’ll be showing more features added to my space shooter game in Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D



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
Fernando Alcantara Santana

Fernando Alcantara Santana


A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).