Hello all,
I’ve researched this quite a bit and found a solution that, I think, works. However, the results aren’t quite what I expected and I’d love some helpful eyes to see if others might spot the error.
My code aims to spawn enemies based on the position of a “Spawner” (LeftSpawner) and then adds variance in the position based on the Y scale of said object.
private void SpawnInitialEnemies()
{
EntityCommandBuffer.Concurrent commandBuffer = beginInitializationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
Entities
.ForEach((Entity entity, int entityInQueryIndex, in DirectionalEnemyData directionalEnemyData, in LevelData levelData) =>
{
// Perhaps a more efficient way to get components of a single Entity that you have a reference too?
ComponentDataFromEntity<Translation> allTranslations = GetComponentDataFromEntity<Translation>(true);
ComponentDataFromEntity<CompositeScale> allScales = GetComponentDataFromEntity<CompositeScale>(true);
if (!allTranslations.Exists(levelData.LeftSpawner) || !allScales.Exists(levelData.LeftSpawner))
{
return;
}
// Get position of Spawner and Y Scale for later manipulation
Translation initialSpawnPos = allTranslations[levelData.LeftSpawner];
float zVariance = allScales[levelData.LeftSpawner].Value.c2.z / 2;
uint BaseSeed = 1;
for (int i = 0; i < levelData.InitialAmountOfEnemies; i++)
{
// Spawn enemies
Entity enemyInstance = commandBuffer.Instantiate(entityInQueryIndex, directionalEnemyData.DirectionalEnemyPrefab);
uint seed = (uint)(BaseSeed + entityInQueryIndex + i);
Random random = new Random(seed);
zVariance = random.NextFloat(-zVariance, zVariance);
commandBuffer.SetComponent(entityInQueryIndex, enemyInstance, new Translation { Value = initialSpawnPos.Value + new float3(0, 0, zVariance) });
}
}).ScheduleParallel();
beginInitializationEntityCommandBufferSystem.AddJobHandleForProducer(Dependency);
}
Now this bit:
uint seed = (uint)(BaseSeed + entityInQueryIndex + i);
Random random = new Random(seed);
zVariance = random.NextFloat(-zVariance, zVariance);
Does the randomness, however, the result is always either very close to the max or very close to the minimal value.
I’m used to using Unity’s built in Random.Range so using seeds and whatnot is a little new to me.
Thank you in advance,
Ramses