Shots Fired Thanks to Ammo Pickups.

addam davis
5 min readJun 22, 2021

--

Objective: Create an ammo powerup and spawn on enemy’s destruction.

Last time we added a basic ammo system. Now we are going to make a limited amount of ammo and create a powerup that gives our player more ammo.

To begin we need to create a prefab to act as our power up. I made mine using an empty object and childing three of the laser sprites inside and arranged them to be three in a row with the middle sprite being larger than the two on the side.

Selecting the parent and drag it into the prefab folder then you can delete it from the hierarchy.

Attach the powerup script and make sure to assign an ID# and the audio clip. Then open the script.

The other powerups are set up using a switch statement. With the new ID# we assigned the new powerup we need to add to the statement. We will need to create a method for this powerup to call, we can name it on the switch statement first so long as the names are identical when we create it on the player script.

Now on the player script we need to create that method we call on the powerup script. We need to use this method to add an amount to the max ammo variable. If we want to gamify the amount and make it random you can make a local variable and use a Random.Range to set the range of how much ammo you could get.

We need to adjust our max ammo value to a larger amount, I chose50. We need to create another variable to represent the amount of ammo we reload with; this way we can choose how much we take from the max ammo and give to the current ammo.

In the update method is where we have our basic reload function. We need to adjust it, so it takes a set amount from the max and gives it to the current ammo. to begin we need to subtract the reload variable from the max ammo, then add the reload variable to the current ammo.

This is fine however currently this well never end. The max ammo will go negative, and the current ammo will continue to gain ammo. We need to add a method for the reload to make it to where if the max ammo is less than the reload variable then the variable will only add what the max ammo has left.

In the input method to reload we need to call on this method. This method changed the value of the reload to what the max ammo is. Meaning if the max ammo is less than 15, it will only take what the value is, then after that the max ammo will be zero and the reload amount will also be zero, and the player is out of ammo.

Now we need to adjust the script of the UI manager. We need to create a method to change the max ammo displayed due to reload. In this method we are going to want to minus the amount we take from the max ammo when we reload. To do that we use a local int variable to subtract from the max ammo. Then we update the ammo text.

To decide the int value we take from the max ammo we go to the player script and call the method using the value of the reload variable, similar to how we did the current ammo from the last tutorial.

Using the reload variable means the value will change if the max ammo is less than a full reload.

Next, create a method on the UI manager for manage the max ammo when the powerup is gathered. To begin we will use an if statement to set a max limit to the max ammo, so we can only gather so much ammo.

Using a local int variable, we will add that value to the max ammo. Using another if statement we will add the value up to 50 and no more. Then after that we adjust the ammo text.

Going back to the player script again, we go back to the method that handles the ammo powerup being activated. Using the same random value as before we want to use that value as the amount we add to the max ammo on the UI manager.

To round this out, I want the ammo powerup to only spawn when an enemy is destroyed. To do this we did not add the ID# to the spawn range, and we add an instantiate line to the enemy when it collides with the player’s laser.

That’s it! We have a fully functioning ammo system and powerup that adds ammo to the player. You can experiment with this code and adjust the values to better suit your needs, and I’ll see you in the next tutorial.

--

--