• 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 /
  • Help Room /
avatar image
0
Question by WARdd · Sep 08, 2017 at 01:21 PM · animationanimatormecanimserializationcrossfade

animator.crossfade() not working properly

I'm trying to create a class that can save and load the exact animation state of an animator. I can load up a normal animation state just fine using animator.Play(), the problem is I can't seem to load a transition between 2 states properly.

my code looks like this right now:

 [Serializable]
     class AnimState {
         //variables...
 
         /// <summary>
         /// Apply this AnimState object to the given animator: LOADING
         /// </summary>
         public void Apply(int layer, Animator anim) {
             //current animation info
             anim.Play(hash, layer, time);
             anim.SetLayerWeight(layer, weight);
 
             //transition info
             if(inTransition) {
                 anim.CrossFade(transitionHash, transitionDuration, layer, transitionTime);
             }
         }
     }

But the result doesn't set the animator in the transition state between the two animations, it just puts the animator in either the source or the goal state completely. Did I misunderstand how CrossFade() works? How should I accomplish the desired effect?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by WARdd · Sep 08, 2017 at 09:11 PM

Managed to find the solution, just posting it for people who might stumble on it. CrossFade() can be used to start a transition, but the animator needs to be updated manually to get it to the right point in time. Getting this to work requires a few nasty workarounds, but I arrived at a reasonable solution:

 [Serializable]
     class AnimState {
         //MAKE SURE THAT NEXT STATE ANIMATION OFFSET == 0
         //variables...
 
         /// <summary>
         /// Create AnimState object using state of given anim: SAVING
         /// </summary>
         public AnimState(int layer, Animator anim) {
 
             //retrieve current animation info
             AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(layer);
             this.currentHash = info.fullPathHash;
             this.currentLength = info.length;
             this.currentTime = info.normalizedTime * currentLength;
             this.weight = anim.GetLayerWeight(layer); 
 
             //if there is a transition, move on and gather transition info
             inTransition = anim.IsInTransition(layer);
             if(inTransition) {
                 AnimatorTransitionInfo transInfo = anim.GetAnimatorTransitionInfo(layer);
                 AnimatorStateInfo nextInfo = anim.GetNextAnimatorStateInfo(layer);
 
                 if(transInfo.normalizedTime == 0f) { 
                     //move animation forward so we have proper info to work with.
                     anim.Update(timeShift);
                     transInfo = anim.GetAnimatorTransitionInfo(layer);
                     nextInfo = anim.GetNextAnimatorStateInfo(layer);
                     anim.Update(-timeShift);
                 }
 
                 //retrieve all necessary info
                 nextHash = nextInfo.fullPathHash;
                 nextLength = nextInfo.length;
                 transitionTime = nextInfo.normalizedTime * nextLength;
                 transitionLength = transitionTime / transInfo.normalizedTime;
             }
         }
 
         /// <summary>
         /// Apply this AnimState object to the given animator: LOADING
         /// </summary>
         public void Apply(int layer, Animator anim) {
             //apply current animation info
             anim.Play(currentHash, layer, currentTime/currentLength);
             anim.SetLayerWeight(layer, weight);
             anim.Update(0f);
 
             //if necessary, apply transition info
             if(inTransition) {
                 //rewind current animation, so it can be wound forward along with the fading
                 float playTime = Util.PosModulo((currentTime - transitionTime) / currentLength, 1f);
                 anim.Play(currentHash, layer, playTime);
                 anim.Update(0f);
 
                 //set crossfade time and update to the required point
                 float crossPlayTime = Util.PosModulo(transitionTime / nextLength, 1f);
                 anim.CrossFade(nextHash, transitionLength / currentLength, layer, crossPlayTime);
                 anim.Update(transitionTime);
             }
         }
     }
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 RedGirafeGames · Nov 13, 2020 at 11:22 PM

After hours of tests I ended with this version (based on yours, thanks @WARdd ). An advantage is that it works even with animator.speed == 0 (which is not the case using the "length" properties because they have "infinity" values in that case)

Get State :

     private void TakeSnap()
     {
         AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(layer);
         currentHash = info.fullPathHash;
         currentLength = info.length;
         normalizedTime = info.normalizedTime;
 
         inTransition = animator.IsInTransition(layer);
         if (inTransition)
         {
             Debug.Log("In transition...");
 
             AnimatorTransitionInfo transInfo = animator.GetAnimatorTransitionInfo(layer);
             AnimatorStateInfo nextInfo = animator.GetNextAnimatorStateInfo(layer);
 
             transitionDuration = transInfo.duration;
             transitionNormalizedTime = transInfo.normalizedTime;
             nextHash = nextInfo.fullPathHash;
             nextNormalizedTime = nextInfo.normalizedTime;
         }
     }

Apply State :

     private void ApplySnap()
     {
         if (inTransition)
         {
             animator.Play(currentHash, layer, normalizedTime);
             animator.Update(0.0f);
             var transitionFixedTime = transitionDuration * transitionNormalizedTime;
             animator.CrossFade(nextHash, transitionDuration, layer, nextNormalizedTime,
                 transitionNormalizedTime);
         }
         else
         {
             animator.Play(currentHash, layer, normalizedTime);
         }
     }

Some Unity documentation is really a shame... not even possible to understand how to use crossFade without countless tests...

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

228 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 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 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 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 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 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 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 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 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

Humanoid Animations 0 Answers

How to make player rotate 180° and run that direction with mecanim + vice versa? 1 Answer

Mecanim animation becomes stuck on object disable. 0 Answers

is it possible to convert generic animations to humanoid ones? 1 Answer

Animate Physics, Character movement problem 0 Answers


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