2D alternative to Physics.CheckSphere

Hi,

I’m trying to convert the following method from a pathfinder script to something that would work in a 2D game with 2D objects.

void CreateGrid() {
	grid = new Node[gridSizeX,gridSizeY];

            Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x/2 - 
            Vector3.forward * gridWorldSize.y/2;

	for (int x = 0; x < gridSizeX; x ++) {
		for (int y = 0; y < gridSizeY; y ++) {
			Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + 
                          nodeRadius) + Vector3.forward * (y * nodeDiameter + nodeRadius);

                **bool walkable = !(Physics.CheckSphere(worldPoint,nodeRadius,unwalkableMask));**

                 grid[x,y] = new Node(walkable,worldPoint, x,y);
		}
	}
}

I have tried to use;

void CreateGrid()
{
    grid = new Node[gridSizeX, gridSizeY];
    Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - new Vector3 (0,1,0) * gridWorldSize.y / 2;

    for (int x = 0; x < gridSizeX; x++)
    {
        for (int y = 0; y < gridSizeY; y++)
        {
            Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + 
            new Vector3 (0,1,0) * (y * nodeDiameter + nodeRadius);

            **BoxCollider2D bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
            bool walkable = !(Physics2D.IsTouchingLayers(bc, unwalkableMask));**

            grid[x, y] = new Node(walkable, worldPoint, x, y);
            print(walkable);
            
        }
    }
}

currently bool walkable is returning true ( or false if i remove !) so the bc boxcollider is not touching unwalkable layer.

I’m well out my depth here. Trying to convert a pathfinder script to 2D. I can give more info but I need some help if poss. Thanks.

The 2d equivalent of CheckSphere / OverlapSphere would be OverlapCircle. The 2d physics system doesn’t have a CheckCircle method. However you just have to check the return value of OverlapCircle against null

Thanks so much. I tried;

void CreateGrid()
{
    grid = new Node[gridSizeX, gridSizeY];
    Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - new Vector3 (0,1,0) * gridWorldSize.y / 2;

    for (int x = 0; x < gridSizeX; x++)
    {
        for (int y = 0; y < gridSizeY; y++)
        {
            Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + new Vector3 (0,1,0) * (y * nodeDiameter + nodeRadius);
            //BoxCollider2D bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
            box = new Vector2(nodeDiameter - 0.1f, nodeDiameter - 0.1f);
            bool walkable = !(Physics2D.OverlapBox(worldPoint, box, 90, unwalkableMask));
            //bc.transform.position = worldPoint;
            grid[x, y] = new Node(walkable, worldPoint, x, y);
            print(box);
            
        }
    }
}

and it worked nicely. just gotta get my player to follow the path now! The full script is here if any one is interested.