Unity 相机在到达左极限时传送到右极限

问题描述 投票:0回答:1

所以我正在尝试制作一款类似于《盗贼之海》的游戏,并且我正在尝试设置相机限制。一切正常,直到我给 ycamlimitwheensteering 一个大于 90 的值。我在这里失去了理智,请有人帮忙,当它大于 90 并且您达到左极限时,相机传送到右极限

using UnityEngine;

public class MouseLook : MonoBehaviour
{
    public float mouseSensitivity = 100f;
    public Transform playerBody;

    float xRotation = 0f;
    float yRotation = 0f;

    private bool isLerping = false;

    private bool isSteeringWheel;

    [SerializeField] public int YCamLimitWhenSteering = 70;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        if (isLerping) return; // Zablokuj kontrolę nad kamerą, jeśli jest lerpowanie

        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        // Rotacja osi X (pionowa)
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

        if (isSteeringWheel)
        {
            yRotation += mouseX;

            // Oblicz nowe ograniczenia

            float minY = 90f - YCamLimitWhenSteering;
            float maxY = 90f + YCamLimitWhenSteering;


            if (yRotation < minY)
            {
                yRotation = minY;
            }
            else if (yRotation > maxY)
            {
                yRotation = maxY;
            }

            playerBody.localRotation = Quaternion.Euler(0f, yRotation, 0f);
        }
        else
        {
            playerBody.Rotate(Vector3.up * mouseX);
        }


    }


    public void SetSteering(bool isAtWheel)
    {
        isSteeringWheel = isAtWheel;

        // Jeśli gracz zaczyna sterować, ustaw yRotation na aktualną rotację
        if (isAtWheel)
        {
            yRotation = playerBody.localRotation.eulerAngles.y;
        }
    }

    public void SetLerping(bool lerping)
    {
        isLerping = lerping;
    }

}

我预计它会在达到某个值时阻塞,但它不起作用。另外,它使用额外的 90f,因为当我不使用它时,前视图就像左边一样,所以我需要添加 90 来实际从前视图生成限制

c# unity-game-engine
1个回答
0
投票

那么,让我们来看看重要的代码

            float minY = 90f - YCamLimitWhenSteering;
        float maxY = 90f + YCamLimitWhenSteering;


        if (yRotation < minY)
        {
            yRotation = minY;
        }
        else if (yRotation > maxY)
        {
            yRotation = maxY;
        }

好吧,假设您将 YCamlimitWhenStearing 设置为 5。

这使得 minY 为 90-5,即 85,maxY 为 95。

但是当值为 5 时,整体看起来仍然很难正确。

现在,我的一部分假设,minY 应该是 -90f,所以,离开,而可能发生的部分原因是有时统一,如果说 -90,实际上显示 270 ....

最重要的部分是,调试,放入调试语句,在它移动时显示值,这样你就可以看到它有什么,它去哪里以及正在执行什么代码,因为你现在有了这些值,为什么。

© www.soinside.com 2019 - 2024. All rights reserved.