# Creating Modular Powerup Systems

So here I wanted to add a speed power up to the game that was modular and easily editable later on, incase we need better balance. We first needed to add the Speed Power up object that we named Speed_Powerup.

Then added a Box Collider 2D and checked the Trigger button as this will be a trigger. That will say “if this object is hit then increase player speed”.

Next we will be adding our RididBody2D and setting our Gravity Scale to 0 as we don’t want our object to be falling off our screen as gravity was applied.

Now we are going to create a script in which we can control all of our powerups in one place. No need to create a script for each of our objects when we can just use a one to control them all. This will also make it very easy to adjust things later on.

So we create a variable for the speed in which our power up will move and create a variable that will store an ID. This is where this script will be used for multiple powerups.

Here we are controlling that trigger and collider we created earlier. We are saying here that if the other object is the Player then do the following based on what ID we assign to the power ups. Here we assign an ID of 1 for the speed powerup. This will run SpeedBoostActive() method within our player script.

Within our player script we have just created a couple of variables for our _speed one being our speed that is default set at 4f and our speedMultiplier that is 2.

Below our method is ran and see a Boolean that is set to true saying that speed boost is active. Then we make speed which is 4 times, the multiplier which is 2. So we essentially double our speed to 8.

Since we can’t have double the speed for the entire game we need to make sure that this only last a certain amount of time. So we went ahead and created a coroutine called SpeedBoostPowerDownRoutine and this just says wait 5 seconds and then set the speedBoostActive Boolean to false. Return our _speed back to normal. Basically dividing current speed of 8 by 2 which is 4 our original speed.

In created a very modular power up ID script we can use this to attach to any of our power ups and just change their IDs. The only thing that we would need to do is just add the new behaviors with the desired effects and add to our switch statement.

We have just made our game very easy to edit and add features later on by doing just a little extra work now. Thanks for reading!

--

--

--

## More from Richard Carpino

Love podcasts or audiobooks? Learn on the go with our new app.