Made With Unity | 2D Space Shooter Part 7: Power Ups And Scripts Modifications

Rehtse Studio
8 min readMay 18, 2022

In this series of article / tutorials we are going to be making a small 2D Space Shooter using Unity3D and later on using the New Input System.

We are going to give the Player some Power Ups to acquire during the game and we are making some code modifications.

POWER UPS

TRIPLE SHOT POWER UP

Drag the Laser prefab on the Hierarchy, duplicate(right click or CTL + D on windows) the Laser object. Edit the Transform on the 3 Laser object and create a new Empty Object called Triple_Shot. Make sure the the Transform Position on the Triple_Shot object is set to 0 in the X, Y and Z.

Drag the 3 Laser objects inside the Triple_Shot object. Then drag the Triple_Shot object to the Prefabs folder, it will turn blue.

Create two new Empty Object, one will be called [ — CONTAINERS — ] and the second one [ — POWERUP CONTAINER — ]. Drag the following object inside the [ — CONTAINERS — ] object: [ — POWERUP CONTAINER — ], [ — LASERS CONTAINER — ] and [ — ENEMIES CONTAINER — ].

I will drag one of the sprites to the Hierarchy from my Assets folder, change is name to PowerUp_TrippleShot and add the following components: Circle Collider 2D, Animator and a Rigidbody 2D, make sure that Gravity Scale is at 0.

Create 3 new folders: Animations, PowerUps, Tripple_Shots and create a Animator Controller inside the Triple_Shots folder called TripleShotController (look the images for guidance). Selecting the PowerUp_TripleShot object, drag the TripleShotController in to the Controller filed on the Animator component.

Open the Animation windows (Windows->Animation->Animation). Selecting the PowerUp_TripleShot object, on the Animation window, press Create, name the file TripleShot_anim and save it on the Triple_Shots folder.

Drag the remaining sprites from the Project window to the Animation window, creating a animation to the object.

Drag the PowerUp_TripleShot object to the Prefabs folder, delete the PowerUp_TripleShot object from the Hierarchy.

SPEED POWER UP

For the Speed Power UP will do the same steps but with different assets. Drag one of the sprite from the Speed Power Up folder and add the necessary components. Will create a new Animator Controller named SpeedController inside the a new folder called Speed in the Animation → PowerUps folders. Once done drag the PowerUp_Speed to the prefabs folder.

Selecting the PowerUp_Speed prefabs, select Add Tag…, create two new tags (image). Back on the Prefab folder add the newly created tags to the PowerUp prefabs.

NEW SCRIPS

POWER UPS SCRIPS

Create 3 new scripts: PowerUps, PowerUpSpeed and PowerUpTripleShot. Inside the Scripts folder create a new folder called Power Ups and drag and drop the PowerUpSpeed scripts and the PowerUpTripleShot.

Inside the PowerUps script:

Line 10 to 15 I’m getting the necessary reference to the Player script component. In the Update() method I’m moving the object down and once it passes a certain Y axis it will destroy itself; line 26 if the power up touches the player it will call a method called ActivatePowerUp(), once called it will destroy the power up game object.

Did you notice anything on this script?

On line 8 instead in using private or public for the variable, we are using protected and on line 34 we are also using protected with virtual. Why? keep reading :)

On the PowerUpSpeed and PowerUpTripleShot script:

👀soooooooooo what is happening? why so short?

You may notice a change on line 5, where are used to see at the end of the class name “: MonoBehaviour” but on this script we are using the same name of the PowerUps script.

WHY?

Well this is what we called Inheritance, in a way we are copying or having the same access to variables and method from one script. Now, in order to have access to the variables and make changes to a method we need to use specifics reference. On the variables we need to reference it as protected (remember the _player variable) and on the methods use protected virtual (ActivatePowerUp()). By doing this both scripts can use the _player variable and add more logic inside the ActivatePowerUp() method. On the method we are adding that the power ups will only call for specific method inside the Player script, by using base.ActivatePowerUp() we are accessing the Destroy(this.gameobject) inside the method that is on the original PowerUps script.

Add the scripts to there corresponding prefabs.

POWERUP SPAWNER AND ENEMY SPAWNER SCRIPTS

Now that we have learned about Inheritance, we are going to make some changes to the spawn logic.

Open the SpawnManager script and make the following changes:

Not major changes just organizing some variable and on line 7 we are introduce the variable of type List<GameObject> from one variable we can have reference to multiple GameObject.

Create a new folder inside the Scripts folder called Spawners, and inside the folder create two new scripts: SpawnEnemies and SpawnPowerUps

Now open the new scrips and makes the following changes😅:

Soooooooooooooooooooooooo I think you know what we are doing here😂; remove the MonoBehaviour on line 5 and just the name of the SpawnManager script. It looks empty but Ill later show the difference.

SCRIPT UPDATE

PLAYER SCRIPT

Added the new variable to hold the Triple Laser object and on line 63 and line 67 I’m passing the lasers object to a method that has the Instantiate line, to keep it organize and not repeat the same line twice; now drag the Triple_Shot prefab to the field on the Player scripts and create a new tag, TripleShot, and add it to the Triple_Shot prefab.

LASER SCRIPT

Line 25 is just to remove the TripleShot object that is left behind ones the lasers are destroyed.

HIERARCHY CHANGES

Select the SpawnManager, remove the SpawnManager script component and add the new one, SpawnEnemies and SpawnPowerUps, fill out the script has show on the image. Since both script are Inheriting from the SpawnManager script I can have different objects and different spawn time for enemies and power ups.

Since there are TWO scrip component on the SpawnManager object, you notice the change on line 25 on the Player scripts, that “[]” next to the SpawnManager name is telling the you that the _spawnManager variable will be of type Array ( a form of list) and on line 40 when getting the component, is not GetComponent<SpawnManager>() is GetComponents<SpawnManager>(), notice there is a “s” at the end of GetComponent. So on line 103, on the Player script, I need to find both component and let them know that the player has died.

Hit Play

🎮PART 8 🎮

--

--

Rehtse Studio

Game Developer sharing content around Unity, programing and tips and tricks in game dev.