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

Are there no 1-line code that knows current playing animator state name?

Discussion in 'Scripting' started by leegod, Aug 20, 2019.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,472
    or animation clip name?

    from throughout all animation layers.

    if there is no built-in code, then what should I use to know current playing animation state name? and when it starts and when it ends?

    I know there is StateMachineBehaviour and OnStateEnter, Exit, but I want to know specific one State's Behaviour.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Erect a flag OnStateEnter and drop it in OnStateExit.
     
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,472
    How to do it? There is no method to derive animator's specific state's name directly.
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Put that script on the state you want.
     
  5. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,472
    Even after doing that, how to know current playing state's name?
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    If you put that script on state named "Foo" and flag is true, then the current state name is Foo. You can't have state name in runtime.
     
  7. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,472
    you are wrong, there is no way to retrieve state's name directly.

    public class HitAnimControl : StateMachineBehaviour
    {
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
    animator.SetBool("BeingHit", true);
    }

    So this is not monobehaviour, so gameObject, name does not work, and animator.name just return animator itself's name, and stateinfo variable also does not have string name inside. It just have int value of stringhash.
     
  8. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    Thats not what they are saying. They are saying you will know what state it was based on which flag got set, and you can then react accordingly.
     
    palex-nx likes this.
  9. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You didn't read my post to the end. I've offered inderect way to test if your animator in specific state or not.
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Yeah, that's garbage.

    The StateMachineBehaviour solution requires you to remember, each time you create a state, to put the correct script on the state and write in the name. In the real world, you'll not remember, and there will be bugs. You also have to remember to update the name on the script every time you update the name of the state.

    Any solution that requires you to keep two pieces of data in sync is a bug magnet, and you only do that if you really, really have to.

    @leegod, there's
    animator.GetCurrentStateInfo(layerIndex).IsName(name);
    . It's got some caveats - like the fact that the animator team hasn't documented when "current state" changes (I believe it's when the transition from the last state is past 50%).

    If you want information about the clips, you can use GetCurrentAnimatorClipInfo to get information about which clips are played, and their current weight.


    You could also get something competent like Animancer and just check the name of the state being played.
     
  11. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,472
    Yes I give up to retrieve state name, instead using animation clip name.

    Eventually, important thing at most cases doing is to control and make reaction of enemy or ally when user's character is now in specific state, which is obvious when specific animation is being played currently.
     
  12. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    This is indirect condition. I can easily imagine character playing same clip in differernt states. You have character, it have states. Statements "if character in logic state X then play clip Y" and "when clip Y is played then character it is in logic state X" are not logically equivalent.
    For now it looks like your're controlling the animator from character script and let animator take control over character script sometimes. It is buggy patter, I recommend you avoid it.
     
  13. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,472
    If game shows same character animation and same UI signs, but they are sometime different states, thats bad UI game for user because it just confuses user.

    All games should avoid this surely.

    Of course I will gather character control script to character itself or other manager script, and will use Animator from that script via register Animator as some variable. Sounds good?
     
  14. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    States what user see, your charater script states, animator states, those are completely different things. It is not neccessary to show all your logic states to user. From user point of view, they might be the same, but technically different. You don't want to tell that to user.
     
  15. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,472
    It seems you and I says different thing and all words are right. Fine.
     
    palex-nx likes this.
  16. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Probably you're right. I should more effort into my english skills.