A simple procedural platform generator for 2D/3D endless climbing games made with Unity. This script dynamically spawns platforms based on the player's height, allowing smooth level generation with configurable spawn limits, spacing, and randomness.
- Dynamic platform spawning
- Adjustable vertical and horizontal spacing
- Maximum platform limit
- Randomized platform positions
- Lightweight and beginner-friendly
- Easy integration into existing Unity projects
- Unity 2021 or newer
- A player object with the
Playertag - A platform prefab
- Create a platform GameObject in your scene
- Add:
- Sprite Renderer / Mesh Renderer
- Collider2D or Collider
- Drag the object into the Project folder to create a prefab
Make sure your player GameObject:
- Has the tag:
Player
- Contains movement and physics components
-
Create an empty GameObject
-
Rename it to:
PlatformSpawner -
Attach the
PlatformSpawner.csscript
In the Inspector:
-
Drag your platform prefab into:
Platform Prefab
| Variable | Description |
|---|---|
platformPrefab |
The platform object to spawn |
maxPlatforms |
Total number of platforms allowed |
initialPlatforms |
Platforms spawned at the start |
verticalGap |
Distance between platforms vertically |
horizontalRange |
Random horizontal spawn range |
initialPlatformYPosition |
Starting Y position of the first platform |
- The spawner creates an initial set of platforms
- As the player moves upward, new platforms generate ahead
- Platforms stop spawning once the maximum limit is reached
- The script uses randomized X positions for variety
- Doodle Jump style games
- Endless vertical platformers
- Tower climbing games
- Infinite runner prototypes
Possible upgrades you can add:
- Multiple platform types
- Moving platforms
- Platform pooling system
- Difficulty scaling
- Coin and enemy spawning
- Finish line generation
void SpawnPlatform()
{
Vector3 spawnPosition = new Vector3();
spawnPosition.y = initialPlatformYPosition;
spawnPosition.x = Random.Range(-horizontalRange, horizontalRange);
Instantiate(platformPrefab, spawnPosition, Quaternion.identity);
initialPlatformYPosition += verticalGap;
platformsSpawned++;
}This project is open-source and free to use for learning and personal projects.
Created by StixKnowledge