Weird 2D Mouse Look At Problem

Okay, so yesterday I decided to start making a 2D top-down shooter. So I started with the main mechanic; getting the player to aim at the mouse, but came across this really strange problem where the player would not fully rotate. The player has a Recttransform component, and is a child of a ‘Screen space - overlay’ canvas. I have tried various options for rotating the player, but opted for the following:

 Vector3 dir = Input.mousePosition - cam.WorldToScreenPoint(transform.position).normalized;
 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

I recorded a video showing the problem:

Any help would be appreciated!

The main problem is that you are normalizing the screen-point vector. Though, I’d recommend working in game-coordinates (as opposed to screen-coordinates) as you should be able to make a point you can simply rotate to (or set as your forward, in this case).

But, using screen coordinates, something like this, perhaps:

    public static Vector2 FaceMouse(Camera cam, Transform player)
        player.forward = Input.mousePosition - cam.WorldToScreenPoint(player.position);

Or just use that as a base reference and go from there.

Yay! I finally got it working. If I set the canvas’s render mode to ‘Screen space - Camera’, it works perfectly, well nearly. (It rotates it by 90 degrees, but I was able to easily fix that.)