Odd gun behavior

I’m trying to animate a gun reloading and firing and so far everythings been working rather well, however when I tried to set up the script to only allow the gun to fire when loaded (or when wound in the case of this gun) and the allow the gun to be loaded a set amount of times. When I run the game the script returns no errors but I can only reload the gun once or (sometimes) twice rather than three times (what the AmmoCapacity variable is set to) and sometimes I get two shots out of one reload.

Thanks in advance - heres the full code

var AmmoCapacity : int;
//var DefaultBullet : GameObject;
static var currentAmmo = 0;


function Update () {
if (Input.GetAxis ("Reload") )
{
reloader();
}

if ( Input.GetAxis ("Fire1") )
{
firing();

}

}

function reloader ()
{
if (currentAmmo < AmmoCapacity)
{
animation.Play("reload",PlayMode.StopAll);
yield WaitForSeconds (animation.clip.length);
currentAmmo ++;
}
}

function firing ()

{
if(currentAmmo > 0)
{
animation.Play("fire",PlayMode.StopAll);
yield WaitForSeconds (animation.clip.length);
currentAmmo --;
}
}

maybe check out my gun script in the showcase forums? it has animations

Well I’d prefer to know whats wrong with this script so I can avoid the same mistake in the future

EDIT - fixed the problem - I wasn’t checking to see if an animation was already in progress

final code

var AmmoCapacity : int;
//var DefaultBullet : GameObject;
static var currentAmmo = 0;



function Update () {
if (Input.GetAxis ("Reload")  !animation.IsPlaying("fire")  !animation.IsPlaying("reload"))
{
reloader();
}

if ( Input.GetAxis ("Fire1")  !animation.IsPlaying("reload")  !animation.IsPlaying("fire"))
{
firing();

}

}

function reloader ()
{
if (currentAmmo < AmmoCapacity)
{

currentAmmo ++;
animation.Play("reload",PlayMode.StopAll);
yield WaitForSeconds (animation.clip.length);

}
}

function firing ()

{
if(currentAmmo > 0)
{

currentAmmo --;
animation.Play("fire",PlayMode.StopAll);
yield WaitForSeconds (animation.clip.length);

}
}

good for you!! solving your own problems :slight_smile: