Giving your player more Ammo

Esteban Ibarra
4 min readApr 21, 2021

Today's challenge is to create a collectible that resets the player ammo so they can shoot again.

Sounds easy enough! All I need to do is add another condition to the powerups switch case, and a function inside of the player to reset the _ammoCount variable to 15. So let’s do it!

I’ll begin by taking the current collectible powerup and desaturating it to greyish color and modifying the text with the power of GIMP and then change the value to give the illusion of animated glowing.

Next, I dragged the frames of this animation into the editor, created an animation in Unity called Powerup_Ammo_anim, scaled it to .5, gave it a Rigidbody2D (gravity set to 0) and BoxCollider2D (set to trigger), attached the powerup script to it, set the powerup ID to 3, dragged the powerup sound into the script as well, tagged it as a powerup and then renamed it to PowerUp_Ammo before prefabbing it.

Let’s take this step by step:

Drag the frames into the scene, save the animation as PowerUp_Ammo_anim in its own folder (or wherever you want). And scale it to .5.

I renamed it to PowerUp_Ammo a bit early, it doesn’t matter when you do it, it’s just helpful that you do it. Next, I put a Rigidbody with gravity set to 0 and then a BoxCollider2D set to trigger.

Don’t forget to resize the BoxCollider2D box.

Finally, attach the script and we’ll give it a new unique id of 3. We’ll also drag the powerup sound effect into the proper slot. The powerup won’t function without it if you forget to place it.

Finally, remember to tag it as a PowerUp. I spent more than a few hair-pulling times figuring out what went wrong when the powerup wasn’t working.

OK! The GameObject is ready! Let’s prefab it!

Go ahead and delete it from the scene, we’re going to begin spawning it from the Spawn_Manager.

Select the Spawn Manager, increase the number of Powerups from 3 to 4, and drag the new Ammo PowerUp into the last slot.

And that’s all there is to it! Well, not quite yet. We hard-coded 3 items into the spawner, but we can actually make it so the spawner will dynamically spawn a random object from your set. Let’s head into the code and do that.

Remember that our powerups are stored in an array, a setlist of objects. Our current code is “Hardcoded” to the size of 3.

But what if we wanted to add more powerups? Our array's size would grow, and we really don’t want to be coming back to this function to type in new numbers. If we can somehow get the size of the array and use that instead of a set number, We can let Unity do all the work when we put in a new powerup! Let’s see if there’s a way we can dynamically figure it out.

Ok, so there we have it! Unity gives us Variable.Length to figure out the size of the array! Let’s put it in!

Fantastic! Now our spawner will dynamically spawn 3 powers, 5 powerups, or even a hundred if we wanted and we don’t need to touch our code anymore!

Next, we’ll have to go into our Powerups Script and write out a case for what happens if powerup #3 comes out.

In the OnTriggerEnter2D function of PowerUp.cs:

We added a new case, case 3: where we call the function AddAmmo inside of the player script. Now all we need to do is make it!

OK! Everything seems to be in order, let’s see it in action!

Awesome! We’re losing ammo, the powerup comes down and we get another 15 shots!

--

--