Question by
markherdt1992 · Jul 01, 2020 at 06:26 PM ·
unity 5rotationmouseposition
3D Look at Mouse relative to Camera direction
Hi,
im trying to make my players y rotation rotate towards the mouse position. The rotation part works, but only at a certain camera angle, when i rotate the camera behind the player, the player doesn't look at the mouse anymore.
https://imgur.com/tD23naf https://imgur.com/IFVDVE7
Here's what i've gto so far
Vector3 mousePos = Input.mousePosition;
Vector3 playerPos = playerCamera.WorldToScreenPoint(transform.position);
Vector3 direction = mousePos - playerPos;
float angle = -((Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg) - 90);
transform.rotation = Quaternion.AngleAxis(angle, transform.up);
Comment