Creating Modular Powerup Systems 2D SpaceShooter
Objective: How to create a modular powerup in unity.
Today were gonna have a break down on how to modulate various powerup systems for our 2D SpaceShooter. The first iteration of our Modular powerup systems will be a speed powerup. Lets start by heading down to our powerup folder and select the first speed sprite from the speed power up folder. The next step will be to drag it into the Hierarchy and rename it Speed_Powerup by right clicking on it or by renaming it in the Inspector. If you haven’t already made individual folders for your enemy prefab and your powerup prefab go ahead and do so. The next step will be to drag our speed powerup just above our player and resize the x, y, z scale to 0.5. Also don’t forget to change the sorting layer to foreground so our speed power up doesn’t seem behind the background in the scene view. Next lets add our Box collider 2D and make sure trigger is checked so our speed powerup can interact with our player and pass through are laser and powerup. The next thing we will need to add will be a rigid 2D body for our speed powerup and don’t forgot to set the gravity scale to 0. Now lets attach our speed powerup script by add component or by dragging it into the Inspector. Your gonna follow this up by taking your Speed_Powerup in the hierarchy and dragging it down to your Powerup prefab folder. You see this all below in the video gif
Now we have to decide what powerup will be selected when the player comes in contact with the powerups. So now we need to add a Integer system called ID powerup. The reason for this is to help our Player know which powerup was collected and to apply the correct powerup effect. Your probably wondering how do we do that, let me explain by making some logic for the ID powerup. Lets write out the pseudocode for our ID system powerup its gonna be ID for powerup follow by 0=Triple Shot and 1 = Speed and then 2 = Shields. Now lets code it by making it private and then add a integer named powerupID follow by SeralizeField attribute. Now we can assign the powerupID in the inspector in unity and now we can check whether its a speed powerup or triple shot. So how do we translate it to code I ‘ll show below in the video gif.
This how you Modulate a Powerup in Unity.