如何在Unity3D中夹紧Camera?

时间:2019-04-26 12:33:13

标签: c# unity3d

我已经尝试过了,但是每次按下显示器时,相机都从这一点开始。
所以我不知道现在该怎么办

我尝试用钳夹做一些事情,但都出错了

if(Input.GetTouch(0).phase == TouchPhase.Began ) 
{
    firstpoint = Input.GetTouch(0).position;
    ???xAngTemp = Mathf.Clamp(xAngle, 10, 10);
    ???yAngTemp = Mathf.Clamp (yAngle, 10, 10);
    xAngTemp = xAngle;
    yAngTemp = yAngle;
}

if(Input.GetTouch(0).phase==TouchPhase.Moved) 
{
    secondpoint = Input.GetTouch(0).position;
    ???xAngTemp = Mathf.Clamp(xAngle, 10, 10);
    ???yAngTemp = Mathf.Clamp (yAngle, 10, 10);

    //Mainly, about rotate camera. For example, for Screen.width rotate on 180 degree
    xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 30 * XSensitivity / Screen.width;
    yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 15 * YSensitivity / Screen.height;

    //Rotate camera
    this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
}

2 个答案:

答案 0 :(得分:0)

首先是一个称为RotSpeed的速度变量:

float RotSpeed = 1; 

然后按如下所示修改您的更新功能:

rotX += Input.touches[0].deltaPosition.x * RotSpeed;
rotY += Input.touches[0].deltaPosition.y * RotSpeed;
rotX = Mathf.Clamp(rotX, -90f, 90f);
rotY = Mathf.Clamp(rotY, -90f, 90f);
Camera.main.transform.Rotate(Vector3.up, rotX * 5, Space.Self);
Camera.main.transform.Rotate(Vector3.left, rotY * 5, Space.Self);

答案 1 :(得分:0)

我很笨 要固定水平旋转,只需移除yClamp))) 非常感谢 所以rotX = Mathf.Clamp(rotX,-90f,90f);实际有效 我只是愚蠢,我在没有-运算符的情况下就做到了)