I have a animated rifle that I am trying to script into playing correctly. The script I have wrote tells the animation to play if the LMB is clicked, this works but I can rapidly click the button without waiting for the animation to finish. How could I make my gun only play the animation when it is not being played to start with?
Here’s another way to do this. This is the one I use for my game:
GameObject objectToAnimate;
void Start()
{
objectToAnimate = GameObject.Find("objectName"); // Just in case is an external object
}
void StartAndWaitAnimation(string animationName)
{
StartCoroutine(PlayAnimationAsync(animationName));
}
IEnumerator PlayAnimationAsync(string animationName)
{
objectToAnimate.animation[animationName].wrapMode = WrapMode.Once; // choose your wrapping mode
objectToAnimate.animation.Play();
yield return new WaitForSeconds(objetToAnimate.animation[animationName].lenght); //This does the magic
//Do the stuff after the animation ends
DoOtherStuff();
}
This way you separate your code from your animation design, so the code takes care of the order and execution, instead having lots of triggers on your animations. Just a point of view.