- Home /
Play two Animations
I have the following code setup so that when I click on my door it plays the animation that is on it the problem lies in the fact that I would like two animations. One for opening the door and one for closing the door. But if the door is already open I don't want it to play the open animation again, I want it to play the close animation.
Here is my code:
void OnMouseDown()
{
animation.Play();
}
You could use a coroutine.
float waitTime = 3.0f;
void On$$anonymous$$ouseDown()
{
animation.Play("Open");
StartCoroutine("CloseDoor");
}
IEnumerator CloseDoor()
{
while(true)
{
yield return new WaitForSeconds(waitTime);
if(!animation.isPlaying)
{
animation.Play("Close");
StopAllCoroutines();
}
yield return new WaitForEndOfFrame();
}
}
I am kinda wanting to leave the door open until the player decides to close or never close if player don't close it. But I do thank you for your suggestion.
Answer by Piflik · Jan 01, 2013 at 08:02 PM
Something like this should work.
private bool isOpen = false;
void OnMouseDown() {
if(!animation.IsPlaying("open") && !animation.IsPlaying("close")) {
if(isOpen)
animation.Play("close");
else
animation.Play("open");
isOpen = !isOpen;
}
}
Perfect thanks you have one to many closing perethases on your if statement at the end.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Toggle on click. 2 Answers
Problems with enums and switches (C#) 2 Answers