Ammo Count | Unity Developer
At this point in my space shooter game, the player is quite powerful with added thrusters and a stronger shield to stop enemy fire. Let’s balance the scales and add an ammo count for the player and give them a limited number of shots. There will be a collectible that the player can collect to restore the ammo count, but one task at a time.
- Create a global variable for the ammo count. For my game, the player will have 15 shots. This will be set in the player script.
[SerializeField]
private int _ammoCount = 15;
2. Create a method to calculate the current ammo. This will have an int parameter. The method will have the value of the ammo count plus the parameter passed in.
public void AmmoCount(int shots)
{
_ammoCount += shots;
}
3. This new method will be called from the method where the laser is fired, or when the player presses the space bar. The important part of my FireLaser method is where the AmmoCount is called. The parameter of -1 means that one shot will cause the ammo to go down by one.
public void FireLaser()
{
_canFire = Time.time + _fireRate;
AmmoCount(-1);
if (_isTripleShotActive == true)
{
Instantiate(_tripleShotPrefab, transform.position, Quaternion.identity);
}
else
{
Instantiate(_laserPrefab, transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
}
_audioSource.Play();
}
4. Create a global variable for an audio clip to play when the player has no more shots left. Add SerializeField so this can be attached to the player in the Unity Inspector View. Do the same with the variable for the ammo count if you haven’t done so already.
[SerializeField]
private AudioClip _noAmmoSound
5. Use an if-statement to play the audio clip when the player has no more shots left. This if-statement needs the return keyword. Return will terminate executing anything that comes after it so long as it is in the same method.
CalculateMovement();
if (Input.GetKeyDown(KeyCode.Space) && Time.time > _canFire)
{
if ( _ammoCount == 0 )
{
AudioSource.PlayClipAtPoint(_noAmmoSound, transform.position);
return;
}
FireLaser();
}
In this case, when the ammo count reaches 0, FireLaser() won’t be called thanks to the return keyword. This is also a good time to play our sound clip when the player has no ammo left. AudioSource.PlayClipAtPoint will automatically dispose of the sound clip after it has played.
Technically, our work is done, but adding some UI elements for the ammo count is helpful to the player, especially for players who may have hearing difficulties.
- Create some text to display the ammo, size it, and anchor it to your desired location. I placed mine in the upper left portion of the screen underneath the player lives.
2. Create a handle for the ammo text in your script that handles your UI elements. Attach this to the Canvas in the Inspector View.
[SerializeField]
private TMP_Text _ammoText;
3. Similar to how the score was set up in the Start method, do the same for the ammo count.
void Start()
{
...
_scoreText.text = "Score: " + 0;
_ammoText.text = "Ammo: " + 15;
...
}
4. Create a method for updating the ammo count. This will look similar to my method for updating the player’s score.
public void UpdateScore(int playerScore)
{
_scoreText.text = "Score: " + playerScore.ToString();
}
public void UpdateAmmo(int playerAmmo)
{
_ammoText.text = "Ammo: " + playerAmmo.ToString();
}
5. Call the UpdateAmmo method from the AmmoCount method created earlier in the Player script. Script communication is needed for this to work because the text is in a separate script.
public void AmmoCount(int shots)
{
_ammoCount += shots;
_uiManager.UpdateAmmo(_ammoCount);
}