Creating Modular Powerup Systems

Day 20- Making 2D Space Shooter Game

Power-ups are a type of game reward that allow the player to customize their experience by altering gameplay for a short period of time. Power-up items are programmed into a game and sometimes require a player to overcome a challenge, such as winning a difficult fight with an enemy or solving a complicated puzzle, in order to achieve the benefit.

Despite the wide use of power-ups in video games, little is known about their effect on gaming experiences. Till now we created a Triple shot power up in our Space shooter game. Now explore how to add new power-ups and randomize them using a modular power-up system.

Now, lets start off by adding a new power-up speed which will increase the speed of player for some time.

First lets create a variable which is going to store a value by which the speed is going to be affected as well as a bool variable which will tell the game if the speed power-up is active or not.

Now when the player takes the speed power-up, SpeedPowerup() method is called which is going to set isSpeedPowerupActive variable to true and start a coroutine SpeedPowerDownRoutine()

In this coroutine, the game waits for 5sec before ultimately resetting the isSpeedPowerupActive variable to false and the time being the speed power-up length.

Now whenever the isSpeedPowerupActive variable is true, the palyer movement will be multiplied by a _speedMultiplier variable thus increasing the player speed.

First create a variable which is going to store the ID of each power-up, 0 for Triple shot and 1 for speed. Change the value in inspector to be as such.

Now create a if else statement for detecting the powerupID inside the null check and call the power-up method from the Player script which will then activate the power-up.

Finally for spawning the power-ups, we are using an array. So what is it and how do we use it?

Simply put, Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare a variable as array, you will need to add [] brackets behind the datatype.

Here, we are creating a array variable of type GameObject. Note that the variable is going to store only the value of type gameobject and within its memory allocated.

You can access the elements inside the array as such. Ignore the red underlie as the variable was not created and I only used it for showing how to access the array. You should also note that the element number starts from 0. So if you set your total element number to 2 the it will store value as 0 and 1.

You can now assign the number of gameobject you want to add in the inspector. After assigning, you will see that number of empty element boxes where you can drag your prefab. We will use this array for power-up spawn so drag the power-up prefabs.

You should also note the element number, it is like your entry number which will be used to call your prefab so be mindful of which prefab to call when you call it as powerup[0] or powerup[1].

Now you can use the same script used to instantiate Triple shot and instead of prefab you are going to use powerups array. Here, powerups[powerupID] takes the prefab assigned in powerID element to instantiate and powerupID is a random number between 0 and 2.

And this will be the final result.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store