Unity Animator SetTrigger not resetting to default

Viewed 4556

As I hopefully correctly understand the Animator "SetTrigger" will set the animation to false once it's done (correct me if I'm wrong), but this is not the case for my project.

I have two states. Default (do nothing) and Move. Default is automatically entered when the app is opened and Move is triggered when I call "SetTrigger". I expect that Animator will set this animation id to false once the animation is done and then go back to Default state. but this is not the case - animation keeps repeating. I can achieve this by calling "ResetTrigger" manually but then I need to detect when the animation is done, which is not convenient in my case.

I want to achieve that animation is played only once when called. Any ideas?

Thanks

EDIT: Adding animation controller

enter image description here

public class TestAnimation : MonoBehaviour
{

    private Animator _animation;
    private static readonly int Playing = Animator.StringToHash("PlayingTrigger");

    // Start is called before the first frame update
    void Start()
    {

        _animation = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.A))
        {
            _animation.SetTrigger(Playing);
         //   _animation.ResetTrigger(Playing);

            IDebug.Log("Playing animation ");
        }
        
        if (Input.GetKeyDown(KeyCode.R))
        {
            _animation.ResetTrigger(Playing);
            IDebug.Log("reseting animation ");
        }
        

    }
}
2 Answers

the Animator "SetTrigger" will set the animation to false once it's done

-> Nope!

The only thing it does is resetting the trigger itself once it was used for a transition, it doesn't mean the animation state is reset once the animation has reached the end.

If you want your Animator go back to the idle/default state after your animation is done then in the controller add a transition

yourState -> Idle/Default

with

  • Has Exit Time -> True
  • Duration -> as you like
  • Exit Time -> 1 - Duration

No further condition.


One pitfall though: Afaik the triggers can stack!

This means if you call 5 times SetTrigger then that trigger will be used for 5 transitions until it is finally reset and doesn't trigger the transition anymore!

At least that was the case in earlier versions .. not sure if this was ever changed.

Therefore I ended up using a trigger resetter directly attached not to a specific state but the Basic layer itself -> it is automatically called for each and every entered state:

public class TriggerRestter : StateMachineBehaviour
{
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        foreach(var p in animator.parameters)
        {
            if (p.type == AnimatorControllerParameterType.Trigger)
            {
                animator.ResetTrigger(p.name);   
            }
        }
    }
}

As @derHugo pointed out; the trigger itself gets reset but not the actual animation. It was bugging me for a while. The fix is pretty simple, all you need to do is make the transition back to the previous state(from which the trigger was triggered).

Related