Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Procedural Level Generation | Programming with C# - Extra Mechanics
Unity for Beginners

bookProcedural Level Generation

Desliza para mostrar el menú

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 GameObject from 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 -> Vector3 position;
  • 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:

  • Y increases by a random amount between 3 and 7;
  • X is randomized between -10 and 13 (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 Y position should increase by a larger random amount between 10 and 12;
  • the X position should still be randomized between -10 and 13 (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?

question mark

What is the purpose of saving the platform as a Prefab?

Selecciona la respuesta correcta

question mark

Why do you use platformPosition.y += Random.Range(3, 7); in the first loop when spawning platforms?

Selecciona la respuesta correcta

question mark

Why are platforms harder in the second loop?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Sección 4. Capítulo 4
some-alt