- Home /
How to enable an animation then disable after it is done playing no matter what fps you are running?
Hey, I could not find any other things relating to this. For some reason, getcomponent().Play("Recoil"); will not work and i have to have it looping. I have it enable it once the gun fires then it plays the animation then disables the animation. I do not understand how to turn it off once it finishes the animation, because different fps will have different animation play times. the clip is .4 seconds long so when i invoke the turn it off aft .4 seconds it sometimes stops in the middle of the animation. I have no idea why it does not let me do the .play for the animator, but it doesn't. here is my script:
using UnityEngine;
using System.Collections;
public class GunRecoil : MonoBehaviour {
public GameObject Gun;
void AnimationTurnOff ()
{
Gun.GetComponent<Animator> ().enabled = false;
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
Gun.GetComponent<Animator>().enabled = true;
Invoke ("AnimationTurnOff", .4f);
}
}
}
Do you see anything wrong or know how to fix the problem? Any help on why .play() doesn't work???
Answer by felixpk · Jun 25, 2015 at 02:33 PM
If you really just want to Play one animation I recommend you to use the "old animation system".
Create your animation like you have before
remove the Animator
add an Animation component
click on your animation, turn on debug mode
mark animation as legacy
Now you can access your animation like this:
if(Input.GetMouseButtonDown(0)){
Gun.GetComponent<Animation>().Play("Recoil");
}
You also can stop the Animation like this:
if(Input.GetMouseButtonDown(0)){
Gun.GetComponent<Animation>().Stop("Recoil");
}
When you want it to loop constantly make sure looping in the inspector is enabled! If not you would choose "Clamp Forever", because somehow "once" does not work.
Thanks, this worked a lot better than what I had. marked as correct.
How would you do this without the legacy animation system? They will eventually make legacy systems obsolete and then you wont be able to upgrade your project without everything breaking.
Your answer
Follow this Question
Related Questions
Play Animation several times 0 Answers
Restore game objects after animation 1 Answer
Multiple Cars not working 1 Answer