Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Difference between playing an animation state directly and via setting trigger

Discussion in 'Scripting' started by davidhfw, Mar 17, 2017.

  1. davidhfw

    davidhfw

    Joined:
    Aug 17, 2016
    Posts:
    77
    In the Animator, I have the following animation states:

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

    Transition condition is Trigger

    Code (CSharp):
    1. Animator anim = GetComponent<Animator>();
    2. if (x == 1) {
    3. anim.SetTrigger("Attack");
    4. }
    vs

    Attack State (no transistion condition)

    Code (CSharp):
    1. Animator anim = GetComponent<Animator>();
    2. if (x == 1) {
    3. anim.Play("Attack");
    4. }
    Both methods activate the Attack animation state. Is there any difference in the two approaches?

    Thanks !
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    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.
     
    Arkadi097 and nhancaosi like this.
  3. davidhfw

    davidhfw

    Joined:
    Aug 17, 2016
    Posts:
    77
    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?
     
  4. malosal

    malosal

    Joined:
    Jun 22, 2013
    Posts:
    151
    Can someone also explain what the difference is between having an animator component and simply just having an animation component?