How can I get a X, Z position from Physics.CheckSphere?

Hi so I have a Physics.CheckSphere that checks for collisions of the ‘unwalkableMask’ layer in a ‘graph’ Node - it is set to a ‘walkable’ bool which is true when there is no collision found and false when there is a collision found. How get I retrieve the x and z co-ordinates of where the collision is found? Here is the function of my script where the Physics.CheckSphere is called:

    void GeneratePathfindingGraph() {
        graph = new Node[mapSizeX, mapSizeZ];
        Vector3 worldBottomLeft = gridPos - Vector3.right * gridWorldSize.x/2f - Vector3.forward * gridWorldSize.y/2f;

        for (int x = 0; x < mapSizeX; x++) {
            for (int z = 0; z < mapSizeZ; z++) {
                worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.forward * (z * nodeDiameter + nodeRadius);
                walkable = !(Physics.CheckSphere (worldPoint, nodeRadius, unwalkableMask));
                graph [x, z] = new Node (walkable, worldPoint);
                graph [x, z].x = x;
                graph [x, z].z = z;
            }
        }

You can’t with CheckSphere but you can with OverlapSphere (Unity - Scripting API: Physics.OverlapSphere) which returns a list of Colliders that were hit. With those colliders you can get the closest point on the collider to the given position (Unity - Scripting API: Collider.ClosestPointOnBounds) and then use the X and Z components from that position.

1 Like

ok so I have created a Collider array from Physics.OverlapSphere - how do I obtain the list of colliders and how do I implement Collider.ClosestPointOnBounds? Basically the way it works is that I have a graph array of tiles and I need to convert the coordinates of the collider object to tiles that become obstacles on my tile array. Here is the GeneratePathfindingGraph function again but I have also included my GenerateMapData which generates the tile array.

Just to make it clear:
tiles[x,z] = 0 refers to all of the tiles being walkable.
tiles[x,z] = 2 referes to all of the tiles being obstacles.

void GenerateMapData() {

        tiles = new int[mapSizeX,mapSizeZ];

        for(int x=0; x < mapSizeX; x++) {
            for(int z=0; z < mapSizeX; z++) {
                tiles[x,z] = 0;
            }
        }
}

void GeneratePathfindingGraph() {
        graph = new Node[mapSizeX, mapSizeZ];
        Vector3 worldBottomLeft = gridPos - Vector3.right * gridWorldSize.x/2f - Vector3.forward * gridWorldSize.y/2f;

        for (int x = 0; x < mapSizeX; x++) {
            for (int z = 0; z < mapSizeZ; z++) {
                worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.forward * (z * nodeDiameter + nodeRadius);
                walkable = !(Physics.CheckSphere (worldPoint, nodeRadius, unwalkableMask));
                Collider[] hitColliders = Physics.OverlapSphere (worldPoint, nodeRadius, unwalkableMask);
                graph [x, z] = new Node (walkable, worldPoint);
                graph [x, z].x = x;
                graph [x, z].z = z;


            }
        }
}
foreach (var c in hitColliders)
{
    Vector3 foo = c.ClosestPointOnBounds(worldPoint);
    // foo is the point on the collider that is closest to worldPoint
}

Yes!! Thank you very much its worked!