Discrepancies between rotation calculated by script and shown by inspector

Hi everybody,

I’ve been a quiet reader within this community for a while now but now it seems that I’m facing an issue that can not be solved by reading other threads and I hope you can help me with it… :slight_smile:

In my scene I’m using a configurable joint to rotate a gameobject that also contains a rigidbody around one axis by user input. To do so I increment or decrement a variable according to the user’s input and use this variable afterwards to set the joints target rotation. When there’s no more input that is meant to drive the joint it is locked again. I was able to prevent the problem (mentioned in other threads) of the joint snapping back to its original position when it gets locked by actualization of its “axis” and “secondary axis” property before finally locking it. To conclude: What I described works fine for me! Next I wanted to implement a variable that tells me how much degree the joint (the gameobject carrying the joint) has rotated totally. To do so I calculate a “delta Rotation” based on the rotation of the gameobject in the current and last update. The code I described so far looks like this…

    void Update ()
    {
        if (Input.GetAxisRaw ("Horizontal") != 0)
        {
            rigidbody.WakeUp ();
            joint.angularXMotion = ConfigurableJointMotion.Free;
            joint.projectionAngle = 180f;

            if (Input.GetAxisRaw ("Horizontal") > 0)
            {
                incTargetRotation += 0.2f;
                joint.targetRotation = Quaternion.Euler (incTargetRotation, 0f, 0f);
            }
            if (Input.GetAxisRaw ("Horizontal") < 0)
            {
                incTargetRotation -= 0.2f;
                joint.targetRotation = Quaternion.Euler (incTargetRotation, 0f, 0f);
            }
        }
        else
        {
            joint.axis = joint.axis;
            joint.secondaryAxis = joint.secondaryAxis;
            joint.angularXMotion = ConfigurableJointMotion.Locked;
            joint.projectionAngle = 0f;
            incTargetRotation = 0f;
        }

        deltaRotation = Mathf.DeltaAngle (oldRotation, joint.transform.localEulerAngles.y);
        totalRotation += deltaRotation;
        oldRotation = joint.transform.localEulerAngles.y;
   }

And here comes the Problem: When I check the value of “totalRotation” against that what’s shown in the inspector within comparable thresholds I always notice small discrepancies. For example: totalRotation saying -28.00153 when there’s -27.937 shown in the inspector. This is giving me a headache as I can’t figure out what the matter of those differences is or how I could prevent them. I would really appreciate any hint from you guys. :wink:

Best regards,
Mathias

I’m not sure what happens as I don’t see where inspector values come from.
Can it be floating point error? After all, euler to quaternion can lose precision, then each rotation you make can lose precision and in the end you convert quaternion to euler, losing even more precision…

Here’s a screenshot of the scene that might help to understand my problem better… :slight_smile:


Don’t consider the possible inaccuracy caused by line 30 to 32 of the code posted above. For test purpose I’m just printing joint.transform.localEulerAngles.y every update as I thought this is what’s shown in the inspector. As you can see in the picture I obviously was wrong as there’s a difference between the values…:eyes: I’m wondering what’s the inspector-values’ equivalent in code then?

Regards,
Mathias