Search Unity

Blending animation in fixed time, start in normalized time?

Discussion in 'Animation' started by DDNA, Jan 20, 2017.

  1. DDNA

    DDNA

    Joined:
    Oct 15, 2013
    Posts:
    116
    I have had this problem with the API for some time and I am hoping there is a solution.

    One of the most common things I find myself doing in blending two animations with a fixed time blend.

    i.e. Anim 1 is playing, switch to Anim 2 with a .25s overlap.

    So the API has Animator.CrossFade(string stateName, float transitionDuration, int layer, float normalizedTime );

    But the problem is that transitionDuration is in normalized time, which isn't that useful.

    There is Animator.CrossFadeInFixedTime(string stateName, float transitionDuration, int layer, float fixedTime);

    Which works good for this, at least I can specify this overlap in a time that makes sense. However now I also need to specify the start time of the animation in real time.

    This would be fine, but there is no way as far as I can tell of getting the length of an animation until after you are playing it. Without that information it is impossible for me to calculate the real time of the animation start point.

    Is there a way of blending an animation in fixed time but specifying a normalized start time? Or a way of getting the length of an animation at run-time?
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Hi DDNA,

    It either all in fixed time or all in normalized time.

    You can get all the clips from a controller with
    https://docs.unity3d.com/ScriptReference/RuntimeAnimatorController-animationClips.html
    If you are using an overridecontroller, the returned list should contain overrided clip.
    And then use this field to get the lenght of the animation clip
    https://docs.unity3d.com/ScriptReference/AnimationClip-length.html
     
  3. DDNA

    DDNA

    Joined:
    Oct 15, 2013
    Posts:
    116
    The problem is that you specify a state to start not a clip to start, and there isn't (as far as I know) a way to ask a state what clips is has until after you are already playing it. Because of this you can't complete this loop to calculate what the fixed or normalized times should be, so it is impossible to generalize this. What is frustrating about this is that is a very basic piece of functionality. I want to start this animation and blend it over x sec, which is made essentially impossible.
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    You need to specify a state because a state can be a blend tree with N clip.

    If you want to generalize this you would need to add some constraint to your state: A state can containt only 1 clip.
    Then simply name your state with the same name than your animation clip.
    This way you always know what is the lenght of the next state since you know which clip is going to be played.

    To change your unit time from normalized to fixed simply multiply the lenght of the clip with your normalize time.
    Then you will be able to use
    Animator.CrossFadeInFixedTime
    with a fixed transition duration but a normalized time for the start of the clip
     
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    The problem with blend tree is that the resulting lenght can change depending on the blending factor and the lenght of all animation clip in the blend tree.