'How to limit angles in (-180,180) range just like Unity3D inspector

I want to get rotation of Camera gameobject attached to Char gameobject

EDIT: Char rotation, pos, scale is 0,0,0

![enter image description here

I have following code: (Script is on the Char gameobject)

Transform cameraT = transform.Find("Camera").transform;
print(cameraT.localEulerAngles);

The problem is that in inspector it shows: transform ss

but in debug console:

enter image description here

I want to get rotation from inspector



Solution 1:[1]

-45 and 315 are same rotations. Inspector just shows it differently.

I want to get rotation from inspector

use

angle %=360;
angle= angle>180 ? angle-360 : angle;

to limit it from -180 to 180 that's what inspector displays.

Hope this helps

Solution 2:[2]

That`s because you look at Global or Local Camera position.

Check it near the Left-Top corner, near the "Pivot" button

Solution 3:[3]

To provide a correct answer that was requested by OP which would provide values between (-180, 180] we need to slightly modify the code provided by Umari M

float RoundAngle(float angle)
{
    angle %= 360;
    return  angle > 180 ? angle - 360 : angle < -180 ? angle + 360 : angle;
}

Readable version with comments

float RoundAngle2(float angle)
{
    // Make sure that we get value between (-360, 360], we cannot use here module of 180 and call it a day, because we would get wrong values
    angle %= 360;
    if (angle > 180)
    {
        // If we get number above 180 we need to move the value around to get negative between (-180, 0]
        return angle - 360;
    }
    else if (angle < -180)
    {
        // If we get a number below -180 we need to move the value around to get positive between (0, 180]
        return angle + 360;
    }
    else
    {
        // We are between (-180, 180) so we just return the value
        return angle;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Richard Tyshchenko
Solution 3 Tomasz Juszczak