Unity - 在屏幕上进行碰撞

时间:2018-02-09 09:22:47

标签: c# unity3d

我希望在我的相机视图周围创建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,它只是一个GameObject,上面有EdgeCollider2D个组件。我实例化4,将他们的位置设置为空闲。然后我得到他们的EdgeCollider2D组件。

然后我分配他们的点数,每个碰撞器只有2个点,例如:

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

等等。

问题是,当我将Camera.main.ScreenToWorldPoint应用于给定值时,它变为Vector3 { 0,0,0.7 }并且我不知道为什么因为我之前使用过此方法并且它工作得很好。 任何有助于在屏幕周围发生碰撞的帮助都会受到欢迎。

1 个答案:

答案 0 :(得分:2)

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

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)) };
    }
}

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