So, here’s my ammunition script:
var damage : float = 5;
var gunshot : AudioClip;
var muzzleflash : GameObject;
var aclip : int;
var ammo : int;
function Update ()
{
pistolflash = GameObject.FindWithTag("pistolflash");
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
var localOffset = transform.position;
if (aclip > 0)
{
if(Input.GetButtonDown("Fire1"))
{
if (Physics.Raycast (localOffset, direction, hit, 400))
{
Debug.DrawLine (localOffset, hit.point, Color.cyan);
print("we have fired!");
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
AudioSource.PlayClipAtPoint(gunshot,transform.position);
Instantiate (muzzleflash, pistolflash.transform.position, transform.rotation);
aclip --;
}
}
if (aclip == 0)
{
reload();
}
}
function reload()
{
if (ammo > 0)
{
yield WaitForSeconds (2);
aclip ++;
ammo --;
}
}
aclip is loaded ammunition, and ammo is your supply. aclip and ammo are both set to 1. When I fire, aclip goes to 0, then it waits a few seconds- like it should- and then ammo goes to about -30 and aclip goes up to about 24. I can’t figure out why it does this, though. Any help?