I’m making an FPS game but I want there to be limited ammo (probably only 2 reloads). My problem now is that Unity subtracts ammunition per frame everytime I do inventoryAmmo -= currentAmmo or inventoryAmmo = inventoryAmmo - currentAmmo so I get negative values.
Here is some code for reference:
if (Input.GetKey(KeyCode.R) && currentAmmo < maxAmmo && inventoryAmmo > 0)
{
StartCoroutine(Reload());
reloading = reloadTime;
}
if(Input.GetMouseButton(0) && currentAmmo <= 0 && inventoryAmmo > 0)
{
StartCoroutine(Reload());
reloading = reloadTime;
}
}
IEnumerator Reload()
{
isReloading = true;
reloadAnim.SetBool("isReloading", true);
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isReloading = false;
inventoryAmmo = inventoryAmmo - currentAmmo;
reloadAnim.SetBool("isReloading", false);
}