I new to unity / c# and need some help with the rotation of my player. While I hold the mouse cursor on the positive y-axis (if the middle of the player is 0) the player faces slightly above the cursor and while on the negative y-axis the player faces slightly below the cursor. Here is a gif to demonstrate:
https://gyazo.com/e417962c20e186f3c6419c23bf8263f6
Here is my code for rotation.
public class LookTowardMouse : MonoBehaviour
{
void Update()
{
//Get the Screen positions of the object
Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
//Get the Screen position of the mouse
Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
//Get the angle between the points
float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
//Rotate player
Debug.Log(angle);
transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
}
float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
{
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
}
}