Creating Modular Power-up Systems

Power-ups! We love them, especially on the multiplayer games that we are competing with our friends, don’t we? When I think about the earliest power-up that I remember in a game, Super Mario comes into my mind. If we can catch the running mushroom, Mario grows up and has an opportunity to take a damage without losing a life. While grown up, he can gain the ability to shoot fireballs if he gets the powerup denoted by a flower.

While playing, games give us the perception that we are actually cathing or eating a power-up in order to gain a special power. We know that that’s actually an illusion in the game development, by using collision systems. You can find my previous article regarding collisions systems in Unity here.

However, creating the power-up logic in game may become complicated. In this article, I will try to explain how to create modular power-up systems in a space shooter game. There are 3 power ups in this game; namely:

  • Triple-Shot: Player shoots 3 lasers instead of 1 for a given amount of time.
  • Speed: Speeds up the player for a given amount of time.
  • Shield: Allows player to crash/to get shot for 1 time without losing a live.

In order to activate the correct power-up, player script has to gather the correct information from the script that manages the power-ups. The basic idea that would come to your mind might be creating separate scripts for each power-ups. But, imagine that you are creating a game with tens or maybe hundreds of power-ups.

For script communication, we use “GetComponent” in Unity. GetComponent simply enables us to access the desired game object’s script by creating an instance of that object. With the initial idea of creating separate scripts for separate power-ups, we would have need to access to every script individually.

However, using GetComponent in Unity is costly and we need to avoid unnecessary usage of GetComponent command as much as possible. Thus, we will try to group and limit the power-up scripts. In our case, we are going to use a single Power-Up script as we only have 3 power-ups.

In the space shooter game, we are using a spawn manager game object and script in order to control the spawning of the enemies and the powerups.

In the Power-up script, we can reference to the player script and create an instance of it. But how are we going to understand which power-up has gathered by the player? By creating a new variable, that we can attach different integers to each power-up, so that we can differentiate the power-ups easily. In order to keep the variable private, we shall use SerializeField attribute. The beginning of the powerup script is provided below:

In our project window, we enumerate the power-ups according to our desire. For instance, I have given the number 1 for the triple sot powerup.

As the power up ID’s are on the same script, we can manage the behavior of the player correctly:

The player script is also provided below:

We have successfully created a modular power-up system, which works perfectly. You can find a sample gameplay below:

Unity Developer