Thank the Space Gods! Here Comes The Ammo!!!
Objective: implement a collection system to resupply the player’s ammunition.
Note: This is the continuation of my previous article listed here in which I implemented logic to keep track of the Player’s ammo. Now that we’ve run out, let’s create a new powerup to collect new ammunition and give the Player a little hope! Not too much hope…just a little…
Important Legal Info → Here is the “Ammo” graphic attribution link from pngtree.com.
So your Player has mismanaged his ammo stores and the magazines are empty. The hoards of enemy ships are hurling down the screen, multiplying as the spawn manager does its thing in the background. That sucks!
Thankfully, we are here to implement a new Power-Up allowing the desperate Player to pick up some ammo. Let’s do this…
It probably would have been much quicker had I started with a simple game object to represent the ammo pickup and concentrated on the logical side of things. Nope…
I started by searching for an icon to represent an ammo collection game object. I finally found what I liked at pngtree.com, took the image file, imported it into my assets, and converted its Texture Type to Sprite (2D and UI).
I wanted this icon to rotate as it descends down the screen, and I did so by attaching the following script (see below). This allows the game object to rotate by manipulating its transform.Rotate information in the Update() method.
I then created an empty game object, named it PlayerAmmoPowerUp, and added my PowerUps script (manages my various PowerUps), a Circle Collider 2D, and a Rigidbody 2D.
I then took my existing AmmoPowerUp Icon game object and made it a child of the PlayerAmmoPowerUp. This ensured that the sprite would rotate independently without interfering with the PowerUps script which controls the vertical motion down the screen.
Now it’s time to make a few additional changes to our code. Starting with the PowerUp script, add the new Ammo PowerUp to the Switch statement (remember we gave it a Power Up ID equal to 3 in the Inspector settings above). This will communicate in turn with PlayerScript and execute the PlayerRegularAmmo() method, passing in a random value between 3 and 6. This ensures that each ammo power-up is “unique” so the Player can’t always count on getting a set number of rounds.
Looking at the SpawnManager script, since the power-ups are stored in an array, there’s no need to hard code changes. The script will automatically adjust for the new object and pick one at random from inside the array.
Finally, let’s return to PlayerScript and add the PlayerRegularAmmo() method. This is the method that will run every time the Switch statement in the PowerUps script detects a game object collected with the Power ID corresponding to 3 (Ammo). It takes the randomly generated number (3 to 6), stores it in ammoCollected, then adds this number to the _ammoCount value which is the number of “rounds’ remaining.
Since we don’t want to give the Player too big of an edge, we then call the UpdatePlayerAmmoStores() method, and use an IF — ELSE IF statements to set the min and max range of the _ammoCount. We finish it off by communicating with the UIManager script and updating the ammo count in the UI by calling the UpdateAmmoCount(_ammoCount) method.
I hope you’ve enjoyed this article and will find it useful in the development of your own Unity game projects. Thanks for reading :)