- Home /
Question by
socialBacons · Nov 20, 2021 at 10:56 AM ·
overlapsphere
OverlapSphere missing colliders
I'm trying to create a batch of trees which I create by finding a random position inside the area and then checking with Physics.OverlapSphere that two tree are not too close to each other.
Vector3 pos = GetRandomPos(min, max);
Collider[] col = Physics.OverlapSphere(pos, density, treeMask);
if (pos != Vector3.zero && col.Length == 0)
{
if (inactiveTreeList.Count > 0)
{
if(inactiveTreeList.Peek() != null)
{
GameObject obj = inactiveTreeList.Dequeue();
obj.SetActive(true);
obj.transform.position = pos;
obj.transform.rotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
Debug.Log(obj.layer.ToString());
treeList.Add(obj);
}
else
inactiveTreeList.Dequeue();
}
else
{
treeList.Add(Instantiate(tree, pos, Quaternion.identity, container));
}
After debugging a little bit a found that some potential trees when spawning do in fact find colliders nearby and therefore not spawning so it does work as intended. But quite frequently col.Length = 0 even though I can clearly see from the editor that trees spawn too close to each other.
Comment
Your answer