- Home /
Animation won't complete unless button is held
Hey there, I'm getting really irritated by this problem :s. I have an animation that I need to play to switch weapons, but the animation won't play unless I am holding down the button? Even when using GetButtonDown it only plays it for half a second then stops because the button has already been pressed.
The following code is what I have so far,
void FixedUpdate()
{
if (Input.GetButtonDown("Swap"))
{
WeaponSwitch();
}
}
protected virtual void WeaponSwitch()
{
if (weaponSwitching)
return;
weaponSwitching = true;
weaponSwitchTime = weaponAnimationHolder.animation["SniperToSMG"].length;
weaponAnimationHolder.animation["SniperToSMG"].speed = weaponSwitchTime;
weaponAnimationHolder.animation.Play("SniperToSMG", PlayMode.StopAll);
StartCoroutine(waitFor(weaponSwitchTime));
weaponSwitching = false;
}
IEnumerator waitFor(float waitTime)
{
yield return new WaitForSeconds(waitTime);
}
Could somebody please tell me how to fix this? All I want is my animation "SniperToSMG" to play once without the need to hold down a key.
Answer by Josh707 · Nov 17, 2013 at 03:10 PM
It's because it's checking for input inside of the fixed update method - It will constantly miss your button presses as it is not called per-frame. Moving it into the Update method will fix that. Most of the time fixed update should just be used for physics manipulation or operations that need to be performed in the same sense of time, regardless of your frame rate