Procedural Level Generation
Stryg for at vise menuen
Automatic Platforms
So far, we have been placing platforms manually. But for an endless jumper, that would be slow and tedious. Instead, we can spawn platforms and powerups automatically using a GameManager script.
Step 1: Prefabs
To spawn objects dynamically, they must first be saved as Prefabs.
- create a folder called Prefabs;
- drag your platform
GameObjectfrom the Hierarchy into the folder; - it turns blue, indicating it is now a prefab.
A prefab:
- stores all components (collider, scripts, etc.);
- can be instantiated multiple times;
- updates all instances if edited.
Step 2: GameManager Setup
Create an empty GameObject called GameManager.
Attach GameManagerScript.
We create references to:
public GameObject platformPrefab;
public GameObject powerupPrefab;
Then assign both prefabs in the Inspector.
Step 3: Understanding Instantiate()
Instantiate() requires 3 parameters:
- what to spawn ->
platformPrefab; - where to spawn ->
Vector3position; - rotation ->
Quaternion.identity(default rotation).
Example:
Instantiate(platformPrefab, platformPosition, Quaternion.identity);
Step 4: Spawning Platforms with a For Loop
We use loops to spawn multiple platforms automatically.
Easy Platforms
int platformCount = 10;
for (int i = 0; i < platformCount; i++)
Each time the loop runs:
Yincreases by a random amount between3and7;Xis randomized between-10and13(or wherever the edges of your game view are).
platformPosition.y += Random.Range(3, 7);
platformPosition.x = Random.Range(-10, 13);
Then the platform is instantiated.
This creates the first 10 easier platforms.
Harder Platforms
int platformCount2 = 40;
for (int i = 10; i < platformCount2; i++)
This second loop continues spawning platforms after the first 10.
Now:
- the
Yposition should increase by a larger random amount between10and12; - the
Xposition should still be randomized between-10and13(for example).
platformPosition.y += Random.Range(10, 12);
platformPosition.x = Random.Range(-10, 13);
Because the vertical spacing is much greater than in the first loop (3-7), these platforms are:
- further apart;
- harder to reach;
- part of a more difficult zone.
This creates a clear increase in difficulty as the player climbs higher.
Step 5: Spawning Powerups
int powerupCount = 10;
Powerups are spawned separately using another for loop:
for (int i = 0; i < powerupCount; i++)
{
powerupPosition.y += Random.Range(30,60);
powerupPosition.x = Random.Range(-10, 13);
Instantiate(powerupPrefab, powerupPosition, Quaternion.identity);
}
Because powerups:
- are fewer;
- appear further apart vertically;
- stay within horizontal bounds.
What We Achieved
- procedural level generation;
- randomized platform and powerup placement;
- progressive difficulty.
Now the level builds itself when the game starts.
1. What is the purpose of saving the platform as a Prefab?
2. Why do you use platformPosition.y += Random.Range(3, 7); in the first loop when spawning platforms?
3. Why are platforms harder in the second loop?
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat