2D Shooter: Adding Collectibles

Sabrina Windsor
3 min readJun 18, 2021

Last time we added the behavior of our first powerup. Now its time to add something visual to it and a way to activate it. This will be in the form of a collectible that the player will pick up.

Objective: Create a powerup sprite that will activate Triple Shot when it is collected by the player.

To start let’s drag the sprite we want to use to represent the powerup from the Assets folder to hierarchy so it can become a new object. This is what mine will look like:

Now we need to add components to it; we need to add Rigibody2D, CircleCollider2D, and a script named “Powerup” that we’ll be using to attach behavior to it. Adjust the collider dimensions if needed, and make sure gravity is set to zero.

Now we want the collectible to move down automatically. So we’ll be using code that’s quite similar to what we used for the lasers in a past article. We also want to destroy the object once it goes off screen.

Before we do anything with GetComponent for script communication, we need to create some methods in the player script that will tell the program to activate the triple shot by setting _isTripleShotActive to true and start a coroutine that will change it back to false after a certain amount of time. An IEnumerator will allow us to use to use the yield command and do just what we need.

Now its time to add some communication between this script and the player, so we can activate the triple shot when it detects collision with the player. This will work quite similar to when we used GetComponent in the enemy script to detect collisions and locate other scripts.

I kept the storage for the collided object as “collision” instead of “other” this time. It makes more sense to me.

Once this is done, triple shot should be activated once the player touches the powerup sprite!

--

--

Sabrina Windsor

Currently learning to code with the help of GamedevHQ in order to someday my my game ideas come to life!