Search Unity

Default State in Animator

Discussion in 'Animation' started by xXApOXx, May 12, 2016.

  1. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    Hi,

    Is there any way to get the default state of an animator ? At least his name ?

    My wish is to reset an animator automatically when the player want to restart the game or when he dies. I mean I want to do it in code.

    Thx!
     
  2. soltex1

    soltex1

    Joined:
    Apr 2, 2015
    Posts:
    14
    Usually, if you don't change it, default state is the first state in the state machine. So, imagining that your AnimatorController is saved on a varaible called _ac, you can access the first state by:

    1) the first layer (if is that the case): _ac.layers[0]
    2) the state machine: _ac.layers[0].stateMachine
    3) all states in the stateMachine: _ac.layers[0].stateMachine.states
    4) and finally the first state: _ac.layers[0].stateMachine.states[0]

    with this you can do what you need with that state.

    If you want to find it by name, you can do something similar to this:
    Code (CSharp):
    1. foreach (var variable in _ac.layers[0].stateMachine.states){
    2. if (variable.state.name.Equals("Your-State-Name")){
    3. // Do something here
    4.   }
    5. }
     
    Last edited: May 17, 2016
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It's astonishing to me that Unity's API design could suck at API design as much as it does... especially given that this is their second stab at it (and the first was superior in many ways).

    But... taking a breath. I have the same desire as the OP, to replay the first (default) state. I don't have a reference to n AnimatorController; I have the Animator component. It has a runtimeAnimatorController property, but that doesn't have a layers property (it has barely anything).

    So. Given an Animator, how the heck do you find the name of the first state, or otherwise tell it "do what you did at startup again"? (I did try playing the "Entry" state but that didn't work.)
     
    akasabutski and dotsquid like this.
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    To actually answer your question:
    • In the editor - you can cast the RuntimeAnimatorController to a UnityEditor.AnimatorController to do what @soltex1 suggested. This obviously doesn't help with what you're trying to do because it won't work in a build.
    • At runtime - you simply can't do it as far as I know. The best you could do would be to have a standard state name like "Idle" so you can call CrossFade("Idle", ...).
    There is another (better) approach though: stop using animator controllers and use my Animancer plugin instead (link in my signature). It lets you control everything using scripts so if you want a function to return to the default animation you can just implement one to do exactly that. For me that's usually an Idle script so I just have a ReturnToIdle function in my base CreatureState class which tells the creature's state machine to enter the Idle state.
     
    JoeStrout likes this.
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yeah, my need here is at runtime, not in the editor. We "solved" it by requiring the default state to always be called "Default".

    But your Animancer plugin sounds like a big improvement. I hate having to create AnimatorControllers all over the place just to play a simple animation. I will definitely check that out!
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Just to follow up (since somebody pinged me on this today)... I did end up getting @Kybernetik's Animancer plugin, and now use it in multiple projects. It has been great. Much easier to use, and more flexible than Unity's approach.
     
    akasabutski likes this.