title: Unity - 2D Aim at Mouse, while restricting min/max rotation and factoring in parent rotation
body: <p>So, I have a ship with a &quot;Hardpoint&quot; attached to it that weapons can be placed on to. Once placed, they aim at the mouse position and have a restricted rotational range:
  <a href="https://i.stack.imgur.com/4JkRQ.png" rel="nofollow noreferrer">See here</a></p>
  <p>The rotational range is based off minAngle and maxAngle, and are calculated by the eulerAngles.z of the parent hardpoint slot, plus/minus the angle modifier (in this case, 20). I had to do some weird rotational voodoo (see OneEightyToThreeSixty() in the code snippet) to get the mouse look angle to match the same format as the minAngle/maxAngle.</p>
  <blockquote>
  <p>angle was coming in as -180 -&gt; 180 degrees instead 0 -&gt; 360 like the hardpoint's rotation. Wrote a method to convert it to 360 during runtime.</p>
  </blockquote>
  <p>I've rewritten this script more times than I can count, but ultimately <a href="https://gfycat.com/unlawfulwellgroomedgermanshepherd" rel="nofollow noreferrer">I'm hit with this issue.</a></p>
  <p>As you can see, for 270 degrees of the 360 rotation, the aiming and restrictions work totally fine. But at the top right quarter of the rotation, it all goes haywire.</p>
  <p>I've tried so many different implementations but they all seem to have this consistent issue. I cannot figure out what I'm missing. Here's the code:</p>
  <pre><code>public class HardpointSlot : MonoBehaviour
  {
      public HardpointController ship;
      public GameObject hardpoint;
  
      public float minAngle;
      public float maxAngle;
      public float angle;
      public float offsetAngle;
      public bool switched;
  
      void Update()
      {
          AimAtMouse();
      }
  
      public void AimAtMouse()
      {
          Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
  
          angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  
          minAngle = (transform.eulerAngles.z - 20) + 90f;
          maxAngle = (transform.eulerAngles.z + 20) + 90f;
  
          angle = OneEightyToThreeSixty(angle);
          if (angle &lt; minAngle)
          {
              angle = minAngle;
          }
          else if (angle &gt; maxAngle)
          {
              angle = maxAngle;
          }
  
          hardpoint.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
      }
  
      public float OneEightyToThreeSixty(float angle)
      {
          float newAngle = 0f;
  
          if (angle &gt;= 0 &amp;&amp; angle &lt;= 180)
          {
              return angle;
          } else
          {
              return newAngle = 360 + angle;
          }
  
  
      }
  }
  </code></pre>
  <p>So quick recap:</p>
  <ul>
  <li>Hardpoint needs to look at mouse but only if its within its aim range bounds, which is calculated by the baseAim offset (20) and the rotation of the parent hardpoint slot.</li>
  <li>Works for 270 degrees of rotation but not for the last 90.</li>
  </ul>
  <p>Any ideas? Am I missing something very obvious?</p>