Unity官方案例内的方法,写在这,方便后续查看。
private Quaternion ClampRotationAroundXAxis(Quaternion q)
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
angleX = Mathf.Clamp(angleX, minRotX, maxRotX);
q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);
return q;
}
例子:
m_Transform.localRotation = ClampRotationAroundXAxis(m_Transform.localRotation);

本文提供了Unity中限制物体绕X轴旋转角度的实用代码片段。通过调整Quaternion来确保物体的旋转不会超出预设的角度范围,适用于游戏开发中精确控制物体旋转场景。
1192

被折叠的 条评论
为什么被折叠?



