Raycast2D不与墙碰撞

时间:2019-06-26 07:30:44

标签: c# unity3d

我有这个代码

        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, Mathf.Infinity, LayerMask.NameToLayer("Wall"));
        Debug.DrawRay(transform.position, Vector2.right);

        if(hit.collider != null)
        {
            Debug.Log("Wall");
        }

这是我要撞墙的地方
https://i.imgur.com/CLu3r5s.png
但是当我运行调试射线时,它越过了墙,但是我却没有收到任何消息

1 个答案:

答案 0 :(得分:-1)

射线广播使用蒙版,即工作代码的方式 RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, Mathf.Infinity, LayerMask.NameToLayer("Wall"));

需要细微的改变

尝试RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, Mathf.Infinity, ~1<<LayerMask.NameToLayer("Wall"));

1 <<移位位。 〜称赞

有关raycast文档中有关统一性的内容(尽管其列在3d下,但仍适用https://docs.unity3d.com/ScriptReference/Physics.Raycast.html