Search Unity

Animator go to default state by script

Discussion in 'Animation' started by scozirge, Oct 27, 2021.

  1. scozirge

    scozirge

    Joined:
    Feb 24, 2015
    Posts:
    80
    Is there any way to make an animator go to its default state animation?
    It's hard to make all animator's default state name the same, so I can't simply call Play("Default Animation")

    Many thanks
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hello,

    As far as I know, there's no easy way of saving the default state as a variable.

    The easiest way of going back to the default state is to make a transition to it from "Any State" with a condition, and then control the parameter of the condition through code.

    Example:
    Animator.SetBool("Default", true);


    Default.JPG
     
  3. scozirge

    scozirge

    Joined:
    Feb 24, 2015
    Posts:
    80
    Yes, if there is no way to easily go default state, I will do it in your way, but I need to ask my friend to make sure all the animators have the same trigger state called "Default". As my project already run a while, there are many animators that need to modify. So that's my final choice.

    Thank you~
     
    Unrighteouss likes this.
  4. scozirge

    scozirge

    Joined:
    Feb 24, 2015
    Posts:
    80
    I found an easier way to go back to the default animation state in some cases.
    If animation-names equal to state name, then in Start() function save current animation-name as default animation.
    So it can easily go back to the default animation state whenever it needs.

    For example:
    upload_2021-11-3_15-22-30.png
    upload_2021-11-3_15-23-7.png