Search Unity

Resolved [Resolved] Animator.GetCurrentAnimatorStateInfo(0)

Discussion in 'Animation' started by ArcticStagMedia, Mar 2, 2023.

  1. ArcticStagMedia

    ArcticStagMedia

    Joined:
    Aug 21, 2020
    Posts:
    5
    Hey there,

    I am trying to dynamically get the length of an animation that I am playing using
    Code (CSharp):
    1. Animator,Play(string)
    because this gives better overall results visually with sprite animations.

    When I call
    Code (CSharp):
    1. Animator.GetCurrentAnimatorStateInfo(0)
    I do not get the currently playing animation, only the information from the Idle animation, even when I play other animations

    Any thoughts on how I can get the details of the currently playing animation without hard coding values or names of animations on for details
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Animator.Play doesn't change the current state until the animation system next updates (i.e. next frame). So you need to either wait a frame before checking the current state or use a non-retarded animation system.
     
    SF_FrankvHoof likes this.
  3. ArcticStagMedia

    ArcticStagMedia

    Joined:
    Aug 21, 2020
    Posts:
    5
    Thanks, I'm not sure to what you mean by
    use a non-retarded animation system
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Use a different animation system that doesn't do dumb stuff like force you to wait a frame to get information about something you just told it to play.
     
  5. ArcticStagMedia

    ArcticStagMedia

    Joined:
    Aug 21, 2020
    Posts:
    5
    Oh okay, Do you have any recommendations for other Animation Systems? Anything in the Assets store for 2D sprites?
     
  6. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    I don't know exactly what info you're trying to get from the state, but my Animancer plugin (link in my signature) would likely make it extremely easy to achieve what you want. Animancer Lite is free and most of the Pro-Only features aren't necessary for sprite based animations.

    The Legacy Animation system is also a fine choice. I don't personally like the way it works, but if I hadn't made Animancer I'd definitely choose Legacy over Animator Controllers for sprite based animations.
     
  7. ArcticStagMedia

    ArcticStagMedia

    Joined:
    Aug 21, 2020
    Posts:
    5
    Thank you very much, I will take a look at Animancer!
     
  8. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    983
    While I don't entirely disagree with the above poster's sentiment about mechanim, in this case there is a simple albeit surprisingly poorly documented feature. Simply call the Update() method on your animator to 'pump' it thus allowing you to get access to the new state info immediately.
     
    hardkor444 likes this.