Search Unity

How to play an animation only once using Mecanim

Discussion in 'Animation' started by Kaze_Senshi, Jan 6, 2013.

  1. Kaze_Senshi

    Kaze_Senshi

    Joined:
    Feb 19, 2012
    Posts:
    243
    Hey guys, I am with a little problem, I want to play the death animation from my boss object, so I just do this in my code:

    Code (csharp):
    1.         anim.SetBool( "isDead", true );
    In the animator state machine I have a transition from "Any State" to "dead", which the condition is "isDead" == true. This transition works in the game but the animation is played in loop, the first time it plays like 80% of the animation and after it keeps playing until 60-70% of the animation and loop the animation. I want just to play the animation only one time and let the boss lied at the end. How can I do this? thanks
     
  2. Alf203

    Alf203

    Joined:
    Dec 7, 2012
    Posts:
    461
    You should look at the tutorial by Will Goldstone, its all explained there. http://www.youtube.com/watch?v=Xx21y9eJq1U


    You need to set it to false when you're transiting from a state to the dead state otherwise it will keep repeating.

    Here's a short example:

    Code (csharp):
    1.  
    2.  
    3. public Animator anim;
    4. static int m_deathStateId;
    5.  
    6. public void Awake()
    7. {
    8. // I assume your state is called Death and is in the base layer
    9. m_deathStateId = Animator.StringToHash("Base Layer.Death");
    10. }
    11.  
    12. public void Update()
    13. {
    14.  
    15. // Check if your layer is transiting to Death
    16. // I assume your transition is in the base layer
    17. if(anim.IsInTransition(0)  m_anim.GetNextAnimatorStateInfo(0).nameHash == m_deathStateId)
    18. anim.SetBool("isDead", false);
    19. }
    20.  
     
    Last edited: Jan 6, 2013
    theANMATOR2b likes this.
  3. Kaze_Senshi

    Kaze_Senshi

    Joined:
    Feb 19, 2012
    Posts:
    243
    Hmmm that is true, if it comes from "any state", it can come from the dead state itself, I am so dumb :p

    Thank you Alf, now it's working nice.
     
  4. kinetifex

    kinetifex

    Joined:
    Nov 4, 2008
    Posts:
    8
    I've found using a simple Coroutine to work pretty well so far.

    Define a method like this:
    Code (csharp):
    1.  
    2.     public IEnumerator PlayOneShot ( string paramName )
    3.     {
    4.         _animator.SetBool( paramName, true );
    5.         yield return null;
    6.         _animator.SetBool( paramName, false );
    7.     }
    8.  
    Then simply call it passing your Parameter name:
    Code (csharp):
    1.  
    2.     StartCoroutine( PlayOneShot("Fire") );
    3.  
    Anyone see any short-comings with this approach?
     
    ZenMicro and theANMATOR2b like this.
  5. jcolemorr11

    jcolemorr11

    Joined:
    Dec 13, 2012
    Posts:
    5
    Holy S*** Kinetifex! That actually works GREAT. Sooooo simple. And honestly, simplicity can be a far greater friend when dealing with the newness of mecanim considering it can get so damn garbled. Thanks Man!
     
  6. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    491
    Oh, that looks a nice trick. This has been rather annoying me, finding an appropriate time to clear these bools cleanly...

    Really, I think there should be an option in the Mecanim state editor to auto-clear a bool when the state is entered. Or simply have a 'RequestState()' function, to enter a state if there's an appropriate transition available.
     
    theANMATOR2b likes this.
  7. TheMasquerader

    TheMasquerader

    Joined:
    Sep 28, 2013
    Posts:
    3
    Thanks a lot for your response, but what happens when you have an actual game an at least 20 custom animations? You need to perform that check for each of them at every update. Doesn't feel very efficient :(

     
  8. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    Hi,

    You can use triggers instead of boolean values. Triggers reset themselves automatically. You don't need to reset them by code.
     
    GeekyTime likes this.
  9. Alf203

    Alf203

    Joined:
    Dec 7, 2012
    Posts:
    461
    Sorry for the late reply. Raziel is right. Note that "triggers" for Mecanim have been available only since Unity 4.3 and at the time I answered that question, they were not available.
     
    Westland likes this.