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

best way to handle animations that you want triggered once

Discussion in 'Animation' started by rapidrunner, Dec 4, 2015.

  1. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    I am looking at some sample code, and found that a jump animation is implemented as following.

    In the editor, the mecanim entry for the animation is triggered when a vlue is true. The animation stop once done

    In code; when the jump button is pressed, the animator call to change the boolean to true is done.

    Code (CSharp):
    1.         if (Input.GetButtonDown("jump"))
    2.         {
    3.             myAnimator.SetBool("jumping", true);
    4.             Invoke("StopAnimation", 0.1f);
    5.         }
    Then there is a call to a function that is called via invoke, with delay. This allow to have the animation to start; then the invoke wait and trigger the function, which set the jump value to false.

    Code (CSharp):
    1.         StopAnimation()
    2.         {
    3.             myAnimator.SetBool("jumping", false);
    4.         }
    It works fine, but there are some problems:

    1) you need a function called via invoke, for any animation that you perform. Quite inefficient.
    2) If you want to have a single function to stop the animation, passing the animation name as parameter, it won't work, because invoke won't accept functions with parameters.

    Pretty sure that there is a better way to perform such actions, like jump or any other action that require the animation to be performed once and be done with it. What are you actually using? All tutorials on Mecanim are mostly showing a similar setup
     
  2. Ysgramor

    Ysgramor

    Joined:
    Jan 23, 2014
    Posts:
    69
    How about just ticking has exit time in animator controller?
    and set the animation to not looping
     
  3. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    Tried that; the jump is triggered from any state, if the jump boolean is true. If you do not change it to false, even if teh "exit time" checkbox is marked; it will contiue to loop through the jump animation.

    I did check how to trigger the jump animation from the "any state" state, but I can't seem to find how to do that in code, beside set a variable and create a transition to the jump
     
  4. Ysgramor

    Ysgramor

    Joined:
    Jan 23, 2014
    Posts:
    69
    I think just make out transition without conditions, so when animation end, they'll move back to base anim.
    thats my solution i used , since im also struggling for jumping anim before :D
     
  5. MightySheep

    MightySheep

    Joined:
    Sep 23, 2015
    Posts:
    21
    And use trigger instead of a bool. Triggers send one impulse and they're back at false automaticaly.
     
    rapidrunner and Ysgramor like this.
  6. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    Ok; changing the code a bit.

    Trigger is an excellent solution; it play the clip once and disable itself so it won't get stuck like with a bool. Thanks.
     
    MightySheep likes this.
  7. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944

    That indeed works; although if you don't use triggers, the animation get stuck in a loop :)
     
  8. Ysgramor

    Ysgramor

    Joined:
    Jan 23, 2014
    Posts:
    69
    Im using bool, and work well. yea but i must change back to false with some conditions.
    maybe trigger much easier to implement