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); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter