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.
This is my current code.
void StackFunction()
{
float turretRotation;
float bodyRotation;
turretRotation = this.transform.eulerAngles.y;
bodyRotation = playerBody.transform.eulerAngles.y;
if (turretRotation > 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);
}
This is my current code. It works fine when the body is facing 0, however it all inverts when the body swaps to 180.