Pointing object at mouse using raycast only works when object is center screen.

Edit 2: Solved this by going a different route, see below.

Edit: Just wanted to note that this is for a 2D rotation

I have an object center screen and a plane to raycast to in order to get the mouse
position in world space so that I can point the object towards the mouse.
This works fine unless the object is not at the center of the screen.

The is object acting as if it’s aiming at the mouse
from the center of the screen as opposed to where it actually is and I have no idea why
unless it has something to do with ScreenPointToRay.

I have a video of this happening here (youtube) in case I’m not explaining it very well

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        Vector3 CursorPos = Vector3.zero;

        GameObject plane = GameObject.FindGameObjectWithTag("RaycastPlane");

        if (plane.collider.Raycast(ray,out hit, 50f))
        {
            CursorPos = ray.GetPoint(50f);
        }


        float x = CursorPos.y - transform.position.x;
        float y = CursorPos.x - transform.position.y;

        float angle = Mathf.Atan2(x, y);
        angle *= Mathf.Rad2Deg;
        transform.localEulerAngles = new Vector3(transform.rotation.x, transform.rotation.y, angle + 90);

Anyone have an idea of what is causing this?

It looks like the object is rotating in local space instead of rotating to face the position of the mouse on a plane in world space.

To fix this I would create a vector that points at the plane location. Then simply set the forward vector of the transform.

 Vector3 f = planePos - transform.position;
 f.Normalize();
 transform.forward = f;

I was reading about another user doing the same kind of rotation I was wanting and they did it a bit different. Instead of using a ray they went used WorldToScreenPoint and worked from that. This seems to be working for me so if anyone else has an issue like I had you can do use this also:

        Vector3 mouse_pos = Input.mousePosition;
        mouse_pos.z = 10f;
        Vector3 object_pos = Camera.main.WorldToScreenPoint(transform.position);
        mouse_pos.x = mouse_pos.x - object_pos.x;
        mouse_pos.y = mouse_pos.y - object_pos.y;

        float angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;

        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + 90));