transform.LookAt in 2d still rotates in 3d even when x and y are set to 0

im trying to get my floating enemy to always face the player, i’ve tried using transform.lookat and changed to x and y to 0 but it still rotates in the third dimension which looks horrible, the ridgidbody is set to not rotate, is there a better way to do this, its a 2d sidescroller so im just wanting it to stay flat and rotate towards the player on the z, im probably being an idiot and missing something simple

public class FloatingEyeController : MonoBehaviour
{
    Rigidbody2D rb;
    Animator anim;

    public float moveSpeed;
    public Transform player;

    public float searchRadius;
    public float attackRadius;
    public bool inSearchRange;
    public bool inAttackRange;
    Vector3 lookat;
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        player = GameObject.FindGameObjectWithTag("Player").transform;
        anim = GetComponent<Animator>();
    }
    private void Update()
    {
        lookat = new Vector3(0,0, player.position.z);
        if (Vector2.Distance(player.position, gameObject.transform.position) < searchRadius)
        {
            inSearchRange = true;
            Debug.Log("In Search Range");
        }
        if (Vector2.Distance(player.position, gameObject.transform.position) > searchRadius)
        {
            inSearchRange = false;
        }
        if (Vector2.Distance(player.position, gameObject.transform.position) < attackRadius)
        {
            inAttackRange = true;
            Debug.Log("In Attack Range");
        }
        if (Vector2.Distance(player.position, gameObject.transform.position) > attackRadius)
        {
            inAttackRange = false;
        }
        if (inAttackRange)
        {
            transform.LookAt(lookat);
        }
    }
    private void FixedUpdate()
    {
        if (inSearchRange)
        {
            rb.AddForce((player.position - transform.position) * moveSpeed);
            if (inAttackRange)
            {
               
               
                rb.velocity = Vector2.zero;
               
                anim.SetTrigger("Attacking");
            }
        }
    }
}

The rigidbody is set to not rotate, but the rigidbody isn’t rotating it - you are.

Basically LookAt is a fundamentally 3D operation and points the Z axis towards the inputted position. Problem is, in the world of sprites, “Z axis” is basically never your sprite’s “forward”, but more likely the back of your sprite. So you’ll need to do something else entirely.

(Note: Manipulating the rotation’s X and Y never would have worked. One, rotation.x and rotation.y are not the values you see in the inspector because they’re quaternion’s X and Y rather than Euler angles’s x and y. But more importantly, even if you modified rotation.eulerAngles.x/y, Euler angles just don’t work that way - separating the X, Y, and Z axes of a Euler angle is generally going to give you nonsensical results.)

The “something else” is the cryptically named Atan2, which gives you a single number for the rotation on a single plane, which is the critical concept. (Note, it returns its answer in radians, while most Unity code uses degrees, so a conversion is needed.)

float targetAngle = Mathf.Atan2(lookat.y - transform.position.y, lookat.x - transform.position.x;
transform.rotation = Quaternion.AngleAxis(targetAngle * Mathf.Rad2Deg, Vector3.forward);

This code will work if your sprite faces to its right; you can offset the first parameter in AngleAxis by 90, 180, or 270 if your sprite’s “forward” is a different direction.

the explanation of Euler angles is great and thank you for answering so quickly, the code works great, what would be the best way to speed the rotation up, it takes a very long time to rotate to face the player if say the player runs underneath the sprite takes a good 30 seconds to rotate and face the player

and would i be better to use the rigidbody2d.rotation to rotate rather than transform as im moving it using the RB, read somewhere that moving using a rigidbody and trying to access the transform component can sometimes screw stuff up, might be wrong though