How to wait for animation completion (C#)?

Hello UA,
I am making a very simple script to animate a door opening and closing on proximity by pressing e. What happens though, is I can spam e and the animations don’t finish. How can I make the script wait before you can press e to start the animation until the other animation finishes? I am still new to Unity, so try to explain why you do certain things vs others. FYI, the door is on the back of a ship and flips down. Finally, ask for clarification if needed.
CODE:

using UnityEngine;
using System.Collections;

public class OpenShipDoor : MonoBehaviour {
		
	bool doorup = false;

	void OnTriggerStay(Collider other){

		if (Input.GetKeyDown ("e"))
		{
			if (doorup == false)
			{
				doorup = true;
				animation.Play("doorup1");

			}
			else
			{
				doorup = false;
				animation.Play("doordown1");
			

			}
		}
	}
}

2 Answers

2

The Animation.IsPlaying field sounds like just what you need!

The Unity method reference is your friend… You only want to play an Open/Close animation if there isn’t already an animation playing, right? And if you browse the reference for the Animation class, you’ll find a property called “isPlaying”: Unity - Scripting API: Animation.isPlaying

The example on that page shows you exactly how it’s used to only play an animation I’d there isn’t already one playing, using the ! operator.