【问题标题】:Unity - Making collision around the screenUnity - 在屏幕周围进行碰撞
【发布时间】:2020-04-09 18:34:24
【问题描述】:

我希望在我的相机视图周围创建EdgeCollider2D-s。 为此,我使用以下代码:

public class CreateWorldEdgeCollision2D : MonoBehaviour {

    public GameObject prefab;

    private void Awake() {

        GameObject[] colladas = new GameObject[4];
        for (int i = 0; i < colladas.Length; i++) {
            colladas[i] = Instantiate(prefab);
            colladas[i].transform.position = Vector3.zero;
        }

        var left = colladas[0].GetComponent<EdgeCollider2D>();
        var right = colladas[1].GetComponent<EdgeCollider2D>();
        var top = colladas[2].GetComponent<EdgeCollider2D>();
        var bottom = colladas[3].GetComponent<EdgeCollider2D>();

        left.points = new Vector2[] { Camera.main.ScreenToWorldPoint(Vector3.zero), 
            Camera.main.ScreenToWorldPoint(new Vector3(0,Camera.main.pixelHeight)) };
        right.points = new Vector2[] { Camera.main.ScreenToWorldPoint(new Vector3(Camera.main.pixelWidth,0)), 
            Camera.main.ScreenToWorldPoint(new Vector3(Camera.main.pixelWidth,Camera.main.pixelHeight)) };
        top.points = new Vector2[] { Camera.main.ScreenToWorldPoint(new Vector3(Camera.main.pixelWidth,0)),
            Camera.main.ScreenToWorldPoint(new Vector3(Camera.main.pixelWidth,Camera.main.pixelHeight)) };
        bottom.points = new Vector2[] { Camera.main.ScreenToWorldPoint(Vector3.zero),
            Camera.main.ScreenToWorldPoint(new Vector3(Camera.main.pixelWidth,0)) };
    }
}

我从检查器中拖出prefab,它只是一个带有EdgeCollider2D 组件的GameObject。我实例化 4,将它们的位置设置为空闲。然后我得到他们的EdgeCollider2D 组件。

然后我分配它们的点,每个对撞机只有 2 个点,例如:

  • 左:从左下角到左上角。
  • 右:从右下角到右上角

等等。

问题是,当我将Camera.main.ScreenToWorldPoint 应用于给定值时,它变成了Vector3 { 0,0,0.7 },我不知道为什么,因为我之前使用过这种方法并且效果很好。如何在屏幕周围进行碰撞?

【问题讨论】:

  • 如果您的相机处于透视模式,我猜这是 z 位置问题
  • 是的,它是 persp。但是画布是 2D 的,我不知道将其 Z 设置为什么,所以我将其设置为零。您认为我应该将 Z 设置为多少?
  • 大声笑我设置了一个正交凸轮,它工作得很好xd
  • 感谢您的提醒。如果其他人需要解决方案,我编写了我的工作代码作为答案。
  • 当它的透视需要点的准确位置。您可以在想要查看的位置放置一个立方体,然后可以使用您的代码转换该点。

标签: c# unity3d


【解决方案1】:

对于需要此解决方案的任何人,只需使用正射相机即可。而且我在代码中犯了一个错误,所以这里是正确的:

public class CreateWorldEdgeCollision2D : MonoBehaviour {

    public GameObject prefab;
    private Camera ortho;
    private void Awake() {
        ortho = GameObject.FindGameObjectWithTag("OrthoCam").GetComponent<Camera>();
        GameObject[] colladas = new GameObject[4];
        for (int i = 0; i < colladas.Length; i++) {
            colladas[i] = Instantiate(prefab);
            colladas[i].transform.position = Vector3.zero;
        }

        var left = colladas[0].GetComponent<EdgeCollider2D>();
        var right = colladas[1].GetComponent<EdgeCollider2D>();
        var top = colladas[2].GetComponent<EdgeCollider2D>();
        var bottom = colladas[3].GetComponent<EdgeCollider2D>();


        var testshit1 = ortho.pixelHeight;
        var testshit2 = ortho.pixelWidth;

        var test1 = ortho.ScreenToWorldPoint(new Vector3(testshit1, 0));


        left.points = new Vector2[] { ortho.ScreenToWorldPoint(Vector3.zero), 
            ortho.ScreenToWorldPoint(new Vector3(0,ortho.pixelHeight)) };
        right.points = new Vector2[] { ortho.ScreenToWorldPoint(new Vector3(ortho.pixelWidth,0)), 
            ortho.ScreenToWorldPoint(new Vector3(ortho.pixelWidth,ortho.pixelHeight)) };
        top.points = new Vector2[] { ortho.ScreenToWorldPoint(new Vector3(0,ortho.pixelHeight)),
            ortho.ScreenToWorldPoint(new Vector3(ortho.pixelWidth,ortho.pixelHeight)) };
        bottom.points = new Vector2[] { ortho.ScreenToWorldPoint(Vector3.zero),
            ortho.ScreenToWorldPoint(new Vector3(ortho.pixelWidth,0)) };
    }
}

(错误是顶部碰撞 - 它的坐标错误)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多