public void Produce(GameObject prefab, SpawnZone zone) { var amount = Random.Range(zone.MinAmount, zone.MaxAmount); var spawnedObjects = new List <GameObject>(); for (var i = 0; i < amount; i++) { var attempts = 5; while (attempts > 0) { var position = zone.GetPositionInsideZone(); var isItFree = true; for (var j = 0; j < i - 1; j++) { if (Vector3.Distance(position, spawnedObjects[j].transform.position) < zone.Space) { isItFree = false; } } if (!isItFree) { ++attempts; continue; } var spawned = Object.Instantiate(prefab, position, zone.GetRandomRotation()); spawnedObjects.Add(spawned); break; } } }
public void Produce(GameObject prefab, SpawnZone zone) { var entity = GetEntity(prefab); var amount = Random.Range(zone.MinAmount, zone.MaxAmount); var entities = new NativeArray <Entity>(amount, Allocator.Temp); manager.Instantiate(entity, entities); for (var i = 0; i < amount; i++) { var spawned = entities[i]; var attempts = 5; while (attempts > 0) { var position = zone.GetPositionInsideZone(); var isItFree = true; for (var j = 0; j < i - 1; j++) { if (Vector3.Distance(position, manager.GetComponentData <Translation>(entities[j]).Value) < zone.Space) { isItFree = false; } } if (!isItFree) { ++attempts; continue; } manager.SetComponentData(spawned, new Translation { Value = position }); manager.SetComponentData(spawned, new Rotation { Value = zone.GetRandomRotation() }); break; } } }