I’m trying to write a function that, when the player clicks on a circular compass sprite, returns the position they clicked in degrees relative to the center of the compass. My math looks right to me, but when I try this out it marks due north (positive Y) as 180 instead of 0, and due south (negative Y) as 0/360:
public void OnPointerClick(PointerEventData eventData)
{
desiredHeading = DegreePositionOfClick(eventData.pressPosition);
}
float DegreePositionOfClick(Vector2 clickPosition) //returns the degree position of the click with reference to the sprite's center
{
float originX = transform.position.x; //Sprite X/Y
float originY = transform.position.y;
float targetX = clickPosition.x; //Coords from click event
float targetY = clickPosition.y;
float degreePosition = FindDegree(originX - targetX, originY - targetY);
return degreePosition;
}
float FindDegree(float x, float y) //Convert [x,y] coordinates to degrees
{
float value = (float)((System.Math.Atan2(x, y) / System.Math.PI) * 180f);
if (value < 0) value += 360f;
return value;
}
My assumption is that my eyes are skipping over an obvious klutz-up in FindDegree, but I keep wondering if it isn’t some weird issue with a screen coord vs. world coord conversion occurring somewhere I’m missing.