繁体   English   中英

如何限制 Y 轴上的旋转,以便玩家无法在 Unity 中连续旋转相机

[英]How can I limit the rotation on the Y axis so that the player can't continuously spin the camera in Unity

我有一个即将到来的项目,我必须在星期一展示,这是我必须解决的最后一个错误。 如果有人可以帮助我,并且可以教我如何应用轴限制器,那就太好了,在此先感谢大家。

问题是相机可以旋转360度

下面是我当前控制相机的代码

public float sensitivity = 30.0f;
private GameObject cam;
float rotX, rotY;

private void Start()
{
    sensitivity = sensitivity * 1.5f;
    Cursor.visible = false; // For convenience
}

private void Update()
{
    rotX = Input.GetAxis("Mouse X") * sensitivity;
    rotY = Input.GetAxis("Mouse Y") * sensitivity;
    //Apply rotations
    CameraRotation(cam, rotX, rotY);
}

private void CameraRotation(GameObject cam, float rotX, float rotY)
{
    //Rotate the player horizontally
    transform.Rotate(0, rotX * Time.deltaTime, 0);
    //Rotate the players view vertically
    cam.transform.Rotate(-rotY * Time.deltaTime, 0, 0);
}

对此添加逻辑钳位使其通读起来更加清晰,此方法将垂直旋转钳位在 60 到 -60 度之间,如果需要,注释应该有助于修改它

这次我最终跳入 Unity 进行测试,而不是从我的头顶

void CameraRotation(GameObject cam, float rotX, float rotY)
{
    //Rotate the player horizontally
    transform.Rotate(0, rotX * Time.deltaTime, 0);
    //Rotate the players view vertically
    cam.transform.Rotate(-rotY * Time.deltaTime, 0, 0);

    //Grab current rotation in degrees
    Vector3 currentRot = cam.transform.localRotation.eulerAngles;
    //If (x-axis > (degreesUp*2) && x-axis < (360-degreesUp))
    if (currentRot.x > 120 && currentRot.x < 300) currentRot.x = 300;
    //else if (x-axis < (degreesDown*2) && x-axis > (degreesDown))
    else if (currentRot.x < 120 && currentRot.x > 60) currentRot.x = 60;
    //Set clamped rotation
    cam.transform.localRotation = Quaternion.Euler(currentRot);
}

祝项目顺利

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM