I’m making an endless runner, with objects instantiating at random points on my terrain. However, some of the objects are instantiating overtop of each other.
I have a hunch this might have something to do with the physics not being updated between instatiations (which are all happening in the same frame).
The prefabs being instantiated all have the tag “PlaceableObject” and a collider.
public void Place() {
int numObjects = Random.Range(TerrainController.MinObjectsPerTile, TerrainController.MaxObjectsPerTile);
for (int i = 0; i < numObjects; i++)
{
int prefabElement = ChooseObject();
Vector3 startPoint = RandomPointAboveTerrain();
RaycastHit hit;
if (Physics.Raycast(startPoint, Vector3.down, out hit) && hit.collider.CompareTag("Terrain"))
{
Quaternion orientation = Quaternion.Euler(Vector3.up * Random.Range(0f, 360f));
RaycastHit boxHit;
if (Physics.BoxCast(startPoint, TerrainController.PlaceableObjects[prefabElement].size, Vector3.down, out boxHit, orientation) && boxHit.collider.CompareTag("Terrain"))
{
Vector3 placementPosition = new Vector3(startPoint.x, hit.point.y, startPoint.z);
Instantiate(TerrainController.PlaceableObjects[prefabElement].gameObject, placementPosition, orientation, placedObjectsParent);
}
}
}
}
Any ideas?