Search Unity

Mecanim Use Case - play animation from any frame, forward or reverse ?

Discussion in 'Editor & General Support' started by EmeralLotus, Jan 27, 2013.

  1. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,462
    Mecanim is really amazing but since it's so new I'm not quite sure how to do the following.

    Ex: We have an animation that has 100 frames.

    Use Cases:

    1. Play animation from frame 1- 20. Stop at 20 until the user clicks a button to resume playing.
    2. Play animation from frame 1-20, then play 40-50, stop at 50 and then play from 50 to the end.
    3. Play animation from frame 20-1, stops, then play 40-50, stops, and then play from 100-50, then stop.

    Can the above use cases be done with Mecanim? or is there some work around?
     
  2. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    You'd split your animation into seperate clips in the importer's Animations tab, and transition (with zero time if you want no blending) between them. Triggering transitions is easy, strongly recommend watching the official tutorial video, which is quite comprehensive:

    http://www.youtube.com/watch?v=Xx21y9eJq1U

    But note I'm pretty sure you can't play animation backwards (i.e. 20-1).
     
    theANMATOR2b likes this.
  3. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,462
    Thanks Pix10 for your suggestion. We wanted to do most of this in code. Would this be possible?
     
  4. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    You can trigger transitions from code with variable states, and you can read the normalizedTime of an animation state to know how far along it is (and/or use Curves if you have Pro), but there's no direct API access to the animator's State Machine at the moment - we've been promised this is coming in an update however so it's worth keeping an eye on the official unity blogs.
     
  5. Maria Chernykh

    Maria Chernykh

    Joined:
    Oct 31, 2012
    Posts:
    7
    If this topic is still relevant, I can suggest the implementation of backwards animation play. You need to duplicate your animation clip and add it to your state mashine, then in inspector select "speed" and set it to -1. Then animator will go to this state it will play animation backwards. Hope it helps:)
     
  6. WorldWideGlide

    WorldWideGlide

    Joined:
    Nov 3, 2013
    Posts:
    26
    Haha it's so simple!
     
  7. fidelsoto

    fidelsoto

    Joined:
    Aug 7, 2012
    Posts:
    87
    Lord Jesus thank you so much for that. I tried everything to set an animation clip's wrap mode as ping pong in mechanim. I can do a simple workaround by using the speed -1 trick. THANKS!!!
     
  8. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    642
    Mecanim needs much more support for direct script control before it replaces the legacy system, doesn't it.

    There's all sorts of cases where you want to control the exact position in an animation from code, or sample an animation at a specific point.

    The state/transition system makes sense for certain cases, say NPC humanoid characters. But it's complete overkill for simple things (e.g. opening+closing a door, or moving a few UI elements around), and it's rather problematic when you want precise script control, and no one-frame-delay/glitch cases.