Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

animation won't finish when using coroutine

Discussion in 'Animation' started by SFDCIVimyProject, Nov 24, 2015.

  1. SFDCIVimyProject

    SFDCIVimyProject

    Joined:
    Oct 21, 2015
    Posts:
    9
    my animation will not play all the way through to simulate running when the right/left arrow
    key is pressed so i tried to use a coroutine to fix the problem by making the animation wait 1 second and to try to get it to play all the way through but it did not work.
    I checked debugging to the console but it said it was waiting the 1 second but the animation did not show it when i went into play mode

    I have a bool (isRunning) set up to only allow the animation to be run but it also does not seem to be helping.

    Here is the code that I have:

    // This is inside the Update() function

    float leftrightButtonPush = Input.GetAxis ("Horizontal");
    if (Mathf.Abs (leftrightButtonPush) > 0.2f && !isRunning) {
    StartCoroutine (Run (leftrightButtonPush));
    } else if ( leftrightButtonPush < 0.2f) {
    //anim.SetFloat("speed", Mathf.Abs (leftrightButtonPush));
    }



    }


    IEnumerator Run(float val ){
    Debug.Log ("hi");
    isRunning = true;
    //yieldreturnnewWaitForSeconds (0.5f);
    anim.SetFloat("speed", Mathf.Abs (val));
    yieldreturnnewWaitForSeconds (1f);
    isRunning = false;
    Debug.Log ("bye");
    }
     
    Last edited: Nov 25, 2015
  2. masood-t

    masood-t

    Joined:
    Nov 25, 2012
    Posts:
    13
    if you put above code in update , so you are calling IEnumeratorRun in each frame that player use left or right.

    call IEnumeratorRun just once . may Input.getkeyDown can help.
     
  3. SFDCIVimyProject

    SFDCIVimyProject

    Joined:
    Oct 21, 2015
    Posts:
    9
    Based on the isRunning (bool) variable, IENumerator should only be called once.
    Yet, it's clearly being run every time the Update() function gets executed.

    Any suggestions?