Adding Ammo Count
In today’s blog post, we’re going to add the ammo count feature so that the player will only have a limited amount of ammo. This will make our game more challenging!
First, let’s create the AmmoCountText object by duplicating the ScoreText and changing the text to “Ammo: 15”. Also, let’s position it at the top right corner below the Score text.
Next, in the Player script, let’s create two variables: _ammoCount and _isOutOfAmmo. By default, we’ll give the player 15 lasers when they start the game and set the boolean variable to false. We’ll switch it to true when the player runs out of ammo.
[SerializeField] private int _ammoCount = 15;
[SerializeField] private bool _isOutOfAmmo = false;
We’ll need an “out-of-ammo” sound for when the player hits the space key without any ammo. Let’s create a variable for that and assign it in the inspector:
[SerializeField] private AudioClip _outOfAmmoSound;
Next, let’s modify the Fire function to accommodate the ammo logic:
public void Fire()
{
if (_ammoCount > 0)
{
if (_isTripleShotActive == true)
{
Instantiate(_tripleShotPrefab, new Vector3(transform.position.x, transform.position.y + 1f, 0), Quaternion.identity);
}
else
{
Instantiate(_laserPrefab, new Vector3(transform.position.x, transform.position.y + 1f, 0), Quaternion.identity);
}
_ammoCount--;
}
else
{
_isOutOfAmmo = true;
}
}
The logic checks if the player is out of ammo before instantiating regular and triple shots, and reduces the ammo count for each shot fired. It also switches the _isOutOfAmmo boolean to true when there is no ammo.
Next, let’s create a method to update the ammo count text in the UIManager script:
[SerializeField] private TMP_Text _ammoCountText;
public void UpdateAmmo(int playerAmmo)
{
_ammoCountText.text = "Ammo: " + playerAmmo;
}
Let’s call the UpdateAmmo method in the Fire function as well:
if (_ammoCount > 0)
{
if (_isTripleShotActive == true)
{
Instantiate(_tripleShotPrefab, new Vector3(transform.position.x, transform.position.y + 1f, 0), Quaternion.identity);
}
else
{
Instantiate(_laserPrefab, new Vector3(transform.position.x, transform.position.y + 1f, 0), Quaternion.identity);
}
_ammoCount--;
_uiManager.UpdateAmmo(_ammoCount);
}
else
{
_isOutOfAmmo = true;
}
We also need logic to detect when the player is out of ammo and play the fire audio accordingly:
if (_isOutOfAmmo == false)
{
_audioSource.PlayOneShot(_laserSound);
}
else
{
_audioSource.PlayOneShot(_outOfAmmoSound);
}
Here’s the full code:
public void Fire()
{
if (_ammoCount > 0)
{
if (_isTripleShotActive == true)
{
Instantiate(_tripleShotPrefab, new Vector3(transform.position.x, transform.position.y + 1f, 0), Quaternion.identity);
}
else
{
Instantiate(_laserPrefab, new Vector3(transform.position.x, transform.position.y + 1f, 0), Quaternion.identity);
}
_ammoCount--;
_uiManager.UpdateAmmo(_ammoCount);
}
else
{
_isOutOfAmmo = true;
}
if (_isOutOfAmmo == false)
{
_audioSource.PlayOneShot(_laserSound);
}
else
{
_audioSource.PlayOneShot(_outOfAmmoSound);
}
}
And our game is working as expected!
In the next article, we’ll add a power-up that refills the ammo count allowing the player to fire again.