Endless Runner with new Biome each time
I made an endless runner, but now I want to have a random Array or List (I don't really understand them) to pick a biome, and depending on the biome it chooses I'll spawn my parts from this List, or that List.
This works but terribly written, I made a for loop above the other one for (int b = 0; b != chosenBiome; b++) but I still need SpawnLevelPart(b)() Even though that's not a thing. Unless I just update it manually which I don't feel like doing.
[SerializeField] private List<Transform> levelPartList;
[SerializeField] private List<Transform> levelPartList1;
[SerializeField] private List<string> biomeTypes;
private int chosenBiome;
private Vector3 lastEndPosition;
private void Awake()
{
chosenBiome = Random.Range(0, biomeTypes.Count);
int startingSpawnLevelParts = 5;
if (chosenBiome == 0)
{
lastEndPosition = levelPart_Start.Find("EndPosition").position;
for (int i = 0; i < startingSpawnLevelParts; i++)
{
SpawnLevelPart0();
}
} else if (chosenBiome == 1)
{
lastEndPosition = levelPart_Start.Find("EndPosition").position;
for (int i = 0; i < startingSpawnLevelParts; i++)
{
SpawnLevelPart1();
}
}
}
private void Update()
{
if (chosenBiome == 0)
{
if (Vector3.Distance(player.position, lastEndPosition) < PLAYER_DISTANCE_SPAWN_LEVEL_PART)
{
SpawnLevelPart0();
}
} else if (chosenBiome == 1)
{
if (Vector3.Distance(player.position, lastEndPosition) < PLAYER_DISTANCE_SPAWN_LEVEL_PART)
{
SpawnLevelPart1();
}
}
}
private void SpawnLevelPart0()
{
Transform chosenLevelPart = levelPartList[Random.Range(0, levelPartList.Count)];
Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition);
lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
}
private void SpawnLevelPart1()
{
Transform chosenLevelPart = levelPartList1[Random.Range(0, levelPartList1.Count)];
Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition);
lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
}
private Transform SpawnLevelPart(Transform levelPart, Vector3 spawnPosition)
{
Transform levelPartTransform = Instantiate(levelPart, spawnPosition, Quaternion.identity);
return levelPartTransform;
}
}
Your answer
Follow this Question
Related Questions
Increase List of Spawns Dynamically 1 Answer
All crops in list growing at once when they are supposed to grow individually 0 Answers
Copying a list of lists of int's in c# 1 Answer
Trying to program two buttons to appear when the player in my game dies 0 Answers
Only spawning power ups that the player wants in that game 1 Answer