我的场景中有一个对象可以通过鼠标滑动旋转 ( RotateAround )。我想给对象一些旋转限制,例如 X 轴的 -45 和 45 度,所以当它的旋转变成 45 度时,它不能超过它。所以我在我的脚本中尝试了 Mathf.Clamp方法,如下所示,它在 Y 轴上工作正常,对象围绕他的 X 轴旋转并且没有超出 Y 限制。但在 X 轴上,当物体的 Y 旋转达到 O 时,它会立即变为 30 度,并出现奇怪的旋转!你能告诉我的代码有什么问题吗?旋转脚本:float sensitivity = 10f;Vector3 firstPressPos;Vector3 secondPressPos;float minRotationX = 45;float maxRotationX = 100;float minRotationY = 30;float maxRotationY = 30;void Update () { if (Input.GetMouseButtonDown(0)) { //save began touch 2d point firstPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y); } if (Input.GetMouseButton(0)) { //save ended touch 2d point secondPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y); if (firstPressPos != secondPressPos) { float RotX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime; float RotY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime; transform.RotateAround(Vector3.up, RotX); transform.RotateAround(Vector3.right, -RotY); Vector3 angles = transform.eulerAngles; angles.x = Mathf.Clamp(angles.x, minRotationX, maxRotationX); angles.y = Mathf.Clamp(angles.y, -minRotationY, maxRotationY); angles.z = 0; transform.eulerAngles = angles; } }}
1 回答

慕慕森
TA贡献1856条经验 获得超17个赞
在编辑器中,旋转值介于 -180 和 180 之间,但在 transform.eulerAngles 中,它们实际上介于 0 和 360 之间。
所以你需要在夹紧它之前调整你的角度值。
if(angles.y > 180)
{
angles.y -= 180f;
}
angles.y = Mathf.Clamp(angles.Y, minY, maxY);
- 1 回答
- 0 关注
- 200 浏览
添加回答
举报
0/150
提交
取消