title: How to trigger animator animation once
body: <p>I have a mecanim character that I have set up animations for in the animator. I added a 4 triggers to trigger the different animations. My question is, what is the best way to trigger the run animation when a the left/right keyboard button is pressed? What I have now is that the animations keeps getting triggered over and over again because I have the code in an <code>Update()</code>. What can I do so it only gets triggered once and loops through that animation?</p>
  
  <p>I have this working:</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  [RequireComponent(typeof(Animator))]
  public class AnimationController : MonoBehaviour {
  
      Animator animator;
  
      bool running = false;
  
      void Start() {
          animator = GetComponent&lt;Animator&gt;();
      }
  
      // Update is called once per frame
      void Update () {
          if(Input.GetButton("Horizontal")) {
              if(!running) {
                  animator.SetTrigger("Run");
                  running = true;
              }
          }
  
          if(Input.GetButtonUp("Horizontal")) {
              animator.SetTrigger("Idle");
              running = false;
          }
  
      }
  }
  </code></pre>
  
  <p>But it seems like it is more that what is needed to accomplish this task, especially as the actions and animations list grows.</p>