I need to calculate the angle between the current mouse world position, and an object sitting on a plane.
I’m currently generating the world mouse position, and distance from the camera using:
Ray ray = Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
currentMousePosition = hit.point;
currentMouseDistance = hit.distance;
}
I’m then using Debug.DrawLine()
to draw a line between the points, and this is working fine. However, I can’t seem to determine the angle.
Vector3 direction = new Vector3(currentMousePosition.x, currentMousePosition.y, currentMouseDistance);
Debug.DrawLine(currentMousePosition, targetObject, Color.red, 5, false);
I’ve tried Mathf.Atan2()
which is suggested everywhere, but when I use it the angle produced seems to range from 30-60 degrees, regardless of where the mouse cursor is placed.
I’m not sure why this isn’t working, so any advice or input would be greatly appreciated.
Thanks