Rotate Object to face player 2d

I’m making a 2D platformer , but i have a problem making the enemy face the player. I was able to make the main character face left or right by rotating its plane mesh 180 so its in reverse depending on what key is pressed a,or d…I want the toaster/ enemy to face the player if the its within a certain distance. In other word rotate the toaster to (90,0,0) on the xyz axis if the character is on the Right side of the toaster , and (90,180,0) if its on the left of the toaster. …

I made this script to check the distance between the Enemy and player and depending on the y value of the rotation it will flip to 180 or 0 on the y ,

the thing is that it doesnt work properly what am I doing wrong ? How do i make the Toaster face the player?

var NoticeDistance = 10;
var Player: Transform;
var RotatedToRight= false;
var RotatedToLeft= true;

function LateUpdate(){

var relativePoint = transform.InverseTransformPoint(Player.position);

if(Vector3.Distance(transform.position,Player.position) <= NoticeDistance){
         if (relativePoint.x < 0.0 && Input.GetKeyUp("d") ){
        print ("Object is to the Right");
        transform.eulerAngles = Vector3(90,0,0);
       /*   if(transform.rotation.eulerAngles.y >= 160 ){
            print("Y rotation is about 180");
    else if (relativePoint.x > 0.0 && Input.GetKeyUp("a") ){
        print ("Object is to the Left");
        transform.eulerAngles = Vector3(90,180,0);
      /*  if(transform.rotation.eulerAngles.y <= 5 ){
          print("Y rotation is about 0");
          } */
        print ("Object is directly ahead");
function RotateLeft(){
      //     yield WaitForSeconds (3); //Debug yeild
           transform.eulerAngles = Vector3(90,180,0);
           RotatedToLeft = true;
     //      yield WaitForSeconds (5);//Debug yeild
           RotatedToRight = false;
function RotateRight(){
     //      yield WaitForSeconds (3);//Debug yeild
           transform.eulerAngles = Vector3(90,0,0);
           RotatedToRight = true;
     //      yield WaitForSeconds(5);// Debug yeild
           RotatedToLeft = false;          

I think this is where you’re misunderstanding what it’s doing:

var relativePoint = transform.InverseTransformPoint(Player.position);

The InverseTransformPoint actually turns the point into local space of the object. Therefore if the object is rotated then the point will be converted as well. When you check the point with this code make sure to check whether the point is ‘in front or behind’ instead of ‘to the left or the right’. Therefore using this technique you’re basically SWITCHING sides when it’s behind instead of always turning left if x < 0. Because the relative point is either in front or behind.

Also if it’s scaled in the X-axes the point will be much closer in local space. (Literally stretch and squash of the relative point.)

To be consistent you’ll probably be better of to do:

var direction : Vector3 = Player.position - transform.position.

You then get a vector pointing from the transform to the Player. Which should be exactly what you need as your code that checks the position checks for this difference already correctly (I just quickly looked at the code)

Another sidenote

You’re using the transform many times. Because it isn’t cached or stored it looks for the transform component each time. Have a look at this: Are .gameObject and .transform both using GetComponent() in the background? There’s a speed difference, which might be neglible in this case but can become obvious if you’re going to expand this code a lot.