Hi there,
I’m tearing my hair out over this and I feel like its something super simple that I’m just not seeing.
I have a 5 spheres (ArchiveNodes in this example) that I’m hoping to spawn on the edge of a circle, radius 3, at the origin. I’m using physics.overlap sphere to check whether to prospective position to place my sphere, tagged archive, is going to overlap with any other archives?
At the beginning there are no spheres anywhere near this circle they are pooled somewhere else off in the scene. When they spawn in they sometimes overlap which is exactly what I don’t want to happen.
What’s strange is that if I place existing objects with colliders on the circle edge such as cubes that are also tagged Archive they never overlap with them? (All the spheres being spawned also have sphere colliders on)
I’ve tried taking this spawn out of the Awake function into OnKeyDown.
Here is my code which I based off this [answer][1] from a couple years ago:
private void Awake()
{
ThisTransfom = GetComponent<Transform>();
for (int i = 0; i < ArchivePrefab.Length; i++)
{
//prospective position for spawn
Vector3 dir = Vector3.zero;
//whether or not we can spawn in this position
bool validPosition = false;
while (!validPosition)
{
//pick new position for potential spawn
Vector2 temp = (Random.insideUnitCircle).normalized * SphereRadius;
dir = temp;
//position is true until proven false
validPosition = true;
//collect all colliders within our obstacle check radius
Collider[] colliders = Physics.OverlapSphere(dir, .75f);
//go through each collider collected
foreach(Collider col in colliders)
{
//if collider has a matching tagged "Archive"
if(col.tag == "Archive")
{
Debug.Log(ArchivePrefab*.name + " is touching " + col.gameObject.name + " at " + dir);*
//then this is no longer a valid spawn position
validPosition = false;
}
}
}
//if we exited while loop with a valid position
if (validPosition)
{
Debug.Log("Place " + ArchivePrefab*.name + " at " + dir);*
GameObject point = ArchivePrefab*;*
Node node = point.GetComponent();
point.SetActive(true);
point.transform.position = dir;
point.transform.parent = ParentCircle;
Vector3 rot = (Vector3.zero - point.transform.position).normalized;
node.AlignObj.rotation = Quaternion.LookRotation(rot, Vector3.right);
}
}
}
*[1]: https://answers.unity.com/questions/1506835/how-to-prevent-3d-object-spawn-overlapping.html*_
I had pretty much the same code before but with a do while instead however being a verified answer and being seemingly exactly what I needed I thought I would go with this instead!
Thanks in advance for taking the time to help! Really appreciate it!_