我的場景中有一個對象可以通過鼠標滑動旋轉 ( 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 關注
- 192 瀏覽
添加回答
舉報
0/150
提交
取消