Tile Collider 2D and Physics2D Raycast

I have a 2D rectangular tilemap that is partially filled. It has a Tilemap Collider 2D component.

Meanwhile, in my script…

void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D raycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.down);
            Debug.Log(raycastHit2D.collider);
        }
    }

And yet, nothing. There is no collision recorded, only null in the debugger.

An answer for someone in the future:

I had to edit the tile in the Inspector and change its collision type to “Grid,” rather than “None.”

1 Like

Yes, you’re not going to detect collision shapes on a Tilemap without adding any!

More important is that you don’t use a raycast to detect an intersection with the mouse in 2D physics like you would in 3D physics. You use the much faster/efficient Physics2D.OverlapPoint simply because this is 2D!

1 Like

What if colliders are 2D but the camera is 3D (perspective)? In that case you have to use ScreenPointToRay and GetRaytIntersection. Do I understand this correctly?

Yes. As long as you remember that it’s 2D physics and it exists on the XY plane only. That query will sort by the Transform Z for you but the physics knows nothing about it. If all the colliders are on the same Z (which doesn’t affect physics) then the order is “undefined”.

I only mention this because I’ve encountered many devs who just grab 3D physics code and straight-up use it with 2D and don’t think about the fact that it’s not 3D. :slight_smile:

1 Like