将摄像机拖动到一个极限(x,y轴) - UNITY

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

我做了一个关于在2D或3D世界地图上拖动相机的教程。

代码是工作的。看起来像下面这样。

void Update_CameraDrag ()
{
    if( Input.GetMouseButtonUp(0) )
    {
        Debug.Log("Cancelling camera drag.");
        CancelUpdateFunc();
        return;
    }

    // Right now, all we need are camera controls

    Vector3 hitPos = MouseToGroundPlane(Input.mousePosition);
    //float maxWidth = 10;
    //if (hitPos.x > maxWidth)
    //{
    //    hitPos.x = maxWidth;
    //}
    Vector3 diff = lastMouseGroundPlanePosition - hitPos;

    Debug.Log(diff.x+ Space.World);

    Camera.main.transform.Translate (diff, Space.World);

    lastMouseGroundPlanePosition = hitPos = MouseToGroundPlane(Input.mousePosition);
}

现在我的问题是,你可以不受限制地拖动到任何方向,我想在X轴和Y轴上划定一些边界,基本上是相机的最大值。遗憾的是,我不知道它是如何工作的,尤其是,因为教程将所有的东西都设置为与Space.World有关--我甚至不知道那是什么。我的意思是,我知道 "diff "是相对于Space.World而言,当前位置和新位置之间的变化,然后摄像机会相应地移动。我只是想定义一个摄像机不能超过的最大值。有什么办法可以做到这一点。不幸的是,我还在学习--所以对我来说有点困难,我希望得到帮助。

unity3d camera drag
1个回答
1
投票

如果你将摄像机的X和Y位置记录在一个变量中,并使用MathF函数。即

如果你有一个100(x)x150(y)单位的地图,你可以使用

xPositionOfCamera = Mathf.Clamp(xPositionOfCamera, -50, 50);
yPositionOfCamera = Mathf.Clamp(YPositionOfCamera, -75, 75);

我不是100%确定这是否是你想要它做的,但这是我如何限制它。

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