Making the enemy always face the player

Hello,
I’m having a bit of a logic issue here. I need an algorithm that will make my enemy sprite always be in either one of four directions (ie. 0, 1, 2, 3), each of which represent a different sprite of the prefab facing a different direction. I’m thinking similar to how Link to the Past did it with their sword knights that would chase you once they saw you. Here is my current algorithm, it works only for showing left vs right, but up and down don’t work. I try to take the difference between the enemy’s x and the player’s x and compare it to the y to see if it should be facing right or up, for example.

Here is my code:

``````GameObject player = GameObject.FindWithTag("Player");
moving = true;
if (gameObject.transform.position.x > player.transform.position.x && (Mathf.Abs(gameObject.transform.position.x) - Mathf.Abs(player.transform.position.x)) > (Mathf.Abs(gameObject.transform.position.y) - Mathf.Abs(player.transform.position.y)))
direction = 3;
else if (gameObject.transform.position.x > player.transform.position.x && (Mathf.Abs(gameObject.transform.position.x) - Mathf.Abs(player.transform.position.x)) <= (Mathf.Abs(gameObject.transform.position.y) - Mathf.Abs(player.transform.position.y)))
{
if (gameObject.transform.position.y > player.transform.position.y)
direction = 0;
else if (gameObject.transform.position.y <= player.transform.position.y)
direction = 1;
}

if (gameObject.transform.position.x < player.transform.position.x && (Mathf.Abs(gameObject.transform.position.x) - Mathf.Abs(player.transform.position.x)) > (Mathf.Abs(gameObject.transform.position.y) - Mathf.Abs(player.transform.position.y)))
direction = 2;
else if (gameObject.transform.position.x < player.transform.position.x && (Mathf.Abs(gameObject.transform.position.x) - Mathf.Abs(player.transform.position.x)) <= (Mathf.Abs(gameObject.transform.position.y) - Mathf.Abs(player.transform.position.y)))
{
if (gameObject.transform.position.y > player.transform.position.y)
direction = 0;
else if (gameObject.transform.position.y <= player.transform.position.y)
direction = 1;
}

if (gameObject.transform.position.x == player.transform.position.x)
{
if (gameObject.transform.position.y > player.transform.position.y)
direction = 0;
else if (gameObject.transform.position.y < player.transform.position.y)
direction = 1;
}

if (gameObject.transform.position.y == player.transform.position.y)
{
if (gameObject.transform.position.x > player.transform.position.x)
direction = 3;
else if (gameObject.transform.position.x <= player.transform.position.x)
direction = 2;
}

anim.SetInteger("Direction", direction);
``````

use transform.lookat