Unity Guide

Creating a power-up for your game in Unity

A quick guide about how to implement a power-up item in your game with Unity

Fernando Alcantara Santana
Nerd For Tech

--

Objective: Create a power-up item for a space shooter game in Unity that makes the player shoot 3 lasers instead of 1 for 5 seconds.

Hello again! In one of my last posts I showed how to upgrade a prototype to the next level in Unity. In this post, I’ll show how to create a power-up item for the space shooter game to make it look like a real video game even more.

Creating the power-up prefab

First, we need to create the prefab for the triple shot, so we just need to instantiate a single gameobject that contains the 3 lasers when enabling the power-up. So, let’s drag 3 laser prefabs into the scene:

You’ll see that the lasers appear in the default place of the prefab, so we need to move them to the position we desire so that they appear right there when I instantiate a copy:

Move the laser prefabs as you wish.

Once the position is decided, create an empty gameobject to store the 3 lasers as children and make it a prefab:

The new prefab will look like this and a copy will be instantiated every time the player shoots with the power-up enabled:

Creating the game mechanic

Now, in order to implement the power-up mechanic in the player, we need to open the Player script attached to it and create 2 new variables:

_tripleLaser will store the triple shot prefab and _tripleShotEnabled will decide if the power-up is enabled.

Then, in the function that instantiates one simple laser shot, we need to handle the _tripleShotEnabled condition to instantiate the respective shot prefab:

We don’t need to add a new vector to the initial position of the triple shot as we already saved the prefab into the position we wanted.

Don’t forget to drag the triple shot prefab to the Player script in the inspector or nothing will be instantiated when the power-up is enabled:

Now, to verify the mechanic, we can enable the power-up while running the game:

Voilà! the player shoots the triple shot when the power-up is enabled.

Creating the collectable power-up prefab

Choose the sprite that you want to be the collectable item that activates the power-up and drag it into the scene:

Resize the sprite as you wish.

Then, like we did in a past post to handle the interaction with the player:

  • Add a collider component and enable the IsTrigger option
  • Add a rigidbody component and set the gravity scale to 0
  • Create a script to handle the power-up movement and its triggers

Also, modify the collider to fit the power-up sprite and avoid triggering actions from a far distance:

Once you are satisfied with your power-up object, drag it to the prefab folders and let’s open the script to implement the collection mechanic.

Implementing the power-up collection mechanic

Once we open the power-up script, let’s create 3 variables to store the speed of the power-up item and the limits of the game’s visible space to instantiate the power-up item correctly:

Then, in the Start function, let’s assign the position of the power-up item so that it gets down from there:

Choosing a random X position between the limits will position the power-up item where the player can collect it while it goes down.

Now, like for the enemy behavior, let’s create a function that brings the power-up item down but let’s destroy it after it reaches the limit of the space. Then add it to the Update function to handle the item once it’s spawned:

If we run the game in Unity, the item will go down at the speed we chose and then gets destroyed from the scene:

Next, to handle the collection mechanic, we need to open the Player script and create a public function to be called and enable the triple shot:

Then, in order to give limited time of the triple shot power-up to the player, we can create a coroutine that disables it after 5 seconds:

Start the coroutine after the power-up was enabled using the StartCoroutine function:

Finally, open the power-up script and use the OnTriggerEnter2D (if your game is 2D) to handle the collision with the player using the appropriate tag. If that’s the case, use the GetComponent function to communicate with the Player script and call the function that enables the triple shot:

Once we run the game in Unity we’ll see that the trigger works and enables the power-up for 5 seconds:

And that’s it, you can create a power-up for your game in Unity! :D. I’ll see you in the next post, where I’ll be showing how to animate sprites in Unity for the space shooter game.

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

--

--

Fernando Alcantara Santana
Nerd For Tech

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