How does quaternion clamping work? Everytime I have looked for an answer online, I usually just come across an answer from pre-ECS/MonoBehaviour only solutions which tell you to convert the angle to an euler angle, clamp the axis you want and convert it back. But ECS quaternions don’t have that feature.
I have a PlayerRotate component that looks like this:
public struct PlayerRotate : IComponentData {
public float RotationSpeed;
public float RotationValue;
public float UpperClamp;
public float LowerClamp;
}
RotationSpeed is how fast the camera rotates, RotationValue is the current user input and then UpperClamp and LowerClamp are the angles in degrees of where the pitch should be clamped.
The only solution I found online that I thought would work was here and I tried to adapt it to use the Unity.Transform version but that didn’t do anything except lock off all pitch rotation. My adaptation:
private void Execute(ref LocalTransform pcTransform, in Components.PlayerRotate playerRotate, Tags.CameraPivot pivot) {
quaternion q = pcTransform.RotateX(playerRotate.RotationSpeed *
playerRotate.RotationValue *
DeltaTime).Rotation;
q.value.x /= q.value.w;
q.value.y /= q.value.w;
q.value.z /= q.value.w;
q.value.w = 1.0f;
float angleX = 2.0f * (360 / (math.PI * 2)) * math.atan(q.value.x);
angleX = math.clamp(angleX,
playerRotate.LowerClamp,
playerRotate.UpperClamp);
q.value.x = math.tan(0.5f * ((math.PI * 2) / 360) * angleX);
pcTransform.Rotation = q;
}
I’d appreciate any effort on the topic since I assume that angle clamping is not gonna be a somewhat common event that I have to handle.
Thanks in advanced