title: Unity 2D: How to make patrolling AI turn around?
body: <p>I'm making a 2D platformer in Unity, and made a patrolling enemy with code from a tutorial video. The enemy basically moves randomly to different spots in the scene. But how can I make the sprite turn around?</p>
  
  <p>This is my code so far. I've tried with different approaches, but not getting the expected behavior.</p>
  
  <pre><code>public float speed = 10f;
  private float waitTime;
  public float startWaitTime = 1f;
  
  public Transform[] moveSpots;
  private int randomSpot;
  
  private bool moving = true;
  private bool m_FacingRight = true;
  
  void Start()
  {
      waitTime = startWaitTime;
      randomSpot = Random.Range(0, moveSpots.Length);
  }
  
  void Update()
  {
      transform.position = Vector2.MoveTowards(transform.position, moveSpots[randomSpot].position, speed * Time.deltaTime);
  
      if (Vector2.Distance(transform.position, moveSpots[randomSpot].position) &lt; 0.2f)
      {
  
          //Debug.Log(m_FacingRight);
  
          // if (moving.x &gt; 0 &amp;&amp; !m_FacingRight)
          // {
          //     Debug.Log("moving right");
          //     Flip();
          // }
          // else if (moving.x &lt; 0 &amp;&amp; m_FacingRight)
          // {
          //     Debug.Log("moving left");
          //     Flip();
          // }
  
          if (waitTime &lt;= 0)
          {
              //moving = true;
              randomSpot = Random.Range(0, moveSpots.Length);
              waitTime = startWaitTime;
          }
          else
          {
              //moving = false;
              waitTime -= Time.deltaTime;
          }
      }
  }
  
  //private void Flip()
  //{
      //m_FacingRight = !m_FacingRight;
      //transform.Rotate(0f, 180f, 0f);
  //}
  </code></pre>
  
  <p>**********************************EDIT****************************************</p>
  
  <p>I ended up with this for the enemy script movement</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>private bool facingRight = true;
      private Rigidbody2D rigidBody2D;
      private SpriteRenderer spriteRenderer;
  
      public Vector2 speed = new Vector2(10, 0);
      public Vector2 direction = new Vector2(1, 0);
  
      private void Awake()
      {
          rigidBody2D = GetComponent&lt;Rigidbody2D&gt;();
          spriteRenderer = GetComponent&lt;SpriteRenderer&gt;();
      }
  
      void OnCollisionEnter2D(Collision2D collision)
      {
          if (collision.collider.CompareTag("Wall") || collision.collider.CompareTag("Player"))
          {
              direction = Vector2.Scale(direction, new Vector2(-1, 0));
          }
  
          if (!collision.collider.CompareTag("Ground"))
          {
              if (!facingRight)
              {
                  Flip();
              }
              else if (facingRight)
              {
                  Flip();
              }
          }
      }
  
      void FixedUpdate()
      {
          Vector2 movement = new Vector2(speed.x * direction.x, 0);
          movement *= Time.deltaTime;
          transform.Translate(movement);
      }
  
      private void Flip()
      {
          facingRight = !facingRight;
          if (!facingRight)
          {
              spriteRenderer.flipX = true;
          }
          else if (facingRight)
          {
              spriteRenderer.flipX = false;
          }
      }</code></pre>
  </div>
  </div>
  </p>