【问题标题】:How to clamp Camera in Unity3D?如何在 Unity3D 中夹紧相机?
【发布时间】:2019-04-26 21:58:39
【问题描述】:

我试过这个,但是每次按下显示屏时,相机都会从这个点开始。
所以我现在不知道该怎么办

我试过用夹子做一些,但是出错了

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);
}

【问题讨论】:

  • 你能解释一下你想要什么吗?!夹相机是什么意思?
  • 我的意思是让相机只旋转90度

标签: c# unity3d


【解决方案1】:

我很傻 要修复水平旋转,只需移除 yClamp))) 非常感谢 所以 rotX = Mathf.Clamp(rotX, -90f, 90f);实际上有效 我只是愚蠢,我在没有 - 运算符的情况下成功了)

【讨论】:

    【解决方案2】:

    首先是一个名为 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);
    

    【讨论】:

    • 不,我知道如何在 PC 上做到这一点我不知道如何在我的 Android 代码中做到这一点
    • 然后将 Input.GetAxis("Mouse X") 替换为 Input.GetTouch(0).position.x 并将 Input.GetAxis("Mouse Y") 替换为 Input.GetTouch(0).position。是的
    • 不,它不适用于手机我给了你我的代码,看看它没有用((
    • 我已经完成了,谢谢我做了 xAngle = Mathf.Clamp(xAngle, -70f, 70f); yAngle = Mathf.Clamp(yAngle, -70f, 70f);没有“-”运算符
    • 试试这个:用 Input.touches[0].deltaPosition.x 替换 Input.GetAxis("Mouse X");和 Input.GetAxis("Mouse Y") 与 Input.touches[0].deltaPosition.y;
    猜你喜欢
    • 1970-01-01
    • 2022-10-15
    • 2021-06-08
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多