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
  4. Dismiss Notice

Getting animator state length after crossfade

Discussion in 'Scripting' started by Giustitia, Dec 22, 2021.

  1. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Hi everybody!

    I'm having troubles getting the duration of the next state/clip after using Animator.Crossfade.

    If I try to get it with Animator.GetCurrentAnimatorStateInfo() or Animator.GetNextAnimatorStateInfo() it seems to return the current and next state (if a transition is being performed, although 0) despite my crossfade call.
    I guess Animator.Crossfade is not called immediately, so how do I get the length of the state i'm trying to crossfade to? Without using some coroutine trick to skip a frame and that kindy of dirty stuff.

    Example code
    Code (CSharp):
    1. animator.CrossFade("StateName", 0.05f);
    2. Debug.Log("Current state length: " + animator.GetCurrentAnimatorStateInfo(0).length);
    3. Debug.Log("Next state length: " + animator.GetNextAnimatorStateInfo(0).length);

    Also, it would be awesome to be able to get any state by name/hash, but for some reason that's not possible. The only thing I can do is getting the runtimeAnimatorController and iterate over the AnimationClips and filter by name, but thats not good since the animation clip name may differs if i choose to change the animation someday, while animator state should not.

    Thanks, and merry christmas :D
     
    Last edited: Dec 22, 2021
    radiantboy likes this.
  2. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Hate to do so but bump :oops:
     
  3. NikyWilliams

    NikyWilliams

    Joined:
    Jun 2, 2017
    Posts:
    3
    Had same exact issue and a Google search brought me here.
    I realize this thread is quite old but figured I'd post a sort of "work around" that did not require coroutines and whatnot. Hopefully it will help someone.
    I needed to return the length of the animation clip that I just cross faded to but both "GetCurrentAnimatorStateInfo" and "GetNextAnimatorStateInfo" were not reflecting the state I just crossfaded to. For this to work though, your states and animation clips have to have the same name. Once you perform a CrossFadeInFixedTime, pull back a list of all the animation clips on the runtimeAnimationController, enumerate them and find the clip that matches the name of your state. Kinda ugly, but has been working for what I needed. I'm sure you could probably pre-load this information so you don't have to iterate through all the clips each time you cross fade and just do a lookup in a Dictionary or something for the info you need.

    Unity 2022.3.6f1

    Code (CSharp):
    1.  
    2. // Set animation to crossfade
    3. animationSetting.animator.CrossFadeInFixedTime(animationSetting.name, animationSetting.transitionDuration, 0, animationSetting.timeOffset);  
    4.  
    5. // Attempt to find the animation clip with same name as the state
    6. for (int index = 0; index < animationSetting.animator.runtimeAnimatorController.animationClips.Length; index++) {
    7.   if (animationSetting.animator.runtimeAnimatorController.animationClips[index].name.Equals(animationSetting.name)) {
    8.     string clipName = animationSetting.animator.runtimeAnimatorController.animationClips[index].name;
    9.     float clipLength = animationSetting.animator.runtimeAnimatorController.animationClips[index].length;
    10.     Debug.Log(string.Format("Found clip named '{0}' with length '{1}'", clipName, clipLength));
    11.     return clipLength;
    12.   }
    13. }
    14.