Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Accessing States at Runtime

Discussion in 'Animation' started by Zullar, Dec 8, 2018.

  1. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    I am being forced to hardcode my animation clip lengths into c# or the Animator Controller and it seems completely wrong.



    My requirements:
    1: Have animations ~normalized. For example if I want an animation to play fully over 0.5sec and the clip length is 1.2sec then I need the animation to play at 2.4x speed. Ideally my input is something like Animator.EnterState(Attack) and then Animator.SetFloat(AttackSpeed, 2f).
    2: I need the speed of the animations to be variable at runtime. For example I pause the animation if the target gets frozen by calling Animator.SetFloat(AttackSpeed, 0f), then resume it when unfrozen.
    3: I need Avatar masks so that you can control right hand, left hand, and movement independently.
    4: Network Synced

    Currently I have all this working, but am hard coding my animation clip length! Does anybody know of a way to accomplish this without hardcoding animation clip lengths? My specific hangup is I cannot access a States Animation Clip length. This is possibly a bug but if I call animatorTemp.Play(StateAttack.hash) and then access the states clip length by calling animatorTemp.GetCurrentAnimatorStateInfo(...).length it returns info for the OLD state, not the NEW StateAttack that I just told it to Play( ). If I wait a few Update( ) frames and call animatorTemp.GetCurrentAnimatorStateInfo(...).length then it gives the correct value. Play( ) seems like it should be instant... but unsure why there is a delay :[

    UnityEditorInternal has tools but isn't safe.

    So seems like what I want to do is simple... enter state and play clip over 0.5sec... but it does not appear to be doable. Am I missing something?!

    Thanks in advance.
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    UnityEditorInternal is not available at runtime so that wouldn't work anyway.

    I don't believe it's possible to control the speed of a specific state at runtime with Mecanim, only the Animator.speed which affects all animations at once (which is bad if you're transitioning between attack and something else).

    My Animancer plugin would meet your first 3 requirements and possibly the 4th, but I have no idea if a NetworkAnimator will work with the Playables API.
     
    Zullar likes this.