Triple Laser Shot! — Power Up Collectable Design

Objective: Create a PowerUp collectable object and script its behaviour

As anticipated in the previous tutorial, we are going to need to deactivate the triple shot by default and activate it upon collecting a gameobject. This means that a little more design awaits us.

  1. The power up should be a 2D object, randomly spawned, travelling down from top side of the screen (such as the enemy)
  2. It is lost once it reaches the bottom (no top respawn)
  3. It should be destroyable by means of lasers or other collisions (my choice)
  4. It has to interact with the player on collision (trigger) in order to be collected (and removed from game)
  5. Once collected it activates the triple shot on player for a certain amount of time

We’re going to re-use as much as possible and try to script the behaviour in a way that it can be re-used itself for other power ups.

First of all let’s create the object. I simply chose a sprite and dragged it onto the scene. Adjust the name, the scale and other settings if you want and add the 2D collider. Remember to set it as trigger, adjust it if you need. Since the player has a rigid body component, it is not necessary on the power up.

Set the position to zero, add a good tag and make it a prefab.

Time to script. Let’s create a PowerUp Script and attach it to the prefab. My plan is to use the same logic used for the enemy so I duplicated the methods and variables… I am thinking of making a little refactor and use an interface in the future, who knows.

A few variables in the new script. They’re almost the same as in the enemy, but here is also the duration value, since the power up will have a cooldown.

Move and CalculateSpawnLimits are exactly the same as the enemy. RespwanAtTop will meet our requirement: no top respawn.

So, now the power up moves downward, and gets destroyed when outside the screen. Let’s proceed on the interaction.

We are going to use the tag logic to check if we’re colliding with player or laser and to recognize what type of power up we are (for future implementation of different types). The power up is always removed upon collision. The result is as follows:

  1. Laser collision: both objects are removed
  2. Player collision: something happens in the player and the power up is removed
  3. Other collision (e.g. enemy): just the power up is removed.

Time to activate the power up! Did someone say Coroutine?

This is a very simple logic: when collected, the power up will cause the player to start a coroutine. The coroutine takes the power up script as parameter, carrying all information such as gameobject tag, duration etc. Once the tag is read, the ability is activated and the deactivated with usual cooldown logic after the given time (duration). It means that during that time the bool is true and the player shoots as hell, after that the bool turn false and no more triple shot… until next collection.

Last thing to do is to set up the spawning. For this we will use the spawn manager Scrip. First: create an empty gameobject PowerUpContainer, as already did for enemies and lasers.

Let’s add a few changes in the variable section of SpawnManager.

Few things to notice: we now have a position for power ups, a spawn rate range (min/max) for power ups, a new container for power ups and a list of power up prefabs, because in future we will have more than one and in this way we can handle them very easily from the start.

Now we need a spawning coroutine and, of course, to start it.

The coroutine will be extremely similar to the enemy spawning one but with a few difference: there is no constant rate/cooldown, so each time a new power up will spawn after a random time and the power up object is passed as a parameter. In fact, the choice of which type of power up has to be spawned is given to the start method. I moved the yield return statement at the beginning of the method in order to avoid power ups to be spawned always at the very beginning of the game.

Instead of starting 1 coroutine, I started 3 by iterating over the prefab list. In the future we could even move the range of spawning rate in the PowerUp script in order to have different values for each prefab.

Now that the code is ready, let’s drag the prefab and the container onto the script component of the SpawnManager. It’s interesting to notice how the list is handled in the inspector, with a useful and comfortable set of buttons that add or remove elements.

That’s the result, it will be even cooler when more power ups will spawn together!

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams