Unity Dev: Powerups

There once was a little ship that shot little lasers. The ship was happy for some time until it realized it could be… more. Welcome to powerups.

Triple Shot: Is one laser ever enough? Of course not. We need a powerup to give us 3 lasers at once for a little bit.

Speed Boost: If you move faster, you kill more alien ships. So why not?

Shield: It’s like an extra life for free! On top of that, it gives us an excuse for another cool visual.

Thought Process

Initial Setup

Drag the image into your scene and rename the object to “Triple_Shot_Powerup”. Create a new C# script for your power-ups (we’re going to use the same one for all three) called Powerup.cs and attach this to your new object. The Powerup object will also require a Box Collider 2D component set as a Trigger.

Note: So far, only our Enemy objects have a Rigidbody 2D because they were the main object our Player and Laser objects would interact with. However, now we need the Player to be able to pick all these items up, so you’ll want to apply a Rigidbody 2D component to the Player object. As always, don’t forget to turn gravity off! We’re in space, after all.

Once you’re done the legwork with the Triple_Shot_Powerup item, drop it into your Prefabs folder so that we can spawn it into the scene.

Some quick work in the Powerup.cs will get item moving across the screen while also ensuring it gets cleaned up from our Scene once it’s left the viewable area.

Remember to have a [SerializeField] with your _speed variable so your game designer’s can tweak the speed of the Powerup objects

I already know that I’m adding additional Powerup types so we’ll save ourselves some time by coding the Powerup script in a modular fashion.

Introducing a Powerup ID variable will help us when it comes to activating the appropriate powers on our Player. We’ll be creating a list variable with all of our Powerup prefabs in it which acts as an array and will be zero indexed. This means, basically, that we’ll start counting our items from 0 instead of 1. Your Triple_Shot_Powerup will have an ID of 0.

Pickin’ Up the Power-up

This is my final version of this script. You can use this as a reference, but it’s important to remember that you’ll be seeing errors in your IDE and your game won’t run if you don’t have coordinating functions established in your Player.cs

We’ve seen the OnTriggerEnter2D(Collider2D other) void before so we already know that other is going to give us information about the Object that’s colliding with us. In this case, we want to run some code when an object with the Tag of “Player” hits us (by “us”, I’m referring to a Powerup prefab. We’re in their script, so we can think of it as “us” when thinking through our coding).

First up, we’re stashing our Player component in a local variable and null checking it to help figure out what’s going wrong if our code doesn’t work.

Now you’ll see a new concept, which might seem scary at first glance, but it’s basically an if statement. You can read more about switch statements here.

When a Player collides with a Powerup, it’s going to have an ID attached to it. In this case, the ID will be 0 because we haven’t set up the other Powerups yet. This means that our code is going to dive into the Player component we stored, and run the ActivateTripleShot() function that we… still need to define! Let’s get going on that.

Making the Triple Shot

Drag 3 laser prefabs into your scene under a blank object called “Triple_Shot”. Organize them in such a way that it looks good coming out of your ship, then drag the Triple_Shot object into your prefabs. Once we get them firing, you may need to tweak the positioning and you can do that by double-clicking the prefab and making your adjustments.

In Player.cs we’ll need a couple variables to make this Powerup.

We need a reference to the new Prefab we’ll be firing, we need to know if the Triple Shot is active, and we need to know how long it’s going to last.

In your Unity editor in the Player inspector, populate the variable with your Triple_Shot prefab and we’ll be good to continue.

We were already referencing player.ActivateTripleShot() in our Powerup.cs so we need to create that. From there, the Triple Shot activation really only requires 2 things. Tell _isTripleShotActive to be true, and then begin a Coroutine that will turn it off after the _tripleShotDuration time is over.

Now, we could simply modify our InstantiateLaser() script to use a simple if statement, but I wanted to make it a little more open ended to introducing othering Laser variations later on. I modified InstantiateLaser() to accept an argument of the prefab I want it to Instantiate.

From there, I modified our OnFire() event with an if statement to fire the Triple_Shot prefab if _isTripleShotActive is true.

Now you can get on with the Speed Boost and Shield powerups. What do you think the code for them would look like? What variables do you need to change, or prevent from changing, to emulate their effects?