Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animator.Play vs using the state machine

Discussion in 'Animation' started by Alakanu, Jun 11, 2018.

  1. Alakanu

    Alakanu

    Joined:
    Oct 17, 2015
    Posts:
    5
    Hi, I've always wondered: is there a reason why Animator.Play and Animator.CrossFade exist? Are they slower/faster than setting a parameters and triggering transitions? Are those just to be considered a plan b when you have no other choice or are they there for people that can't stand state machines? I don't know you but those function feel like anti-pattern to me.
     
    virgobr likes this.
  2. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Hey, thanks, I think you just answered a question I was having! :)

    When I say jump I don't want to wait for the run animation to first play the remaining 50 frames and THEN jump at the end when it eventually checks the transition states... so I simply don't use the whole transition due to a transition condition being met (most of the time) thing. I call Play instead.

    I use transitioning to return to the idle state after doing a once off animation but I trigger my animations using Play (thus skipping the wait for the current animation to finish before it goes to the next one). I forgot there is a CrossFade option. Nice. Now I can trigger my animation faster but still benefit from some transition rather than just cutting from one animation to the other immediately. Cool!

    Thanks for reminding me of that
     
    eytienne likes this.
  3. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    @Alakanu
    It depends on each project, some users like to control everything from script, there is also users that don't know how to write code so for them the statemachine transition allow them to define their animation blend more easily.

    there is also some situation sometime that you can't simply define with a statemachine transition because your statemachine is complex, by example a fighting game where one character start an attack but the opponent interrupt the attack and you need to trigger a knockdown animation. So exception in your normal statemachine flows can be enforced with Play/CrossFade rather than trying to cover all the possible case and create a transitions spaghetti :D

    they are there to give flexibility to the system.

    Play and CrossFade are like dynamic transition which can be created at runtime and that live only for this frame.

    Performance wise they should behave exactly like normal transitions, so you shouldn't see an impact on the performance when you use them.
     
    WayneJP, KateSu, virgobr and 3 others like this.
  4. Alakanu

    Alakanu

    Joined:
    Oct 17, 2015
    Posts:
    5
    Thanks for the explanation, so it's just different flavours of the same thing.

    You might wanna check out the ExitTime parameter on your transitions.