How to Create a Modular Power Up System in Unity 2D

Mohamed Hijazi
Level Up Coding
Published in
3 min readApr 2, 2021

--

Objective

Build a modular power up system for your 2D game. Let’s say your game has around 5 or maybe 10 power ups and you do not want to go too complex with the power ups system, today we will learn an easy and effective way to do so.

Method

Let’s start off by create a C# script and call it PowerUps, create an empty gameObject for your first power up (say Speed Power Up). Make sure to add a Collider2D and check the isTrigger box and add a Rigidbody2D and make sure gravity is set to zero so we can control it’s movement upon Instantiation if needed.

Also create a Player script to control each power up method that will dictate what each PowerUp will do.

As you can see, each method contains what happens upon picking up the power up.

In the Awake method of the PowerUp gets hold of the Player component you created in order to call the power up methods when necessary.

Since there is only one Player in the game, we can easily get hold of it

In the Update method of the PowerUp and if you want it to move upon instantiation, you can write the following code to specify it’s direction of movement and speed (speed value can be serialized for further control).

The idea behind a modular system is being able to create one method in order to take control of many power ups without recreating the code over and over. So an easy way to do it is by giving each power up and ID and then acting upon that ID when the player picks it up. Let’s create a serialized field for power up ID

//Here we have 6 power ups and each ID represents one.
//We add a Range and a tooltip to make it designer friendly
>[Range(0,6)]
>[Tooltip("tripleshot = 0, speed =1, shield = 2, ammo = 3, health = 4, heatseek = 5, weaponFailure = 6")]
>[SerializeField] int powerUpID;

Now to the heart of this system

Power Ups are picked up when the Player collider collides with the trigger based collider of the Power Up, so let’s create an OnTriggerEnter2D method, check we collider with the player using its tag

>private void OnTriggerEnter2D(Collider2D other)
> if(other.CompareTag("Player"))
> if(_player != null)
//Always null check
//code below

Using the ID we created for each powerup we can Create a switch statement in the trigger method. This way and for example, if we set the ID of Speed Power up to 1, then in the switch method we say case: 1, call the player script and call the Speed power up method. You can even play a clip or a VFX effect upon pick up. Here is how the Switch method will look like…

Do not forget to destroy the powerup after this.

From here on you can create as many power ups as you want and you can either place them manually in your game or let them spawn randomly. You can create something similar to the Modular Spawn System we created for the enemy waves.

--

--

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/