Problem getting Animator to change state via code (C#)

The idea is to have a 2D arrow animate when I press “A”.

public class ArrowAnimation : MonoBehaviour 
{

    private Animator animator;

	// Use this for initialization
	void Start () 
    {
        animator = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            animator.SetBool("ArrowPop", true);
            Debug.Log("Bum");
        }
	}
}

The parameter in the Animator is as follows:

What am I doing wrong? :confused:

What’s going wrong? I’m guessing that it’s not playing but you do see “Bum” in the console log.

  • In the screenshot, it looks like the transition’s condition is “ArrowPop equals False”. Should this be True?
  • Consider using a Trigger animator parameter instead of a Bool.
  • Make sure that the ArrowPop animation actually plays on the object.

The “wrong” part is the animation not playing at all when i press “A”. ¨

I do see the “bum” in the console.

The ArrowPop needs to be false because I have set it to play when it changes to true.

I have no idea how to access the trigger animator parameter :confused:

Ok. I managed to get it working using the trigger parameter.

public class ArrowAnimation : MonoBehaviour 
{

    private Animator animator;

	// Use this for initialization
	void Start () 
    {
        animator = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            animator.SetTrigger("ArrowPop");
            Debug.Log("bum");
        }
	}
}

Now is my question. How do I make it play only once? Or atleast stop the animation after 0.5 second.

Add a transition back to Idle. The default condition on the transition will simply wait until the ArrowPop animation is done before changing back to Idle.