限制相机旋转角度 [英] Limit camera rotation angle

查看:99
本文介绍了限制相机旋转角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够限制摄像机在特定点之后旋转,并且只能在特定区域旋转,这是到目前为止的代码...

I want to able to restrict camera rotation after a certain point and only be able to rotate in a certain area, this is the code so far...

void Update() {
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = -Input.GetAxis("Mouse Y");

    rotY += mouseX * mouseSensitivity * Time.deltaTime;
    rotX += mouseY * mouseSensitivity * Time.deltaTime;

    desiredy = Camera.main.transform.eulerAngles.y;
    desiredx = Camera.main.transform.eulerAngles.x;

    if (!(desiredy < maxy && desiredy > miny))
    {
        //i dont know what to put in here, i have tried everything
    }


    else if (!(desiredx < maxx && desiredx > minx))
    {
         //i dont know what to put in here, i have tried everything
    }

    else
    {
        localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
        transform.rotation = localRotation;
    }
}

推荐答案

由于不必要的逻辑,这使情况变得复杂.您只需要Mathf.Clamp函数即可确保您的值在指定范围内.

You are making this complicated with your unnecessary logic. All you need is the Mathf.Clamp function that makes sure that your value is within specified range.

void Update()
{
    rotateCamera();
}


public float xSensitivity = 100.0f;
public float ySensitivity = 100.0f;

public float yMinLimit = -45.0f;
public float yMaxLimit = 45.0f;

public float xMinLimit = -360.0f;
public float xMaxLimit = 360.0f;

float yRot = 0.0f;
float xRot = 0.0f;

void rotateCamera()
{
    xRot += Input.GetAxis("Mouse X") * xSensitivity * Time.deltaTime;
    yRot += Input.GetAxis("Mouse Y") * ySensitivity * Time.deltaTime;
    yRot = Mathf.Clamp(yRot, yMinLimit, yMaxLimit);
    Camera.main.transform.localEulerAngles = new Vector3(-yRot, xRot, 0);

}

这篇关于限制相机旋转角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