title: How to Lerp a rotation the shortest path
body: <p>I have a tank, which is made of a base and turret on top, both separate but under a parent object. To shoot, the turret will rotate to the shooting direction, then lerp back towards the tanks base rotation. 
  I want to rotate the turret in the shortest possible direction. So if the base is at 0 degrees and the turret is at 270, I change the 270 to -90 and it will lerp from -90 to 0, however if the base is at 180 and the turret is at 270, it needs to lerp from 270 to 180, not from -90.</p>
  
  <p>This is my current code. </p>
  
  <pre><code>    void StackFunction()
      {
          float turretRotation;
          float bodyRotation; 
  
          turretRotation = this.transform.eulerAngles.y;
          bodyRotation = playerBody.transform.eulerAngles.y; 
  
          if (turretRotation &gt; 180)
          {
              turretRotation -= 360; 
          }
  
          float targetRotation = Mathf.Lerp(turretRotation, bodyRotation, lerpRate);
  
          this.gameObject.transform.eulerAngles = new Vector3(this.gameObject.transform.eulerAngles.x, targetRotation, this.gameObject.transform.eulerAngles.z);
      }
  </code></pre>
  
  <p>This is my current code. It works fine when the body is facing 0, however it all inverts when the body swaps to 180.</p>