1

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.

TillEmpea
  • 11
  • 1
  • 4
  • 2
    You have to show what you have tried – Programmer Jan 19 '17 at 15:19
  • You have to read what I write: "Right now I'm manipulating the value Animator.speed, but that only works for values >= 0." Dont mean to sound like a douche but I clearly did clarify what I attmepted and also didn't ask anyone to write me code. – TillEmpea Jan 25 '17 at 15:05
  • "You have to read what I write:" Attitude like this wont get you anywhere. I was only trying to help. How can we know why it's not working without your code? Good luck with that! – Programmer Jan 25 '17 at 15:08
  • I may have misinterpreted but your first comment seemed quite similar in attitude, so I responded like that. Weird that you misuse your reputation to downvote the question after you felt offended by a comment. I genuinely tried to clarify that I'm not taking a jab at you, just that I very clearly stated my attempt _and_ that I'm not looking for someone to do it for me. – TillEmpea Jan 25 '17 at 15:45
  • Oh also, you kind of coul've followed the link I provided that shows you exactly my code ;) – TillEmpea Jan 25 '17 at 15:46
  • I actually **did** down-vote you. Not because of your comment. I could have done that long ago. I made my first comment so that you will edit it with your code. You failed to do that. You shouldn't be posting your code on another website. SO has a code formatting system. That code you posted on another site could be gone anytime. Happy coding! – Programmer Jan 25 '17 at 15:50
  • Fair enough, then please clarify your intention. This way you just came over as pedantic. In case you also marked it as duplicate, the question it links to is a different one with a different solution. – TillEmpea Jan 25 '17 at 15:56
  • In case you didn't catch my edit: The question you linked as duplicate is very different, as it uses the old animation system. That's why I clarified which system I use in my post. – TillEmpea Jan 25 '17 at 16:25

1 Answers1

2

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.

Nonameghost
  • 773
  • 1
  • 7
  • 11
  • You're probably right. Hadn't really figured out the parameters yet. I ended up duplicating the animator state, having the duplicate play in reverse and switch between the states while checking for the desired animation speed and normalized time. That way I can switch between the two states without any hitches, as the next state picks up at `1-timePassed`if you will – TillEmpea Jan 25 '17 at 15:14
  • I tested it and it kinda worked. For some reason my check if the time passed is over maximum or under 0 didnt work anymore, so I'll stick to my solution for now (NTARS ;)) – TillEmpea Jan 25 '17 at 15:37