Dungeon Generation Script for others reference or for feedback/tips

Hey guys! I’ve been working on a game idea of mine - but for that idea I need dungeon generation.

I’m NOT asking for help - just showcasing my algorithm/approach to see what you guys think.

I am aware that there is plenty of dungeon generation resources out there - I have been using them. But if you happen to know of a really good one you want to make sure I know about or have any good tips/resources - I will gladly check them out and appreciate the help !

Anyway, I thought I’d attach my script to see what you guys think.

using UnityEngine;
using System.Collections;

public class RandomRoomBuilder : MonoBehaviour
{
	public GameObject Handle;
	public GameObject Placer;
	public GameObject Wall;
	GameObject placedTile;
	GameObject placedWall;

	// Use this for initialization
	void Start () 
	{
		float roomLength = Random.Range(2, 20);
		float roomWidth = Random.Range(2, 20);
		roomLength = Mathf.Round(roomLength);
		roomWidth = Mathf.Round(roomWidth);

		float placerCurrentWidthPos = Handle.transform.position.x;
		float placerCurrentLengthPos = Handle.transform.position.z;

		for(int y = 0; y < roomLength; y++)
			{
				for(int i = 0; i < roomWidth; i++)
					{
						placerCurrentWidthPos = Mathf.Round (placerCurrentWidthPos);
						placerCurrentLengthPos = Mathf.Round (placerCurrentLengthPos);
								
						Vector3 tilePos = Handle.transform.position;
						
						placedTile = Object.Instantiate(Placer, tilePos, Placer.transform.rotation) as GameObject;
						
						float placedTileWidthPos = placedTile.transform.position.x;
						float placedTileLengthPos = placedTile.transform.position.z;
						float placedTileHeight = placedTile.transform.position.y;
						
						placedTileWidthPos = Mathf.Round(placedTileWidthPos);
						placedTileLengthPos = Mathf.Round(placedTileLengthPos);
						
						placedTile.transform.name = "Tile (" + placedTileWidthPos + "),(" + placedTileLengthPos + ")";

				/////////// code below assigns corners and edges

								if(placedTileWidthPos == 0)
									{
										placedTile.tag = "edge";
									}
								
								if(placedTileLengthPos == 0)
									{
										placedTile.tag = "edge";
									}

								if(placedTileWidthPos == roomWidth - 1)
									{
										placedTile.tag = "edge";
									}
								
								if(placedTileLengthPos == roomLength - 1)
									{
										placedTile.tag = "edge";
									}
								
								if(placedTile.transform.position == new Vector3(0, placedTileHeight, 0))
									{
										placedTile.tag = "corner";
									}

								if(placedTile.transform.position == new Vector3(0, placedTileHeight, roomLength-1))
									{
										placedTile.tag = "corner";
									}

								if(placedTile.transform.position == new Vector3(roomWidth-1, placedTileHeight, 0))
									{
										placedTile.tag = "corner";
									}

								if(placedTile.transform.position == new Vector3(roomWidth-1, placedTileHeight, roomLength-1))
									{
										placedTile.tag = "corner";
									}
				///////// code above assigns corners and edges		
						
						Handle.transform.Translate (1, 0, 0, Space.World);
						
					}
				
				Handle.transform.Translate(-roomWidth, -1, 0);
			}
/////////// code below places walls
			Vector3 wallStart = new Vector3(-1, 0, -1);
			Handle.transform.position = wallStart;
			
			for(int i = -2; i < roomLength; i++)
				{
					Vector3 wallPos = Handle.transform.position;
					
					placedWall = Object.Instantiate(Wall, wallPos, Wall.transform.rotation) as GameObject;
					
					float placedWallWidthPos = placedWall.transform.position.x;
					float placedWallLengthPos = placedWall.transform.position.z;
					float placedWallHeightPos = placedWall.transform.position.y;
					
					placedWallWidthPos = Mathf.Round(placedWallWidthPos);
					placedWallLengthPos = Mathf.Round(placedWallLengthPos);
					
					placedWall.transform.name = "Wall (" + placedWallWidthPos + "),(" + placedWallLengthPos + ")";
					
					Handle.transform.Translate (0, 0, 1, Space.World);

				}
			
			Handle.transform.Translate (1, 0, -1, Space.World);

		for(int i = 0; i < roomWidth; i++)
				{
					Vector3 wallPos = Handle.transform.position;
					
					placedWall = Object.Instantiate(Wall, wallPos, Wall.transform.rotation) as GameObject;
					
					float placedWallWidthPos = placedWall.transform.position.x;
					float placedWallLengthPos = placedWall.transform.position.z;
					float placedWallHeightPos = placedWall.transform.position.y;
					
					placedWallWidthPos = Mathf.Round(placedWallWidthPos);
					placedWallLengthPos = Mathf.Round(placedWallLengthPos);
					
					placedWall.transform.name = "Wall (" + placedWallWidthPos + "),(" + placedWallLengthPos + ")";
					
					Handle.transform.Translate (1, 0, 0, Space.World);
					
				}

		for(int i = -1; i < roomLength; i++)
				{
					Vector3 wallPos = Handle.transform.position;
					
					placedWall = Object.Instantiate(Wall, wallPos, Wall.transform.rotation) as GameObject;
					
					float placedWallWidthPos = placedWall.transform.position.x;
					float placedWallLengthPos = placedWall.transform.position.z;
					float placedWallHeightPos = placedWall.transform.position.y;
					
					placedWallWidthPos = Mathf.Round(placedWallWidthPos);
					placedWallLengthPos = Mathf.Round(placedWallLengthPos);
					
					placedWall.transform.name = "Wall (" + placedWallWidthPos + "),(" + placedWallLengthPos + ")";
					
					Handle.transform.Translate (0, 0, -1, Space.World);
					
				}

		for(int i = -1; i < roomWidth; i++)
		{
			Vector3 wallPos = Handle.transform.position;
			
			placedWall = Object.Instantiate(Wall, wallPos, Wall.transform.rotation) as GameObject;
			
			float placedWallWidthPos = placedWall.transform.position.x;
			float placedWallLengthPos = placedWall.transform.position.z;
			float placedWallHeightPos = placedWall.transform.position.y;
			
			placedWallWidthPos = Mathf.Round(placedWallWidthPos);
			placedWallLengthPos = Mathf.Round(placedWallLengthPos);
			
			placedWall.transform.name = "Wall (" + placedWallWidthPos + "),(" + placedWallLengthPos + ")";
			
			Handle.transform.Translate (-1, 0, 0, Space.World);
			
		}

/////code above places walls
			

			

	}
	
