- Home /
OverlapSphere doesn't detect colliders
I have a problem that has been driving me crazy for a week already. I have 6 boxes as Buildings and I have a script that places each building randomly according to the previous building's position. The script uses OverlapSphere method to check if the place where it places the next building is empty. Buildings are in one layer that is included in layermask used in this script.
GameObject[] Buildings=new GameObject[6];
int i;
int k;
Vector3 Move;
Vector3[] Moves=new Vector3[8];
public float Distance=10f;
public LayerMask Mask;
void Start () {
//This is a set of possible directions of building placements (horizontal, vertical and diagonal directions)
Moves[0]=new Vector3(0,0,1);
Moves[1]=new Vector3(1,0,1);
Moves[2]=new Vector3(1,0,0);
Moves[3]=new Vector3(1,0,-1);
Moves[4]=new Vector3(0,0,-1);
Moves[5]=new Vector3(-1,0,-1);
Moves[6]=new Vector3(-1,0,0);
Moves[7]=new Vector3(-1,0,1);
Buildings=GameObject.FindGameObjectsWithTag("Building");
//Placing the first building
Buildings[0].transform.position=new Vector3(0,0.5f,0);
i=1;
while(i<=5)
{
//Choosing random direction
k=Random.Range(0,8);
Debug.Log(k);
Move=Moves[k];
//Checks if there is a building in the place for the new building
Collider[] Scan=Physics.OverlapSphere(Buildings[i-1].transform.position+Move*Distance, 5f,Mask);
if(Scan.Length==0)
{Buildings[i].transform.position=Buildings[i-1].transform.position+Move*Distance;
Debug.Log(Buildings[i]);
Debug.Log(Buildings[i-1].transform.position+Move*Distance);
i++;
}
else if(Scan.Length>0){
Debug.Log("Found taken place");
Debug.Log(Buildings[i]);}
}
}
The problem is that OverlapSphere doesn't detect boxes. I tried placing terrain from another layer and changing the layermask to that layer and it detected terrain. It just refuses to detect boxes, resulting in multple boxes being placed at the same position. All boxes have standard box colliders. I can't find any other explanation, other than something is wrong with OverlapSphere.
Are you sure your buildings have a Collider ? (not in trigger mode)
By the way, when you will have more complicated colliders, keep in $$anonymous$$d the Unity note about the Physics.OverlapSphere function :
NOTE: Currently this only checks against the bounding volumes of the colliders not against the actual colliders.
Yes, rechecked it many times. They all have box colliders which are not triggers. Can the problem be that all buildings are children of an empty GamObject?
P.S. Does the objet need to be fully inside the sphere to be detected or can it partly be inside the sphere and still be detected?
I don't think the empty parent could be the problem.
I have never used the Physics.OverlapSphere, according to the doc : this function Returns an array with all colliders touching or inside the sphere.
Have you tried to draw a Gizmo representing the OverlapSphere with the specified parameters ? It could be useful to see the representation of this sphere in the editor.
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnDrawGizmos.html
EDIT : Which game object holds the script you've pasted ? Are you sure the script is executed ?
Checked it. Every box is fully wrapped in its own sphere, exactly in the center.
You could do your own overlapsphere: if you're doing the sphere from point P, you could check if the distance from point P to the bounds of the collider is less than the overlap sphere's distance:
//for checking overlapSphere(P, rad) against house with collider hCol:
if(rad <= Vector3.Distance(P, hCol.ClosestPointOnBounds(P)) {
//the house is within the overlap sphere.
}
Answer by rageingnonsense · May 29, 2015 at 06:31 PM
I believe the problem you are having is because you are doing this in Start().
What is happening is that you are moving the transforms inside Start(), but the positions have not actually changed yet until the end of the frame. As such, you are checking for colliders that are not in their desired positions yet, just scheduled to be.
See http://docs.unity3d.com/460/Documentation/Manual/ExecutionOrder.html. Notice the physics update happens AFTER Start().
Trying doing this without using physics:
if(Vector3.Distance(building[i].transform.position, building[i-1].transform.position) < buildingDiameter) {
// they are overlapping
}
This has the added benefit of being cheaper than checking against colliders.
Yes! Thank you very much, it worked! I only needed to cycle it through checking distance with every previous building to see if there's an overlap. Thank you very much @rageingnonsense and @Baste!