Western Shooter Devblog #11 | Creating A Modular Powerup System in Unity
To really make this game feel like an arcade shooter, we should start adding some powerups. And what’s a cooler powerup then a BIGGER BADDER GUN?
To create our powerup, first we should make the powerup prefab that the player can pick up.
That’ll do for now!
Since I know I’ll be making multiple powerups for this game, I’m gonna make a powerup script that I can apply to this and all my future powerups to keep things modular!
Cool! And that will be all we need for our Shotgun Powerup Prefab, the script couldn’t be any smaller either!
So how will we spawn these? In a similar way to our enemies, we’ll use our Spawn Manager and use a Coroutine and Random.Range to randomize the spawn rate.
This method allows us to choose how long in-between spawn attempts should be, as well as the chances of the shotgun spawning during a spawn attempt. So here I can set the cooldown to just 1 second, and then the spawn rate to 10. That way every second, the shotgun will have a 1 in 10 chance of spawning! Pretty nifty right?
Now for the Player Script side of things, we need to activate our Shotgun Powerup in a way that will allow us to seamlessly transition from using the revolver, to the shotgun, and then back to the revolver once the powerup runs out. As well as determine how long we want the shotgun powerup to last.
So in our OnTriggerEnter method, once the Shotgun Powerup Prefab collides with our player, we can activate our powerup. Instead of using a timer for our powerup, I think it would be more fun to give the player a limited amount of ammo, so they can use it for as long as they want, but only get 6 shots. So first, It will destroy the prefab so it doesn’t activate more than once or clutter our scene. Next, it will give the player 6 shots with the shotgun. And if the shotgun isn’t already activated, it will call our DrawShotun() method. That way, if the player collects another shotgun prefab while they still have one, it will just refill the players ammo!
I wanted our DrawShotgun() method to act as a toggle for activating the shotgun powerup, so that way I can call the same DrawShotgun() method to deactivate the shotgun, as well as activate it! So the first thing it will do is call our DrawPistol() method which will effectively holster the revolver. It will then activate the shotgun gameobject that is in the player’s hands, and then activate our shotgun boolean.
I reworked our shooting function so that if shotgunActive == true it will fire shotgun rounds, and if it fires it’s last shot, it will call the DrawShotgun() method to deactivate the powerup, and go back to the revolver. Which will then be calling the else statement, which just fires the revolver rounds by default.
So now we have a randomly spawning, shotgun powerup that can reliably and seamlessly activate and deactivate!
We just need to create our shotgun bullet prefab and we should get a fully functioning powerup!
Since we’ll be sending multiple rounds at once as a big shotgun blast, we’ll need to be able to control each individual round since they will also be going in different directions. So I accomplished this by creating an empty gameobject that will act as a container for 5 separate bullets, that are all pointing in different directions, and then made a script that will tell all of these gameobject to move forward in the direction they are facing!
And this is the end result!
In the next devblog we are gonna work on our next powerup, a Speed Boost!