Powerup Series: Collecting the Powerup

Part 3: Triggering the powerup behavior on collision

Erin Kerr
2 min readOct 30, 2022

With the Triple Shot and the Powerup objects built, it’s time to build the collection functionality to trigger the Triple Shot

Laying groundwork for collisions

First, we’ll need to add a Collider and a Rigidbody to the Powerup object. Next, we need to ensure “Is Trigger” is enabled and the Gravity Scale is zeroed out. Then, we’ll need to adjust the size of the collider to better fit the sprite of the Powerup object.

Adding a method to activate the Triple Shot

Since our Powerup object will house the collision behaviors, we’ll need a public method in the Player script that allows the Powerup to activate the Triple Shot.

Adding Collision Behaviors

Then, in the Powerup script, we’ll define an OnTriggerEnter2D method. When the Powerup object collides with the Player (based on the Player tag assigned), the Powerup will get a reference to the Player script and call the public method to activate the Triple Shot. Then, it will destroy the Powerup object to give to appearance of collecting it.

Now, when the Player collides with the Triple Shot Powerup, the Triple Shot behavior activates and the powerup object is destroyed. As it stands right now, once the powerup is collected, the Triple Shot is enabled for the rest of the game. Next time, we’ll make the Triple Shot behavior temporary.

--

--