我的场景中有一个对象可以通过鼠标滑动旋转 ( 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;
}
}
}
慕慕森
相关分类