Search code examples
animationunity-game-engineenumeratormulti-step

Simple 2-step animation using Unity's Animation Tool


I am new to Unity/coding and trying to create a simple 2-step animation in which I can adjust the delay times for each step. I have a lift in my game that uses two animations: "Up" and "Down".

I'm using an enumerator to play the animations, and this is what I have so far:

IEnumerator Go()
{
    while(true)
    {
        GetComponent<Animation>().Play ("Up");
        yield return new WaitForSeconds(delayTime);
        break;
        GetComponent<Animation>().Play ("Down");
        yield return new WaitForSeconds(delayTime);
        break;
    }
}

I understand I could just animate the whole thing as one motion, but I want to be able to adjust the delay times on the fly. My goal is to animate these two in succession. Up, then down. At the moment my lift goes up and stays there. What am I doing wrong?

Thanks for the help!


Solution

  • Remove the break-clauses:

    IEnumerator Go()
    {
        while(true)
        {
            GetComponent<Animation>().Play ("Up");
            yield return new WaitForSeconds(delayTime);
            GetComponent<Animation>().Play ("Down");
            yield return new WaitForSeconds(delayTime);
        }
    }
    

    Now it should loop the two animations. In the original code the break statements are causing the execution jump out of the loop and, therefore, the Play for the "Down" is never called and execution of the function is terminated.

    If you want the lift go up and down only once you need to remove the while loop.