Last Updated: November 19, 2020
·
4.68K
· badawe

Normalize Angles for Quaternion to Euler

Sometimes when you work with EulerAngles some rotations can miss because unity do not block the rotation at 360, this are two simple extensions to get the normalized angles and use that insted:

public static Vector3 NormalizeAngles (this Vector3 angles)
    {
        angles.x = NormalizeAngle (angles.x);
        angles.y = NormalizeAngle (angles.y);
        angles.z = NormalizeAngle (angles.z);
        return angles;
    }

    public static float NormalizeAngle (this float angle)
    {
        while (angle>360)
            angle -= 360;
        while (angle<0)
            angle += 360;
        return angle;
    }

Related protips:

Improved PlayerPrefs for Unity