2D Procedural Level Generator
Wed Oct 04 2023 01:31:20 GMT+0000 (Coordinated Universal Time)
Saved by
@juanesz
private void GeneratePlatform()
{
int currentDifficulty = GameManager.instance.difficulty;
List<Transform> validParts = new List<Transform>();
// Iterate through levelObject array and add parts with matching difficulty
foreach (Transform part in levelObject)
{
LevelPart levelPart = part.GetComponent<LevelPart>();
if (levelPart != null && levelPart.difficulty == currentDifficulty)
{
validParts.Add(part);
}
}
while (Vector2.Distance(lasttLevelObjectSpawned.position, player.transform.position) < distanceToSpawn)
{
if (validParts.Count > 0)
{
// Randomly select a valid level part of the current difficulty
Transform selectedPart = validParts[Random.Range(0, validParts.Count)];
Vector2 newPosition = new Vector2(lasttLevelObjectSpawned.position.x - selectedPart.Find("StartPosition").position.x, 0);
Transform newPart = Instantiate(selectedPart, newPosition, selectedPart.rotation, transform);
lasttLevelObjectSpawned = newPart.Find("EndPosition").transform;
spawnedObjects.Add(newPart);
}
}
}
content_copyCOPY
Comments