Creating a Powerup in Unity

Brian Ratnasinghe
3 min readJun 28, 2021

The next feature I created for the game is a powerup feature. Here we will focus on creating our first powerup which is called Triple Shot. This powerup will allow the player to shoot three lasers instead of one. the first step here is to actually create the Triple Shot itself. What I did here was create a new prefab called triple shot which has three separate laser positioned inside of it.

Now we need to set up some logic within the Player GameObject’s Player script component to spawn the Triple_Shot prefab. We fist need to create a variable within the script to keep track of when the powerup is active.

When _isTripleShotActive is set to true the FireLaser method needs to instantiate the Triple_Shot prefab instead of the original Laser prefab. This can be accomplished using If statements.

Next I coded the ability of _isTripleShotActive to be changed from false to true. I created a specific method for this which will be called from the Powerup GameObject created later on.

The Coroutine TripleShotPowerDownRoutine is called such that the powerup only lasts 5 seconds.

Now the player is ready to have its fire type changed the actual powerup functionality needs to be created. Before going over the code lets check out the prefab itself.

Here is the initial sprite for the prefab, which is implemented into the prefab using a sprite render component. Next we actually to implement an animation component to actually animate the powerup. To do this I simply opened an animation window in Unity and dragged in the files for each frame of the animation.

Doing this automatically creates an animation component for the prefab, so I can go back and make changes to the animation whenever. Here is what the animation looks like.

The last components that needed to added to Triple_Shot_Powerup prefab we a Circle Collider 2D and a Rigidbody 2D so that we can code in a way for collisions to activate the powerup.

Lastly we just need a script component to call the Player’s TripleShotActive function after a collision using an OnTriggerEnter2D method as we have discussed in previous articles.

Now the Triple Shot Powerup functionality is complete. Let’s look at the final outcome.

--

--

Brian Ratnasinghe

Here I am documenting everything I am learning in order to become a successful game developer.