- Home /
Javascript; Wait for animation
I know this question is asked frequently, but none of the answers helps.
I want to wait until an animation is finish.
My code (not in an Update function):
Animation = this.gameObject.GetComponent(Animator);
function manipulate(){
if (Input.GetKeyDown(KeyCode.E)){
if (Open == false){
Animation.Play("Tür|Door_70");
}
Open = true;
}
}
Now I want the boolean 'Open' toggles only when the animation is complete.
I've tried:
yield WaitForSeconds (Animation.clip.length);
yield WaitForSeconds (Animation.clip("Tür|Door_70").length);
yield WaitForSeconds (Animation.state.length);
yield WaitForSeconds (Animation.state("Tür|Door_70").length);
yield WaitForSeconds (Animation.length);
while (Animation.isPlaying){ ... }
while (Animation.clip.isPlaying){ ... }
while (Animation.clip("Tür|Door_70").isPlaying){ ... }
if (Animation.isPlaying){ ... }
if (Animation.clip.isPlaying){ ... }
if (Animation.clip("Tür|Door_70").isPlaying){ ... }
... and so on. What's the right command I need to do??
Answer by LazyElephant · Feb 01, 2016 at 10:35 PM
You should be able to do this using an animation event. First, create a function in your script to set the value of Open.
function setOpen( isOpen:Boolean) {
Open = isOpen;
}
Next, open the animation window and select the Tür|Door_70
animation. Add an animation event to the final frame of the animation. Right click on the small rectangle that appears and choose Edit Animation Event. In the window that appears, select the setOpen function and pass in the value you want, which would be true in this case.
Everything worked but I can't choose a function in the window. There is just "Function: (no function selected)" I can choose...
Edit: Got it! Thanks.