title: Unity 2D rotate the AI enemy to look at player
body: <p>I'm making a 2D game with two sides, left and right, so the player can only move to the left or right or jump. I made an AI as the enemy. The AI is working fine. He can go to the player to kill him.</p>
  
  <p>The problem is the enemy can't rotate or face the player when he goes to kill him. I want the AI to look at the player. When the player is to the left of the enemy, the enemy should rotate to the left to look at the player. I searched many websites and I didn't get any correct solution. This is my enemy script:</p>
  
  <pre><code>public class enemy : MonoBehaviour
  {
      public float speed;
      private Animator animvar;
      private Transform target;
      public GameObject effect;
      public float distance;
      private bool movingRight = true;
      public Transform groundedDetection;
      public float moveInput;
      public bool facingRight = true;
  
      void Start()
      {
          animvar = GetComponent&lt;Animator&gt;();
          target = GameObject.FindGameObjectWithTag("Player").transform;
      }
  
      // Update is called once per frame
      void Update()
      {
          if (Vector3.Distance(target.position, transform.position) &lt; 20)
          {
              transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
          }
          else
          {
              transform.Translate(Vector2.right * speed * Time.deltaTime);
          }
          RaycastHit2D groundInfo = Physics2D.Raycast(groundedDetection.position, Vector2.down, distance);
      }
  
      void OnCollisionEnter2D(Collision2D col)
      {
          if (col.gameObject.tag.Equals("danger"))
          {
              Instantiate(effect, transform.position, Quaternion.identity);
              Destroy(gameObject);
          }
          if (col.gameObject.tag.Equals("Player"))
          {
              Instantiate(effect, transform.position, Quaternion.identity);
              Destroy(gameObject);
          }
      }
  }
  </code></pre>
  
  <p>The game is 2D, left, right, with jump using Unity3D.</p>