How would I go about making a random dungeon generator script to make random(ish) dungeons at run time?
I know that I would have to make some prefabs for each section (right corner, t-section, large area…) and instantiate them when needed. But I can’t get my head around how to do it. Specifically, how to make a grid for them to align to, and how to make it so the paths all join up.
Any help?
Thanks, Geko_X
PS: I use JavaScript or UnityScript, whatever it’s called.
I did find a solution for this. It is a cheat method, but it works for what I wanted.
I made 10 different “SuperTiles” prefabs in the editor. A SuperTile is a 5*5 group of small tiles, where there is an exit at the North, South, East and West, plus a walkable space in the centre. These SuperTiles are like a small maze.
I then wrote a script that randomly picked a SuperTile out of an array, spawned it, repeat… until the set size on the X and Z axes is reached.
//The prefabs to use in the generation
//The actual tiles
var DungeonPartsToUse : GameObject [];
//The outer wall
var WallPiece : GameObject;
//Size of the prefabs. Must be set to the prefab's side length
var PartSize : int;
//Length(x) and width(y) of the dungeon to be generated
var DungeonSizeX : int = 3;
var DungeonSizeY : int = 3;
//The pieces to use
private var PieceNumber : int;
//Current piece to instantiate
private var CurrentPiece : GameObject;
function Start() {
//Tells us if we have set the size of the part. Without this, dungeons would generate oddly
if(PartSize > 0){
GenerateMaze();
}
else {
Debug.LogError("PartSize must not be 0");
}
}
//Function to generate maze
function GenerateMaze() {
for (x = 0; x < DungeonSizeX*PartSize; x +=PartSize) {
for (y = 0; y < DungeonSizeY*PartSize; y +=PartSize) {
PieceNumber = Random.Range(0, DungeonPartsToUse.length);
CurrentPiece = DungeonPartsToUse[PieceNumber];
Instantiate(CurrentPiece, Vector3 (x, 0, y), Quaternion.identity);
}
}
//Then generates the outer wall
GenerateWall();
}
//Function to generate wall around the edges of the maze
function GenerateWall() {
for (n = 0; n < DungeonSizeX*PartSize; n += PartSize){
Instantiate(WallPiece, Vector3 (n, 0, (-(0.5*PartSize) + 1)), Quaternion.identity);
Instantiate(WallPiece, Vector3 (n, 0, (DungeonSizeX*PartSize) -((0.5*PartSize) + 1)), Quaternion.identity);
}
for (e = 0; e < DungeonSizeY*PartSize; e += PartSize){
Instantiate(WallPiece, Vector3 (-(0.5*PartSize - 1), 0, e), Quaternion.Euler(0,90,0));
Instantiate(WallPiece, Vector3 ((DungeonSizeX*PartSize) -(0.5*PartSize + 1), 0, e),Quaternion.Euler(0,90,0));
}
}