Thank the Space Gods! Here Comes The Ammo!!!

Michel Besnard
4 min readMar 4, 2023

--

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…

Ammo PowerUp! Very nice!…

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).

.png imported into the project Assets and converted to a 2D Sprite.

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.

RotatingIcon script attached to the Ammo PowerUp Icon (Sprite) game object.
AmmoPowerUp Icon settings and attached RotatingIcon script.

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.

Let’s adjust the Switch statement in the PowerUps script to include the new Ammo PowerUp ID.

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.

Random.Range uses the length of the PowerUp array to select a power-up at random. This ensures the new Ammo PowerUp (or any future power-ups) are included in the mix.
Notice how the last Ammo PowerUp doesn’t allow for an ammo count higher than “25'” the maximum capacity of the Player’s “magazine”. Also notice how the color of the text goes from red, to yellow, to green, giving the Player added situational awareness.

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.

UpdatePlayerAmmoStores() is called when you fire a laser shot and when you collect an ammo power-up.

I hope you’ve enjoyed this article and will find it useful in the development of your own Unity game projects. Thanks for reading :)

--

--

Michel Besnard
Michel Besnard

Written by Michel Besnard

Military member with 35+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.

No responses yet