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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Using the API, how can I recreate an animator state where a crossfade is in progress?

Discussion in 'Animation' started by tcz8, Oct 4, 2022.

  1. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    498
    I have characters rendered using DrawMeshInstancedIndirect and an animation shader, they have no game object. I'm switching them at runtime with game object based rag dolls when physics interaction is required (ie.: getting hit by a car). Matching the animation time works flawlessly, and you can't see the switch EXCEPT when a transition is in progress.

    Obviously I need to take into account the transition which is blending two animations together, but so far I'm having no luck "recreating" an animator state where a transition is halfway done.

    I need a way to recreate that crossfade at a specific time of the transition. I tried calling Animator.Play() to set the source animation and then Animator.CrossFadeInFixedTime for the target animation. I was hoping the NormalizedTransitionTime parameter would allow setting the transition to a specific time, but it doesn't work as I had hoped.

    When calling Play() and CrossFadeInFixedTime() I'm not even sure if I should set the animation times to the time at the start of the transition or at the time i'm trying to resume (tried both). I'm not even sure NormalizedTransitionTime does what I want.

    I also tried using Animator.Update(transitionTimeToResume) but that doesn't seem to work either.

    If you have any insight on how this works, PLEASE share.
     
  2. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    498
    Figured it out.

    You need to do this:
    Code (CSharp):
    1. animator.PlayInFixedTime(sourceAnimation, 0, sourceAnimTime);    // Play the source anim of the transition at the current time in seconds
    2.                 animator.Update(0.0f);    // Force an animator update
    3.                 animator.CrossFadeInFixedTime(targetAnimation, transitionDuration, 0, targetAnimTime, transitionNormalizedTime);  
    SourceAnimation = the animation/state from which the transition is started
    SourceAnimTime = the time the source animation was at, at the moment you are trying to resume
    TargetAnimation = the animation/state we are to transitioning to
    TargetAnimTime = the time the target animation was at, at the moment you are trying to resume
    TransitionDuration = the original duration of the transition you want to resume
    TransitionNormalizedTime = The time along the transition you want to resume the transition at (normalized)


    Please note that I'm still testing if SourceAnimTime and TargetAnimTime should match the moment you are trying to resume in the transition or the begining of the transition. So far it seems to be the moment you are trying to resume, but I sometimes have inconsistencies. It may be caused by the other animation system I am trying to synchronize with.
     
    radiantboy likes this.