Implementing a Negative Pickup for the Player in our 2D Galaxy Shooter in Unity!
Objective: To bring some chaos to our game by implementing a negative pickup for the player.
For this particular feature I have decided to choose 2 negative effects for the same negative powerup. These are going to be that the player’s speed is set to 0 and they can’t fire their weapon for 4 seconds total. You better hope you don’t collect this powerup and you at least have a shield to withstand the barrage from the enemies! Let’s get started with the powerup itself…
Negative Pickup Powerup
As I have recently created a bunch of new powerups for the game, I won’t run through the steps again and instead direct you to an article that will assist you with this:
https://christopherhilton88.medium.com/creating-a-health-collectable-for-our-2d-galaxy-shooter-in-unity-1ec6c88abbfc
Let’s Update Some Code
Firstly let’s start in our ‘Powerup’ script :
We need to create a handle to the ‘ThrusterController’ script as we need to call a coroutine within this which is going to assist in handling the player not being able to fire their weapon. Don’t forget to GetComponent<>() and also null check!
Now within our OnTriggerEnter2D() method, let’s create a new case within our existing switch statement. It has 2 main priorities which is to call a NegativePickup() method within the ‘Player’ script and also start the NegativePickupRoutine() coroutine as mentioned above. The other 2 lines are consistent with all the other cases.
Let’s now head over to our ‘Player’ script:
Firstly, let’s create a variable that sets the player’s speed to 0 and also create a trigger switch for our NegativePickup powerup.
Then within our Update() method, let’s add a simple ‘if’ check to see whether the negative powerup is enabled and if it isn’t then we can continue to fire our lasers as normal, else, don’t allow to fire lasers.
Next, create the NegativePickup() method that is called from the ‘Powerup’ script when the powerup is collected. This will switch the enabled to ‘true’ and also start the NegativePickupRoutine() coroutine.
This coroutine is going to handle setting the player’s speed to 0 for 4 seconds before returning back to normal.
Lastly, let’s jump into our ‘ThrusterController’ script:
Let’s create a trigger switch for our ‘else’ statement and coroutine.
Now let’s add an if, else check to our existing ‘else’ statement to see whether the negative powerup is active or not. If the powerup is not active, continue to run the existing code, else, if the powerup is active do nothing except run both coroutine’s from the this script and the ‘Player’ script for 4 seconds before returning to normal.