【问题标题】:How to add boundaries to my camera without jittering?如何在不抖动的情况下为我的相机添加边界?
【发布时间】:2020-11-08 00:05:51
【问题描述】:

我试图在统一中制作部落冲突风格的游戏,而且我是统一和 C# 脚本的新手,所以请放轻松。到目前为止,我已经在论坛和 YouTube 的帮助下编写了两个脚本。第一个是名为 PerspectivePan 的脚本,它附加到层次结构中名为 perspectiveHolder 的空对象。第二个脚本称为clampBounds 并附加到主摄像机。主摄像机通过第一个脚本中名为 cam 的变量附加到 perspectiveHolder。第一个脚本平移和缩放,第二个脚本设置相机的边界。我已经能够将相机保持在界限内,但是相机在边缘处抖动,并允许您向上超出界限,直到您在平移时松开鼠标按钮。如何在停止抖动的同时停止边缘平移?

public class PerspectivePan : MonoBehaviour
{
    private Vector3 touchStart;
    public Camera cam;
    public float groundZ = 0;
    public float zoomOutMin = 1;
    public float zoomOutMax = 8;
    private bool multiTouch = false;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            touchStart = GetWorldPosition(groundZ);
            multiTouch = false;
        }

        if (Input.touchCount == 2)
        {
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne = Input.GetTouch(1);

            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
            Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

            float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float currentMagnitude = (touchZero.position - touchOne.position).magnitude;

            float difference = currentMagnitude - prevMagnitude;

            zoom(difference * 0.01f);

            multiTouch = true;
        }

        else if (Input.GetMouseButton(0) && multiTouch == false)
        {
            Vector3 direction = touchStart - GetWorldPosition(groundZ);
            Camera.main.transform.position += direction;
        }

        zoom(Input.GetAxis("Mouse ScrollWheel"));

    }

    private Vector3 GetWorldPosition(float z)
    {
        Ray mousePos = cam.ScreenPointToRay(Input.mousePosition);
        Plane ground = new Plane(Vector3.down, new Vector3(0, 0, z));
        float distance;
        ground.Raycast(mousePos, out distance);
        return mousePos.GetPoint(distance);
    }

    void zoom(float increment)
    {
        Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView - increment*10, zoomOutMin, zoomOutMax);
    }
}
public class clampBounds : MonoBehaviour
{
    public float maxX, minX, maxY, minY, maxZ, minZ;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), Mathf.Clamp(transform.position.y, minY, maxY), Mathf.Clamp(transform.position.z, minZ, maxZ));
    }
}

【问题讨论】:

  • 一个建议:例如,为左边缘定义一个布尔标志。当值被钳位时,将此标志设置为真。当光标位于左边缘时不要向左滚动,因为标志为真。如果用户向右滚动,则将标志设置为 false。

标签: c# unity3d


【解决方案1】:

Clamp() 方法添加到您的clampBounds 类,然后在您的PerspectivePan Update() 方法的末尾调用GetComponent<clampBounds>().Clamp()

PerspectivePan 可能在clampBounds 之后更新,因此,当PerspectivePan 更新相机位置时,clampBounds 所做的调整将被覆盖并完全忽略

【讨论】:

    猜你喜欢
    • 2011-11-17
    • 2020-04-21
    • 2013-01-02
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2016-09-13
    相关资源
    最近更新 更多