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

Reverse Animator Animation [SOLVED]

Discussion in 'Animation' started by SkillBased, Jun 26, 2015.

  1. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    I have a 2D animation handled by the Animator (walking forward) that I want to play in reverse when walking backward.

    This should be simple to do but is so far eluding me. I figured out I can flip the Animator.speed value to -1 to play it in reverse but this causes a glitch where the animation plays in reverse for a while but then gets stuck on the first frame and there is no clear reason why this is happening. I checked my code and it has nothing to do with an error in the code itself.

    Searching around I found a few 'solutions' that involve duplicating the ordinary forward STATE and setting its Speed to -1 but the solution is very inelegant for a bunch of reasons. Also, being able to simply reverse the forward animation allows me to start going backwards from whatever frame is currently active, yielding a much nicer looking result. Any help is appreciated!
     
  2. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
  3. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    Perfect timing for this update. Worked a charm!

    Here's how I made it work:

    Code (CSharp):
    1. void Update()
    2. {
    3.   float strafeDirection = 1;
    4.  
    5.   if (Strafing)
    6.   {
    7.        if ((moveX < 0 && FacingRight) || (moveX > 0 && !FacingRight))
    8.        {
    9.             strafeDirection = -1;
    10.        }
    11.   }
    12.   anim.SetFloat("StrafeDirection", strafeDirection);
    13. }
    And in the animator I just added StrafeDirection as a parameter, and in the particular state that handles the animation I set the Speed multiplier to the StrafeDirection parameter. Easy! Wonder why it took so long to add this...
     
    Last edited: Jun 26, 2015
  4. zero_null

    zero_null

    Joined:
    Mar 11, 2014
    Posts:
    159
  5. ratmat2002

    ratmat2002

    Joined:
    Jun 13, 2014
    Posts:
    35
    In Unity 5.3.1p1, if I set the speed of an AnimationState in the MecAnim Inspector to -1, my animation isn't starting at the very end of the animation (it's 1 second, 20 frames long). I added AnimationEvents and I only see the ones fired at 1 second (1:00) and then backwards to the start of the animation (0:00). The animation frames past 1:00 never play and any corresponding AnimationEvents set past 1:00 never fire.
     
    Last edited: Jan 4, 2016
  6. Dreambox

    Dreambox

    Joined:
    Mar 21, 2014
    Posts:
    1
    Does not work in 5.3.2f1 Plays until frame 0 then gets stuck
     
    Unlink likes this.
  7. ratmat2002

    ratmat2002

    Joined:
    Jun 13, 2014
    Posts:
    35
    I was able to get it to work by making sure the Apply Root Motion checkbox was off. This is a change from Unity 4!