Having trouble using Physics.OverlapSphere

101431-ezgif-4-94df342338.gifI’ve made up a simple 3D grid to detect objects within its area, and I’m trying to use OverlapSphere to identify the objects inside. I know this must be something super simple, or perhaps I just don’t understand the OverlapSphere method correctly. I hope someone can look at this and give me some insight into what I’m doing wrong.

This is the code I’ve made for the process:

public bool walkable = true;
public LayerMask unwalkableMask; // defined in editor
public LayerMask playerMask; // defined in editor
public bool housingPlayer = false;

private Collider[] coll;
private Vector3 nodeSize = new Vector3(0.7f, 0.7f, 0.7f);

void Update() {
    PhysicsChecks();
}

private void PhysicsChecks() {
    walkable = !Physics.CheckSphere(transform.position, .1f, unwalkableMask);
    housingPlayer = Physics.CheckSphere(transform.position, .1f, playerMask);
    coll = Physics.OverlapSphere(transform.position, .1f);
}

void OnDrawGizmos() {
    Gizmos.color = (walkable) ? Color.white : Color.red; // Will always start white in this test
    if (housingPlayer)
        Gizmos.color = Color.blue;
    if (coll[0].tag == "Player")
        Gizmos.color = Color.green;

    Gizmos.DrawCube(transform.position, nodeSize);
}

As you can see it’s a very short and simple block of code. All it does right now is use the (15, 20, 10) grid to detects when the player is touching one it its nodes via the CheckSphere method. If that returns true, the node turns blue. After that, if a node detects the player and allows access to the gameObject via the OverlapSphere method the node turns green. Simple enough.

The issue is that only one third to one half of the cubes turn green (Are correctly detected by OverlapSphere) when they come into contact with the player. It’s a different amount every time, but it’s generally around a third. Cubes 1-7ish of each axis work correctly, before they stop reacting after that number. All cubes in the grid react correctly to the code for the blue (CheckSphere) despite being practically the same code.

Below is the code that constructs the grid if that’s any help.

    grid = new GameObject[gridWidth, gridHeight, gridDepth];

    for (int x = 0; x < gridWidth; x++) {
        for (int y = 0; y < gridHeight; y++) {
            for (int z = 0; z < gridDepth; z++) {
                grid[x, y, z] = GameObject.Instantiate(node, new Vector3 (x, y, z), new Quaternion(0,0,0,1), parent);
                grid[x, y, z].GetComponent<NodeScript>().x = x;
                grid[x, y, z].GetComponent<NodeScript>().y = y;
                grid[x, y, z].GetComponent<NodeScript>().z = z;
            }
        }
    }
}

In summary, please help me figure out why OverlapSphere isn’t working correctly on all of my nodes.

I would das that not using any mask with OverlapSphere gives you an arbitrarily ordered array with colliders. It’s not guaranteed that the player is the first (col[0]).
You should use a mask or check all colliders for the player.