Wait, This is Starting to Look Like a Real Game!

Matej Marek
4 min readApr 16, 2021

Here we are, the point where this little project starts to look like something… A player reacting to the user input, with the ability to shoot down the ever spawning enemies with a laser weapon or colliding with them, with some basic artwork and effects. There is still a long way to go, but yes… it is starting to look like a real game!

And the next thing to add is a power-up - a triple-shot! Because one laser is never enough!

There are four steps to adding this power-up!

  • Setting up the ‘pickable’ prefab that will represent the power-up.
  • Creating the triple-shot prefab, which will replace the regular laser for the power-up duration.
  • Implementing a ‘PowerUp’ script that will handle the logic.
  • Editing the ‘Player’ script to Instantiate triple-shot prefab instead.

Step I - Power-up Prefab:

For this GameObject, I will again be using the sprite from the GameDevHQ Asset collection. Next up, since it was a bit too big, I have scaled it down and added a ‘Circle Collider 2D’ component to it, which I also adjusted to match the shape of the GameObject.

Note: Don’t forget to add a ‘Rigidbody 2D’ component to the Prefab and set the Gravity down to 0!

Step II - Triple-shot Prefab:

For this one, I will create an empty GameObject and give it three child laser prefabs with different positions to match the guns on our player sprite. Each of them has the laser script on it, so we don’t have to make any adjustments to its behavior.

Step III - PowerUp Script:

Let’s open up the script and create the basics first - the movement.

This script will make the power-up move downwards until it is beyond the edge of our screen, and then it is destroyed.

Now let’s create the behavior when it collides with our player. For this, we will use our good old friend, the ‘OnTriggerEnter2D’ - and we have to remember to set the ‘Circle Collider 2D’ option ‘Is Trigger’ as active.

In this method, we check if the object that the powerup collided with is the player. If it is a player, we then call for a method in our ‘Player’ script, which will activate the Triple-shot power-up!

Step IV - Player script.

Now, all we need to do is implement the rest of the logic in the ‘Player’ script.

First up, we need to create a few variables:

We then populate the _tripleShotPrefab variable with our triple-shot prefab. Next, we have to create the method that we call in the ‘PowerUp’ script - ‘ActivateTripleShot().’ Here, we also have to create a Coroutine that will turn it off after the _tripleShotDuration.

And now that we have this done, we have to adjust our ‘FireLaser()’ method to differentiate which type of laser it should fire. But since I might implement some different lasers in the future, I also decided to create an ‘InstantiateLaser()’ method, which takes the’ _laserType’ GameObject as a parameter.

And here is the implementation in action…!

One thing to note is that if we take a look into the Hierarchy, we can see that there are clones left over after we destroy our laser prefabs. This is happening because we are not destroying the parent GameObject of the triple-shot prefab.

To fix this, all we have to do is to make a slight update to the ‘Laser’ script and the Clones of the prefab will be destroyed as well!

But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!

--

--