无法钳制我的相机旋转值

Not able to Clamp my camera rotation value

我制作了一个围绕物体旋转的相机...一切正常。但我无法夹住或限制相机旋转。这是代码..

//First - Get the Initial Position
        if (Input.GetMouseButtonDown(0))
        {
            mPreviousPosition = mCamRef.ScreenToViewportPoint(Input.mousePosition);
        }

        //Second - the difference amount and change in x
        if (Input.GetMouseButton(0))
        {
            Vector3 newPosition = mCamRef.ScreenToViewportPoint(Input.mousePosition);
            Vector3 direction = mPreviousPosition - newPosition;
            
            float rotationAroundYAxis = -direction.x * 180;
            mCamRef.transform.position = mTargetToRotateAround.position;
            rotationAroundYAxis = Mathf.Clamp(rotationAroundYAxis, -60f,60f);
            
            mCamRef.transform.rotation = Quaternion.Euler(Vector3.up * rotationAroundYAxis);

            mCamRef.transform.Translate(new Vector3(mDistanceToTarget.x, mDistanceToTarget.y, -mDistanceToTarget.z));
            mPreviousPosition = newPosition;
        }

你在夹紧

rotationAroundYAxis = Mathf.Clamp(rotationAroundYAxis, -0.6f, 0.6f);

然后将它用于 Transform.Rotate,它从当前旋转角度围绕给定的量旋转 => 你总是旋转一些东西。

您可能更愿意使用例如Quaternion.Euler

mCamRef.transform.rotation = Quaternion.Euler(Vector3.up * rotationAroundYAxis);
       

但是,请注意,使用 +- 0.6° 限制旋转几乎没有任何意义...

根据您的评论,您希望夹紧到 +-60°,所以最好使用

rotationAroundYAxis = Mathf.Clamp(rotationAroundYAxis, -60, 60);

虽然我仍然不明白你想如何从方向向量获得角度旋转...