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

Mehod Calls after an Animation is Done

Discussion in 'Animation' started by Gardosen, Jul 25, 2015.

  1. Gardosen

    Gardosen

    Joined:
    Oct 25, 2014
    Posts:
    39
    Hello everyone,

    i try to find a proper solution for this since months, i was working with a "Workaround" now for this time but i want to get rid of this Workaround and do it correctly.

    I am coding a small game, where one after the other, cards are getting fliped.
    I wrote a Manager for this which contains a Queue where all the FlipActions are stored in.

    as soon as an Animation is done, i want to call a method which sets the state of the Action to "done", which should happen directly after the animation.

    currently i am doing this with the Animation and a dirty trick where i added a useless Animation to the animator like this

    CardFlipAnim --> AnimationDoneMethodCallAnim --> Exit

    the AnimationDoneMethodCallAnim just contains a Eventcall to the method.

    my first idea was to use a listener which starts to run when the animations starts, and checks periodicly if the animation is done, and when its done he triggers the method.
    but i dont find anything for this.... i am really happy about so much things which are so handy on unity, but this animation system, is really breaking my brain :(

    I hope someone can help me with this.

    Kind regards
    Gardosen

    PS.: I am using Unity 5.0.2f1
     
  2. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,511
    I implemented a really unsatisfactory solution for what you want. lol. I found that monitoring the animation states is inefficient and error prone, and my system was not full-featured at all. I ended up ripping it out and using AnimationEvents instead, since that is the supported way to do these sorts of things in Unity. I wrote a blog post about it here: http://undertheweathersoftware.com/...ender-pose-markers-to-unity-animation-events/

    Of course, if you are not interested in going that route (especially since it's quite involved, I admit), I can share some snippets of my earlier solution that can give you some hints as to how to capture animation state start/end "events".
     
  3. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Hi gardosen,

    You can either use animation events or a state machine behaviour.
    If you want to use animation events simply add one at the end of your clip that call a method that change your action state.
    If you want to use state machine behaviour simply override the method OnStateExit and change your action state there.
     
  4. Demarest

    Demarest

    Joined:
    Aug 11, 2013
    Posts:
    3
    I'm having problems with those two solutions.

    I can't find a way to make animation events 100% reliable at the last frames of the animation. The change of frame rate or simply changing the number of animation samples can result in that event skipping and breaking the game logic.

    Machine behaviour works but I need to access scene objects to change the game logic and I'm not sure I should be searching for objects there.

    I feel that I'm missing something important. What is be the most correct way to do it? Thanks.
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,534
    That's what I do in OnStateEnter:
    Code (csharp):
    1. var myScript = animator.GetComponent<MyScript>();
    2. myScript.UpdateSomeLogic();
    3.  
    4. anotherObject = GameObject.Find("foo");
    5. // etc. you know the drill
    It's not like it's running every frame, so I don't see any problem with it. Alright, so I don't do GameObject.Find(). Usually "MyScript" has a reference to whatever other objects I might need to access.