Why are all of my degree calculations off by 180?

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.

(8:14 if the timecode link doesn’t work) maybe?

Ooh that’s really useful, thank you! It’s clunky, but ended up using his exact method and just subtracting to move the circle:

float value = (float)(((System.Math.Atan2(x, y) / System.Math.PI) * 180f)-180);

Use Unity’s math functions, rather than the .NET implementations – and your Atan2 parameters are backwards, it’s y,x not x,y:

float value = Mathf.Atan2(y, x) * Mathf.Rad2Deg;
1 Like

GrozzleR: Atan2(x,y) is a hack to fix the start+direction mismatch between real math angles to Unity/layman angles.

It converts Atan’s proper “0=east, CCW” angles into Unity/layman’s angles “0=North, CW.” Without it, you have to use something like a=-(a-90). It seems truly bizarre to me that it works out, but try it - it does, leaving just the radians to degrees conversion.

In this case, I doubt it was on purpose. It’s likely a “2 wrongs make a right” situation.