Skip to content

Generate Drunkard’s Walk Cells

Generates an organic pattern of grid cells using the Drunkard’s Walk algorithm.
This is a classic procedural generation technique where one or more “walkers” move step-by-step through a grid, marking cells as they go.

The result can range from winding tunnels to blobby cave systems or isolated pockets - depending on the settings:

Walkers: 5
Steps: 100
Forward: 0.7
Walkers: 200
Steps: 4
Forward: 1.0
Walkers: 600
Steps: 600
Forward: 0.7
Walkers: 5
Steps: 100
Forward: 0

What it does

The algorithm starts with one or more walkers inside a bounded region.
Each walker randomly steps in one of the four cardinal directions, marking each cell it visits as selected (e.g., floor, grass, water).

  • A higher Forward Bias makes walkers continue straight more often, forming tunnels or paths.
  • A lower Forward Bias causes frequent turns, forming pockets or caves.
  • With more walkers and steps, the result can fill much of the region, forming complex networks of connected areas.

The output is a list of Vector2Int positions representing the selected cells - how you interpret or paint them is up to you.

Inputs

Field Description
Bounds The BoundsInt region the walkers can move within. All selected cells will be inside this area.
Walker Count Number of walkers to spawn. More walkers create more regions or branches.
Max Steps Total number of steps shared by all walkers. Higher values expand the generated area.
Forward Bias Chance to continue straight instead of turning. Low = twisty, blobby shapes; high = straighter paths or tunnels.
Target Fill % Stop early once this fraction of the area is selected. ≤ 0 ignores this and just uses Max Steps.

Outputs

Field Description
Cells The set of selected Vector2Int grid positions. You can interpret these as any type of feature - floors, water, grass, or paths.
Fill Percent Fraction of the Bounds that ended up selected (0–1).
Used Bounds The BoundsInt region actually used during generation (usually matches input Bounds).

How it works

  1. Initializes the Drunkard’s Walk simulation within the input Bounds.
  2. Spawns Walker Count agents at random starting points.
  3. For each step (up to Max Steps):

  4. Mark the current grid cell as selected.

  5. Continue in the same direction or pick a new random direction based on Forward Bias.
  6. Clamp movement to stay inside Bounds.
  7. If Target Fill % > 0 and reached, stop early.
  8. Returns all selected cells and final fill statistics.

This simple algorithm can generate a surprising variety of organic shapes:

  • A single walker with high Forward Bias → winding paths.
  • Multiple walkers with moderate Forward Bias → interconnected tunnel networks.
  • Low Forward Bias and many steps → wide caverns or lakes.

Determinism: Uses UnityEngine.Random. To reproduce results, set a seed with Random → Random Init State before running this action.

Tips & pitfalls

  • Tune for shape:
    • Low Forward Bias = scattered or rounded blobs.
    • High Forward Bias = long, continuous tunnels.
  • Walker Count adds complexity and branching - use multiple for more natural, interconnected patterns.
  • Target Fill % can prevent overfilling when generating compact pockets.
  • Interpretation is flexible: Use the same algorithm for caves, paths, grass clearings, rivers, or lakes - just change how you paint or fill the output cells.
  • Post-processing: You can smooth results by eroding, expanding, or connecting nearby clusters.