Step 9 — Speed Powerup & Powerup Modularity

Nicholas Lim
2 min readApr 21, 2021

--

We will now add a second Powerup for a speed boost. The setup for that object is the same as the Triple Shot, which means giving it a Rigidbody2D, Collider2D, and the Powerup script.

On the Player script, the effect of a Speed powerup is as below

Player script

The private _speed variable used in regular movement Translation is increased by a factor of three for five seconds, before being brought down to its initial value by dividing three.

Back on the Powerup script, there’s an issue. It has to know which Powerup it is in order to call the appropriate method on the Player, because we are not going to create a bunch of scripts to do one specific thing each when they are all of type Powerup.

Thus, in the Powerup script, we create a Serialized int field to help with identification, and then assign those int values on the prefabs in the Inspector.

Powerup script

As is written in the code, value 0 will be the Triple Shot, and value 1 will be the Speed powerup, so open the Inspector of the the Triple Shot prefab in Unity and set the Powerup ID value to 0. Do the same for the Speed prefab but set it to 1.

Now that there are two different powerups, the SpawnManager also needs to spawn both randomly. To do that we add an array for Powerup objects in the SpawnManager script and have it randomly pick which one to spawn.

SpawnManager script
SpawnManager Inspector

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response