Unity 5.5.0f3 play animation backwards at runtime

3.6k Views Asked by At

Original question on the Unityforums here

I've been trying to get an animation to not only slow down and speed up, but also play backwards depending on user input for my Hololens-application. I am using the Mecanim system, not legacy animations.

The whole thing is supposed to happen at runtime, through dynamic user input.

I know that it's possible through scripting, as I had it working before I lost local progress and some files during some Unity-Collaborate issues. As stupid as it sounds, since then I have not been able to remember what I did different from my current approach.

Right now I'm manipulating the value Animator.speed, but that only works for values >= 0.

Any help would be greatly appreciated!

Edit: In case the link is not working or visible for anybody, here is my code:

private Animator anim;
 //...
 anim = gameObject.GetComponent<Animator>();
 //...
 public void OnManipulationUpdated(ManipulationEventData eventData)
     {
         if (anim.isActiveAndEnabled)
         {

             anim.speed = eventData.CumulativeDelta.x;
             anim.Play("KA_Cover_Anim");
             return;
         }
         //...
     }

Edit2: Incorrectly marked as dupicate! The linked question does not regard a similar problem and required a different solution

Edit3: For clarification, the linked "duplicate" uses the legacy animation system which is irrelevant for my question. In Mecanim, the new animation system in Unity 5.xx, you can not access the Animations directly as shown in the selected answer. Neither is it possible to alter the animation speed as shown in in the second answer.

1

There are 1 best solutions below

2
On

I'm not exactly sure what you're end goal is, but you can play animations backwards and at different speeds by using a parameter.

enter image description here

On the animation state, you can make it watch a parameter and multiply it against the default speed of the animation. All you need to do in code is something like

animator.setFloat("Speed",-1.0f);

Hope that helps.