Search Unity

animationState.normalizedTime in OnStateMove acting weird

Discussion in 'Animation' started by steveh2112, Jan 13, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    169
    i have a state machine behavior attached to an animation clip and want it to do something about a 1/3rd of the way through so i have this code
    Code (CSharp):
    1. override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    2.     {
    3.         // Implement code that processes and affects root motion
    4.         AnimatorStateInfo animationState = animator.GetCurrentAnimatorStateInfo(0);
    5.         if (animationState.normalizedTime > 0.3f) {
    6.             Debug.Log("TRIGGERED at "+ animationState.normalizedTime);
    7.             _WeaponManager._BowOnBack.SetActive(false);
    8.             _WeaponManager._BowInHand.SetActive(true);
    9.         }
    10.     }
    when i look on the console i see that when i start the animation, OnStateMove starts with normalized time around 2 secs, then goes to 0 and starts counting up normal.


    why isn't it starting at 0?
    Untitled.png
    i could put an if in to exclude any strange values but seems odd its doing it at all.

    thanks

    i just added this
    if (animationState.normalizedTime > 0.3f && animationState.normalizedTime < 0.4f)
    and it works now but seems like a hack
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    717
    The time will keep going up by 1 every time an animation loops (same if it does not loop).

    I've never used OnStateMove because the whole Animator Controller system seems stupid to me, but I assume that the state with a high time is the previous state which is transitioning into the state with your script on it. So during the transition, GetCurrentAnimatorStateInfo is still returning the info about that state. Check the name hash to confirm which state it is.
     
  3. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    169
    interesting that the time goes up by one one each loop, but in my case , its not a looping animation
    i see your point about the time on the state transitioning into it, maybe that's it, but i would expect the first OnStateMove to be called at t=0 for this animation

    maybe OnStateMove is the wrong callback, i need to study the callbacks a bit more i guess

    thanks
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    717
    Even if its not a looping animation the time will still increase, it will just be frozen on the last frame rather than repeating the animation.
     
  5. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    169
    i found the answer, just use stateInfo arg and all is well

    Code (CSharp):
    1. override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    2.     {
    3.         if (stateInfo.normalizedTime < 0.5f)
    4.             return; // ignore first half of animation, wait untill she's drawn the arrow
    5.  
    6.         if (!_WeaponManager._BowDrawn)
    7.             _WeaponManager._DrawBow = true; // start bow draw logic
    8.          
    9.     }
     
    Last edited: Jan 15, 2020
unityunity