2D Shooter: Ammo Collectible Part 1

Objective: Create a collectible that replenishes the ammo supply that spawns when an enemy is destroyed.

Let’s focus on getting the ammo to spawn when the enemy dies. We’ll want to treat our new powerup just like the ones prior, create a prefab, give it a powerup ID and attach it to the spawn manager.

Go ahead and add this to the switch statement in the powerup script as well. We will need to make a new method in the player script for it to be linked to.

For the AddAmmo() method we want to add whatever number is needed to get back to fifteen. We also want to make sure that if the player already has full ammo, it won’t add any more to it. Don’t forget to update the UI method, and we’ll be creating a spawnmanager method and a bool that we’ll get to in the next article.

Now let’s go to the SpawnManager script and creating a method that tells the manager to spawn this powerup 1 in every 5 times an enemy dies.

If you want to increase the chances, make the range smaller, and vice versa.

To get the enemy’s position, we will need to go to the enemy script. We’ll want to grab the coordinates when the enemy gets hit by a laser.

Now the enemies should sometimes drop an ammo collectible on death.

Currently learning to code with the help of GamedevHQ in order to someday my my game ideas come to life!