raycast2d

I need some help to understand how to use raycast2d. I am using the top down kind of map (you see everything from above), my map is compose from tiles and every one of them has its own layer (or no layer at all). My walkableMask contains elements so it isn’t empty. But the hit variable does not return any value. Could you help me figure why?

for(int x = 0; x < gridSizeX; x++)
        {
            for(int y = 0; y < gridSizeY; y++)
            {
                Vector3 worldPoint = worldButtomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.up * (y * nodeDiameter + nodeRadius);
                bool walkable = !(Physics2D.OverlapCircle(worldPoint, nodeRadius, unwalkableMask));

                int movementPenalty = 0;

                if (walkable)
                {
                    Vector3 rayOrigin = worldPoint + Vector3.up * 50;
                    Ray2D ray = new Ray2D(new Vector2(rayOrigin.x, rayOrigin.y), Vector2.down);
                    RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 100, walkableMask);
                    Debug.Log(hit.collider.gameObject.layer != 0 ? hit.collider.gameObject.layer : 0);
                    if (hit)
                    {
                        walkableRegionsDictionary.TryGetValue(hit.collider.gameObject.layer, out movementPenalty);
                        Debug.Log(movementPenalty);
                    }
                }
                grid[x, y] = new Node(walkable, worldPoint, x, y, movementPenalty);
            }
        }


What exactly are you trying to do? Are you trying to figure out what terrain the player is walking on?