我有这个代码,它允许相机通过触摸屏的输入进行旋转,我需要将旋转锁定在特定范围内。我试着用";数学夹具";没有成功。有人能帮我吗?
我试图输入这个字符串,但视觉得到一条错误消息,上面写着">无法从'UnityEngine转换。四元数"到"float">";
rotationY = Mathf.Clamp(rotationY, -30, 30);
我把允许相机无限制运行的代码留在这里。感谢大家对的帮助
private Touch touch;
private Vector2 touchPosition;
private Quaternion rotationY;
private float rotateSpeedModifier = 0.01f;
//public float yPos;
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Moved)
{
rotationY = Quaternion.Euler(0f, -touch.deltaPosition.x * rotateSpeedModifier, 0f);
transform.rotation = rotationY * transform.rotation;
Debug.Log(rotationY * transform.rotation);
}
}
}
不能夹紧Quaternion
。
然而,您可以存储您已经旋转的角度的浮动,并将其夹紧:
private Touch touch;
private Vector2 touchPosition;
private float rotationY;
private float rotateSpeedModifier = 0.01f;
private Quaternion originalRotation;
private void Start ()
{
originalRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Moved)
{
rotationY -= touch.deltaPosition.x * rotateSpeedModifier;
rotationY = Mathf.Clamp(rotationY, -30, 30);
transform.rotation = originalRotation * Quaternion.Euler(0, rotationY, 0);
}
}
}