Search Unity

Reversing a 2D animation at runtime.

Discussion in '2D' started by underoathgames, Jul 15, 2014.

  1. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    Hi, I'm looking for some help regarding playing an animation in reverse after a button click. Here's the skinny: I have a button sprite that I animate to fly up onto the screen on play. After I click the button I want it to fly back down, hence the animation in reverse. Since the 2D animation is being controlled by an Animator class I'm not sure how to access the particular animation I'm interested in and set the speed to -1 so it'll play in reverse. I know I can just create another animation that is the reverse and transition to that but that is a waste as long as I'm able to play the original animation in reverse. Any help is much appreciated.
     
  2. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    Since no one seems to have an answer to my previous question i'll ask a slightly different one. Is there a way to play a 2D animation from code without using the Animator component?
     
  3. valhalla_cats

    valhalla_cats

    Joined:
    Jan 27, 2013
    Posts:
    16
    Hi underoathgames, you can check the animator state adding a code like this:
    First declare a couple of variables:
    var currentBaseState : AnimatorStateInfo;
    var anim : Animator;

    anim = GetComponent(Animator);
    currentBaseState = anim.GetCurrentAnimatorStateInfo(0); /

    Then you can check the state and play the inverse animatimation:

    if (currentBaseState.nameHash == stateName) {
    anim.speed = -1;
    }

    Hope it helps
     
  4. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    Thanks Zaqnafein,
    I wasn't able to use the functions exactly as described

    But it did lead me to find the right solution, so thanks for pointing me in the right direction!
     
  5. valhalla_cats

    valhalla_cats

    Joined:
    Jan 27, 2013
    Posts:
    16
    I am happy to have pointing in the right direction :).