Rotation is limited on 180 degree

I’m using this snipplet to make my enemies look at my player in a 2D game, and it works only 50% of the time.
The problem is, if the enemy gets approached from behind, it does not turn 180 degrees to face , but however just rotates to the nearest, front or back (in unity transform.right or -transform right).

I’d loke to have them face the player all the time, not just if the player comes from the front.
Anybody find my error and care to explain?

 if (target != null) {
            Quaternion rotation = Quaternion.LookRotation(target.transform.position - transform.position, transform.TransformDirection(Vector3.up));
            transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
        }

Nevermind, fixed it with this code.

 public Transform target;
     private Vector3 v_diff;
     private float atan2;
 
     void Update()
     {
         v_diff = (target.position - transform.position);    
         atan2 = Mathf.Atan2 ( v_diff.y, v_diff.x );
         transform.rotation = Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg );
     }