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 End

Discussion in 'Animation' started by Nihil688, Sep 28, 2015.

  1. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    Is there any way to find when an animation ends? I mean a proper event, not like:

    Code (CSharp):
    1. AnimatorStateInfo currentState = p_Animator.GetCurrentAnimatorStateInfo( 0 );
    2. float playbackTime = currentState.normalizedTime % 1;
    or the StateMachineBehaviour which needs to be added in each individual animation state, it's useless in any proper game with advanced animations which usually have loads of states...
     
  2. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
  3. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    either you create an animation event on each animation clip at the end of each clip or

    you can use StateMachineBehaviour and override OnStateExit but rather than setting the SMB on every state you can set it on the parent statemachine, every state contained in the state machine will inherit this SMB too.
    To select the top most statemachine in your controller simply select the layer and it should select the SM for you, then in the inspector click on Add Behaviour.

    Also since this behaviour is stateless, no variable won't change since it will probably only forward the call to something else, I would suggest you to use the attribute SharedBetweenAnimators
    http://docs.unity3d.com/ScriptReference/SharedBetweenAnimatorsAttribute.html
    This will reduce the memory footprint by a large amount, otherwise every state in your statemachine will instantiate their own instance of the statemachinebehaviour.

    So with one instance of a SMB you will get a proper event when all your animation ends.
     
  4. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    That sounds like a great plan, so what I did was, created a substate machine, copy pasted everything into that ssm, added the SMB on it and gave it a go...

    Assume that the code looks like your example with debugs in all states: here: http://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html

    The debugs appear only once for the first time passed by that state machine. Happens both with SharedBetweenAnimators and without. Any thoughts? Is it a 5.2.0f3 bug, should I wait for 5.2.1p2 fixes?
     
  5. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    I've just did a quick test in 5.2.1p1 and it does work
    Import this package into unity, open test scene and hit play.

    everytime that a state end it should call OnAnimationEnd.OnStateExit and print the name hash of the state.
     

    Attached Files:

  6. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    Oh you attached it on the layer! You didn't say that =D
    Lovely I didn't know you could do that, this will fix so many of our bugs!
    Thanks!
     
  7. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Hello @Mecanim.Dev , I'm looking into using StateMachineBehaviour objects to handle some logic in my characters abilities and one thing I'm wondering is whether it is possible to add a StateMachineBehaviour at runtime. To my knowledge that is not possible right now but maybe I'm mistaken. If not, is it a feature that will make it eventually in Unity?
     
  8. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    yes I did but it maybe it was not clear enough.

    Right now there is no plan to add SMB at runtime. What is you use case exactly? I would like to know the detail to see what we could do to help you
     
  9. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Actually I was gonna attach distinct SMBs to specific states and each SMB was gonna handle a single character ability. If each character in my game has 4 given abilities, adding these SMBs at runtime would allow me to have a generic controller with 4 states (ABILITY1, ABILITY2 and so on...) that I would override with character specific clips and then attach the SMB related to this specific ability on the relevant state.

    But I've thought about it and I could justhave a single SMB on my layer and re-route the SMBs callbacks to my Character component which could apply the correct logic depending on the current state of the state machine. I think it might be simpler than my first idea because the SMB would only forward data to the Character and all my abilities timing logic would be handled by my concrete Character.
     
    Last edited: Oct 5, 2015
  10. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    @Mecanim.Dev you did indeed :)

    Since we are saying our wishes what I would ask of Mecanim is, get names of states in code as I don't like using hardcoded hash numbers and some sort of idea what's more optimized, to override a SM, to have loads of controllers with one state for simple animations etc.