Search Unity

Blending states without making transitions in Animator

Discussion in 'Animation' started by ivolkov, Feb 26, 2017.

  1. ivolkov

    ivolkov

    Joined:
    Feb 15, 2017
    Posts:
    7
    I need to start animation state at certain time and at certain coordinates. I use "Animator.Play()" function and it works good, but it has no blending with previous state. I could make the trigger or condition in Animator, but in this case I cannot control the time of animation start (e.g. normalizedTime).
    How can I blend animations without Animator or how can I start transition between states with normalized time?
     
  2. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    I can't speak to the actual use of this functionality as I have never had a case for it. However, it would seem that Animator.Crossfade() should do what you are trying to accomplish.
     
    Ghosthowl and thomasdots like this.
  3. ivolkov

    ivolkov

    Joined:
    Feb 15, 2017
    Posts:
    7
    Thanks for the answer! Animator.CrossFade works, but is there way to supress blending of root bone position?
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    TrickyHandz likes this.
  5. ivolkov

    ivolkov

    Joined:
    Feb 15, 2017
    Posts:
    7
    Sounds good, in method OnAnimatorMove() I can assign root position. However how can I get the required root position? I want to get it from animation which fade out.
    SetTarget() call and following Update() call lead to OnAnimatorMove() - it is the vicious circle
     
  6. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    The solution would be to use SetTarget but don't call Update(), the engine will tick the animator which will compute the target position which then you can retrieve in the next OnAnimatorMove to override the root motion blending as you wish.
     
  7. ivolkov

    ivolkov

    Joined:
    Feb 15, 2017
    Posts:
    7
    Should I have second animator for my character? Because animator.CrossFade and animator.Play cannot be executed simultaneously.
    What if I have 30-40 characters?
     
  8. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    I'm not sure what you are trying to achieve. can you give us more detail?
     
  9. ivolkov

    ivolkov

    Joined:
    Feb 15, 2017
    Posts:
    7
    Ok, I'll try to explain what I need step by step.
    I had a character in AnimationState1 and I wanted to make transition it to AnimationState2. It's important to note that I need precise coordinates of character in any time.
    So I wrote code like this:
    Code (CSharp):
    1. float normalizedTime = getNormalizedTimeForAnimation(AnimationState2);
    2. animator.Play(AnimationState2, 0, normalizedTime);
    3. animator.SetTarget(AvatarTarget.Root, normalizedTime);
    4. animator.Update(0f);
    5. character.position = animator.targetPosition;
    It works good, but there is no smooth transition between AnimationState1 and AnimationState2.
    So I used CrossFade() function instead Play() and one problem appeared:
    I cannot get required root position for AnimationState2 during cross fading.
    Code (CSharp):
    1. float normalizedTime = getNormalizedTimeForAnimation(AnimationState2);
    2. animator.CrossFade(AnimationState2, transitionDuration, 0, normalizedTime);
    3. animator.SetTarget(AvatarTarget.Root, normalizedTime);
    4. animator.Update(0f);
    5. character.position = animator.targetPosition;
    This code doesn't work. If I don't use SetTarget() function, I won't be sure about precise coordinates.