• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lev-Lukomskyi · Jul 23, 2015 at 02:43 PM · mecanim

Backward animation in Mecanim

With Unity old animation system you could make speed = -1 and animation will play backwards - so easy! But with new mecanim you cannot do this — if you make negative animator speed you will get error in console.

In my scenario I have tutorial animation with 6 slides, on each slide player have buttons «prev» and «next» so it can move forward an backward between slides with nice animation. Because mecanim cannot do backward animation, I had to write comlex script to manage each frame of animation on Update(). Here it is and I hope It will help someone:

 using UnityEngine;
 
 public class Tutorial : MonoBehaviour
 {
     const float slidePeriod = 1 / 3f;
 
     Animator animator;
     int stateHash;
     float curAnimTime;
     float totalAnimTime;
     float nextSlideTime;
     bool playForward;
 
     void Start()
     {
         transform.localPosition = Vector3.zero;
         animator = GetComponent<Animator>();
         stateHash = animator.GetCurrentAnimatorStateInfo(0).tagHash;
         totalAnimTime = slidePeriod * 5; //animator.runtimeAnimatorController.animationClips[0].length;
         animator.speed = 0;
         enabled = false;
         if (PlayerPrefs.GetInt("tutorialShown", 0) == 0)
             Show();
         else
             gameObject.SetActive(false);
     }
 
     void Update()
     {
         if (playForward)
         {
             curAnimTime += Time.unscaledDeltaTime;
             if (curAnimTime > nextSlideTime)
             {
                 curAnimTime = nextSlideTime;
                 enabled = false;
             }
         }
         else
         {
             curAnimTime -= Time.unscaledDeltaTime;
             if (curAnimTime < nextSlideTime)
             {
                 curAnimTime = nextSlideTime;
                 enabled = false;
             }
         }
         animator.Play(stateHash, 0, curAnimTime/totalAnimTime);
     }
 
     public void PlayForward()
     {
         playForward = true;
         nextSlideTime += slidePeriod;
         if (nextSlideTime > totalAnimTime)
             nextSlideTime = totalAnimTime;
         enabled = true;
     }
 
     public void PlayBackwards()
     {
         playForward = false;
         nextSlideTime -= slidePeriod;
         if (nextSlideTime < 0)
             nextSlideTime = 0;
         enabled = true;
     }
 
     public void ShowSlide(int slideNo)
     {
         curAnimTime = nextSlideTime = slidePeriod * slideNo;
         enabled = false;
         animator.Play(stateHash, 0, curAnimTime/totalAnimTime);
     }
 
     public void Show()
     {
         gameObject.SetActive(true);
         ShowSlide(0);
     }
 
     public void Done()
     {
         PlayerPrefs.SetInt("tutorialShown", 1);
         gameObject.SetActive(false);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Cherno · Jul 23, 2015 at 02:57 PM

Animator states can be set to any speed, including negative. This has always been true for the editor environment, and with teh release of Unity 5.1 it's also possible to change the speed of an animation state at runtime.

Runtime Animator State Properties (speed, mirror, cycleOffset) for 5.1

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lev-Lukomskyi · Jul 24, 2015 at 01:34 PM 0
Share

Wow, I didn't know about this speed parameter

alt text

Yeah, it works, but if I set speed parameter to 0 animation doesnt stop. So I set animator.speed = 0 to stop animation, and ended with this code:

 public void PlayForward()
 {
     animator.SetFloat("Speed", 1);
     animator.speed = 1;
 }

 public void PlayBackwards()
 {
     animator.SetFloat("Speed", -1);
     animator.speed = 1;
 }

 public void StopAnimation()
 {
     animator.speed = 0;
 }

A little confusing.. But anyway, I will use my old code, because mecanim events are not precise. Event, which should stop animation at particular frame stops few milliseconds later and I see beginning of animation between slides.

unityspeedanimationparameter.png (18.6 kB)
avatar image
1

Answer by iHaveReturnd · Jul 23, 2015 at 02:54 PM

Alternatively, you could just duplicate your animation in the Animator tree, set the speed of the duplicate to -1 in the inspector, and just set the conditions for that animation to be the conditions of your backwards animation. I don't like that you can't modify speed via code anymore, but this is a pretty simple solution.

I just tested this out on an animation and it worked just fine. This way you could still setup all the blending and not have to mess with code if you didn't want to.

Edit: As per Cherno's post, glad to see they added it back in.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lev-Lukomskyi · Jul 24, 2015 at 01:45 PM 0
Share

This is not suitable for my scenario as I have animation with 6 slides. I could make 6 mecanim states for each slide and use mecanim blending, but then I lose possibility of easy animation editing through Animation Window - I have complex animations between slides.

avatar image gstarch · Nov 02, 2017 at 07:14 PM 0
Share

This worked well for me. Was an easy solution to integrate into $$anonymous$$ecanim with no additional code. (v 2017.2)

avatar image
0

Answer by jthouston · Aug 25, 2016 at 11:37 AM

Hey, I've been working with Unity 5.3 and another way I found to do it was to create a new float parameter for the animator controller (call it 'direction') and then enable it as a multiplier parameter for the Speed (with Speed = 1) in the state you wish to play in reverse.

Then in the scripting I set 'direction' = -1f and the animation plays in reverse.

Animtor anim = GetComponent(); anim.SetFloat('direction', -1);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by NatCou · Feb 08, 2017 at 10:52 AM

Heres how I did it

1)in the animation clip you want to control the speed create a new state "animSpeed"

2)On the script attached to the object that plays the animation add

 void Start () {

     //accessing the animation on the object to change the speed
     //create a animSpeed parameter in the animation clip you wish to control
     Animator anim = GetComponent<Animator>();
     anim.speed = animationSpeed;
 }

Hope it helps :)

alt text


captureb.jpg (22.5 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 4 Mechanim: play audio when a foot touches the ground 4 Answers

mecanim blend direction 0 Answers

Mecanim animation not played 1 Answer

Javascript to control parameters of animator controller 3 Answers

Mecanim: NullReferenceException 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges