Using 2D Triggers For Power Ups In A Unity 2D Platformer

Adding power ups to your game creates a scenario where it’s necessary to use conditional logic. If the player collides with a power up.

First thing you need to do it is create the item you would like to represent your powerup. We are going to be using a 2x sprite in the gif above for this example.

Once you have chosen your item make sure it has a Collider2D.
We are going to be using a box collider 2D with in the example below.

We want to click the Is Trigger box as well like in the photo above.
This will be used with an OnTriggerEnter2D method later on.

Next we are going to make a tag for the power up. Tags can be found right under the item name like in the photo below.

Click the tag button and scroll down to add a tag and click it.
You will be taken to the tags & layers menu

Click the add button and give the tag a name. We are going to be using DoubleJump in this example. Then we are going to need to assign that tag to the object that is our powerup like the example below.

Next lets turn our item into a prefab make sure you have a prefabs folder created in the project window and just drag it into that folder to make it a prefab.

After we want to make sure your player character has a 2d collider as well along with a rigidbody 2d see example below

We are now going to add the OnTriggerEnter2D method to our script.
This example I will use a player script I created for this game.

The collision variable has an option for Comparing Tags which we will use to pass the tag we created in as a string in an if statement.

Pass in any code of the features you want your power up to have in my example I have a bool that allows the player to jump twice one time with each power up. We then want to destroy the game object upon collision to show that our player has picked it up.

Save your code and test it out

When the player collider passes the power up collider trigger will activate causing the power up game object to disappear like in the example below.



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