#Why does clamping the X axis reset the Y axis?

1 messages · Page 1 of 1 (latest)

opal bluff
#

I have a job that rotates a pivot object around the X axis and clamps it between -90 degrees and 90 degrees (converted to radians during bake). But when clamping the X axis it also locks the Y axis to 0 degrees and I can't figure out why. Any help to figure out what's going on here would be appreciated.

The job/component in question:

public class CameraPivotAuthoring : MonoBehaviour {
        [SerializeField] private float _rotationSpeed = 10;
        [SerializeField] private float _upperClamp = 10;
        [SerializeField] private float _lowerClamp = 10;

        
        private class CameraPivotAuthoringBaker : Baker<CameraPivotAuthoring> {
            public override void Bake(CameraPivotAuthoring authoring) {
                Entity entity = GetEntity(TransformUsageFlags.Dynamic);
                AddComponent(entity, new Tags.CameraPivot());
                AddComponent(entity, new CameraPivotRotate {
                    RotationSpeed = authoring._rotationSpeed,
                    UpperClamp = math.radians(authoring._upperClamp),
                    LowerClamp = math.radians(authoring._lowerClamp),
                });
            }
        }
    }
    
    public struct CameraPivotRotate : IComponentData {
        public float RotationSpeed;
        public float RotationValue;
        public float UpperClamp;
        public float LowerClamp;
}

private void Execute(ref LocalTransform transform, in Components.CameraPivotRotate cameraPivotRotate, Tags.CameraPivot pivot) {
    quaternion q = transform.RotateX(cameraPivotRotate.RotationSpeed * cameraPivotRotate.RotationValue * DeltaTime).Rotation;

    float t0 = 2.0f * (q.value.w * q.value.x + q.value.y * q.value.z);
    float t1 = 1.0f - 2.0f * (q.value.x * q.value.x + q.value.y * q.value.y);
    float X = math.atan2(t0, t1);
    X = math.clamp(X, cameraPivotRotate.LowerClamp, cameraPivotRotate.UpperClamp);

    float t2 = 2.0f * (q.value.w * q.value.y - q.value.z * q.value.x);
    t2 = math.clamp(t2, -1.0f, 1.0f);
    float Y = math.asin(t2);

    float t3 = 2.0f * (q.value.w * q.value.z + q.value.x * q.value.y);
    float t4 = 1.0f - 2.0f * (q.value.y * q.value.y + q.value.z * q.value.z);
    float Z = math.atan2(t3, t4);
    quaternion newQ = quaternion.Euler(X, Y, Z, math.RotationOrder.XYZ);

    transform.Rotation = newQ;
}```