Difference between playing an animation state directly and via setting trigger

In the Animator, I have the following animation states:

Any State --------> Attack State

Transition condition is Trigger

Animator anim = GetComponent<Animator>();
if (x == 1) {
anim.SetTrigger("Attack");
}

vs

Attack State (no transistion condition)

Animator anim = GetComponent<Animator>();
if (x == 1) {
anim.Play("Attack");
}

Both methods activate the Attack animation state. Is there any difference in the two approaches?

Thanks !

With anim.Play, you don’t have any transition time. You can get that from anim.CrossFade, though.

The big difference is that the trigger plays into the animator controller’s systems. So if you’re playing a state that doesn’t have a transition out that uses the “Attack” trigger, nothing will happen right away in the first case. You’ll still have set the trigger, though, so when you get to a state that uses the “Attack” trigger, that transition will happen.

2 Likes

Thank you for pointing out the difference.

In the case of Any State → Attack, with the trigger set in the transition arrow, does that difference still apply?

Can someone also explain what the difference is between having an animator component and simply just having an animation component?