- Home /
Limited Ammo? Help
Hello, I asked more than once this and I did not get any response, How would you do in this script c # so that when TotalAmmo is 0 you can not shoot ?, but if it is GREATER than 0 reload and keep firing. Please. Thank you
Answer by triis4924 · Dec 31, 2018 at 12:14 AM
maybe this helps you:
public int ammo;
public int reloadtime;
private int currenttime;
public int fullammo;
void Start ()
{
currenttime = reloadtime;
}
void FixedUpdate ()
{
if(ammo == 0)
{
currenttime = -1;
}
if (currenttime == 0)
{
ammo = fullammo;
currenttime = reloadtime;
}
}
}
your fullammo is your ammo you want to have when you reloaded. Ammo is your current ammo thats left. Just decrease ammo by 1 every time you shoot
Hello, thank you very much for your help, only that the part that is subtracted the ammunition 1 in 1 when shooting is already in the script, I was looking for TotalAmmo (Total ammunition that the player has), When reaching 0, CurrentAmmo is 0 also, This was the only thing that worked for me! towards this but it gave error: If (TotalAmmo <= 0) { CurrentAmmo == 0; }
But I was wrong, you know how to do that? Thank you
every time you reload do : `
if (currentammo == 0 && TotalAmmo > 0)
{
if (TotalAmmo > currentammo)
{
TotalAmmo -= currentammo;
currentammo = $$anonymous$$axammo;
}
else
{
$$anonymous$$axammo = TotalAmmo;
currentammo = $$anonymous$$axammo;
}
Does this work for you?