Player Missiles part 2!

addam davis
3 min readAug 10, 2021

Objective: Create an ammo system for the player’s missiles.

During the last tutorial we created a new type of attack for the player, a missile that chases the closest enemy. However, as the code stands now the player can create an unlimited number of missiles in quick succession. We need to limit the number of missiles the player has at their disposal and limit how quickly the player can fire those missiles.

Dealing with the first limitation we want to place on our player, we need to create a variable that represents the missile ammo.

Just like we did with the laser ammo, we want to set a variable to represent the current amount of ammo, and a variable to represent the maximum amount of ammo.

Now we need a method that will subtract from the current missile ammo.

We want to call this method every time the player instantiates a missile. We can set the limit that as long as the ammo variable is higher than zero, we can instantiate a missile.

We need to set a limit to how quickly the player can launch missiles. We do this using the same formula as we did with the laser. We create a variable to represent when the player can fire, as well as a fire rate.

Now we need to set the calculation of can fire = time plus the fire rate and set the limiter that the player can only fire when time is larger than the can fire variable.

This is it, now the player has a limited number of missiles and can only shoot once about every two seconds. In the next tutorial we will go over setting a visual representation to show the player how many missiles they have.