	// Update is called once per frame
	void Update () 
	{
	
	
	}


}

I’m not sure how to upload a demo of it here yet - I’ll have to look around for how to do that. But it does indeed work the way it’s supposed to using my custom wall and tile prefabs in the “wall” public gameobject and “placer” public gameobject.

(I should probably revise the “placer” gameobject so it’s called “tile” so that I don’t confuse the instantiator and tile gameobjects)

Tried the script but got some strange ‘thing’ that didnt look like a dungeon or a room.

Look forward to the demo so I can see what it should look like.

Interesting…my guess is that I suited this code pretty heavily for the models/prefabs I’m using. The tiles and walls are all scaled for a perfect 1,1,1 unit to match the unity built in grid … So the only thing I could see going wrong is your prefabs aren’t scaled correctly.’

I am newish to the coding thing though - so i could be far off.

I’ll get a Demo up today man - would love to see other people looking at it.

Also - the “handle” is what moves around and places tiles at the handles current position . You need to have an instance of the handle made in the unity scene before you launch the script - otherwise there is no handle for the code and it has nothing to move around.

My handle instance in the scene is just my tile prefab named “handle” and a disabled mesh renderer and a rigid body as well as a mesh collider.

I was using a unity cube for tiles and walls but just scaled them on the x and y axis to make the tiles flat and the walls thin.

I didnt know what the handle was so just used a full cube.

I will try it again.

Here ya go man! Maybe this’ll help - if not I like to show off anyway :wink: