localEulerAngles.x changes when the angle is close to -90 in the inspector

I need to read the angle of the knee joint but the problem is the (eulerAngX) value is not a linear relationship with the X angle in the inspector? I think the (eulerAngX) will change depending on how close it is to -90 in the inspector. (see the two attached pictures)
I need to read the angle of the knee joint to use in a robotic application you can see the YouTube video here to understand why I need to know the angle

I can’t seem to get my little brain to figure this problem out. Thanks in advance for your help.

public class HelpMe : MonoBehaviour
{
public float eulerAngX;

// Update is called once per frame
void Update()
{
    eulerAngX = transform.localEulerAngles.x;

    Debug.Log(eulerAngX);
}

Well this issue has several aspects to it. First of all when the x rotation of the eulerangles reaches ±90° they are in a gimbal lock position and you loose one degree of freedom. This is an inherent problem of euler angles in general. The next problem is that euler angles are not unique. There are always more than one way to represent the same orientation. For example (45, 0, 0) is the same rotation as (180, 180, 135). Finally Unity does not work with euler angles but uses unit quaterions. Quaternions do not suffer from gimbal lock and allow a smooth transition between any two orientations. When you read eulerAngles / localEulerAngles Unity actually calculates the 3 angles from the quaternion representation. Since euler angles are not unique you can’t be sure which one you get depending on the actual orientation.

For example in the gimbal lock orientation you loose one degree of freedom. For example if you rotate an object to (90,0,0) your object points straight up. If you want to rotate the object now to the left or right side (world z axis) there is no gimbal that allows this motion. In order to solve that issue the y and z angle would have to change immediately by 90° to provide the proper rotation axis.

If you have difficulties understanding gimbal lock, this video does a decent job explaining the issue in general.

To get the angle between two limb parts I would recommend to use the two direction vectors of your joints and just use Vector3.Angle() to calculate the angle between the two vectors. This makes it independent from the whole euler angles issue.

Note that when modifying the local euler angles in the inspector while in edit mode, the Unity editor actually stores the 3 euler angles internally which makes it easier to manipulate them through the handles in the scene view. You will notice that they can actually rotate beyond 360° which makes no sense under runtime conditions. Angles in general are a pain to work with since they wrap around either at 0° / 360° or at -180° / 180°

Quaternions on the other hand represent an orientation through a direction vector and the rotation around that vector. If you have never heard about quaternions before I always recommend this Numberphile video. Quaternions are quite tricky to understand since they represent a 4 dimensional number system. However they are just beautiful from a mathematical and practical point of view. If you want to know a little more about quaterions I can also recommend the 3B1B videos on quaterions

Thank you for getting back to me
I’m having problems getting the angle to read the same as the inspector. See the two attached pictures. The 2 pictures should show the same angle because they are the same angle, but they show different numbers that don’t mach up to what is in the inspector angle for X.
thanks in advance for your help.

public class HelpMe : MonoBehaviour

{
public float angle;
public Transform getTransform;

void Update()
{
    angle = Vector3.Angle(getTransform.position, transform.position);

    Debug.Log(angle);
}

}