Make enemy face player on y axis in 2d

Im making a top-down 2d shooter game and when my enemy is following me i want it to face me on the y axis like if im on the right of the enemy the enemy is facing right and if im on the left of the enemy the enemy is facing left. Pls help

Something like this should get it working

public enum Direction { Left , Right }

// Given the player's position and an other's 
// return where the "other" object should face to look at the player
public Direction GetDirection(Vector2 player , Vector2 other)
{
     return player.x < other.x ? Direction.Left : Direction.Right;
}

As for how to make the other actually use this method to look in the right direction , there are many ways, you can either :

  • set the object’s rotation.y to 0 or 180
  • set the object’s scale.x to 1 or -1
  • set SpriteRenderer’s flipY to to true or false

Thanks. it seemed to work