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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Is the AWS Solution architect professional difficult to pass?

Why you need to use DTO’s in your REST API

Mapping my Journey through GSoC’20 — Flight.

Secure JupyterLab using Keycloak

TASK-4:AWS VPC with public, private subnets, internet gateway and NAT gateway

A few things to consider before localising your application

For the Layman — What Is a Distributed System? (Part 1)

“Attendance at the Scrum Events is Mandatory”

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ali Emre Onur

Ali Emre Onur

Unity Developer

More from Medium

Determining How Long Powerup Effects Should Last

Introduction to Physics in Unity

Unity Input System Basics

How to Initialize Unity Projects for Git Source Control