C# 使用Mathf.Clamp可以使我的相机以允许的最小角度启动

C# 使用Mathf.Clamp可以使我的相机以允许的最小角度启动,c#,unity3d,math-functions,C#,Unity3d,Math Functions,对Unity和C#有些陌生,尝试让相机四处移动,让用户看到房间。这有点像一部视觉小说,所以我只希望房间的某个特定部分是可见的 这就是我目前所拥有的。它工作得很好,但它从最小角度开始,我希望它从我在检查器中设置的坐标开始 我尝试创建一个方法,以精确的值启动它,但没有成功 public class CameraMovement : MonoBehaviour { // Start is called before the first frame update public float

对Unity和C#有些陌生,尝试让相机四处移动,让用户看到房间。这有点像一部视觉小说,所以我只希望房间的某个特定部分是可见的

这就是我目前所拥有的。它工作得很好,但它从最小角度开始,我希望它从我在检查器中设置的坐标开始

我尝试创建一个方法,以精确的值启动它,但没有成功

public class CameraMovement : MonoBehaviour
{
    // Start is called before the first frame update
    public float mouseSensitivity = 70f;
    public float yawMax = 90f;
    public float yawMin = -90f;
    public float pitchMax = 90f;
    public float pitchMin = -90f;
    private float yaw = 0.0f;
    private float pitch = 0.0f;
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        SetCameraStartingPosition();
    }

    // Update is called once per frame
    void Update()
    {
        HandleMouseMovement();
    }
    void SetCameraStartingPosition() {
        transform.eulerAngles = new Vector3(0.02f, 0.292f, -0.323f);
    }

    void HandleMouseMovement() {
        yaw += mouseSensitivity * Input.GetAxis("Mouse X") * Time.deltaTime; 
        pitch -= mouseSensitivity * Input.GetAxis("Mouse Y") * Time.deltaTime;
        yaw = Mathf.Clamp(yaw, yawMin, yawMax);
        pitch = Mathf.Clamp(pitch, pitchMin, pitchMax);
        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    }
}

在调用该
SetCameraStartingPosition
方法后,将立即调用Update方法,并使用光标位置更改相机旋转(或相机附加到的变换)。但在此之前您使用了
CursorLockMode.Locked
,当前光标位置位于窗口的中心。因此,在第一帧开始显示之前,您的相机会移到窗口的中心。

切换了它们,不是这样。切换它们不会解决问题。根据光标在窗口中的位置更新变换摄影机。因此,您应该在
SetCameraStartingPosition
中将光标位置(而不是摄影机变换)设置为摄影机起始位置,或者使用除
Input.GetAxis
之外的任何方法。