Question by
cyper43 · May 09, 2017 at 05:48 AM ·
randomnewbiegenerationprocedural generationdungeon
Dungeon Generator script
Greetings, i have some questions about a dungron generation script im making for a university project. The script is supposed to create a random dungeon but not spawn rooms on top of each other.
This is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelCreator : MonoBehaviour {
public GameObject startRoom;
public GameObject finalRoom;
public List <GameObject> roomList = new List<GameObject>();
public List<GameObject> levelList = new List<GameObject> ();
// private enum spawnPosition { Front, Back, Left, Right};
public int maxRooms;
// Use this for initialization
void Awake () {
Instanciator ();
}
//Adds Randomly rooms to the list and isntanciates them on the level
void Instanciator(){
for (int i = 0; i < maxRooms; i++) {
Vector3 spawnOffset = new Vector3 (20,0,0);
Vector3 nextPosition = new Vector3 ();
if (i == 0) {
Vector3 initialPos = new Vector3 (0, 0, 0);
GameObject newRoom = Instantiate (startRoom, initialPos, Quaternion.identity);
levelList.Add (newRoom);
} else if (i == maxRooms - 1){
nextPosition = levelList [i - 1].GetComponent<Room> ().GetRoomPosition () + spawnOffset;
GameObject newRoom = Instantiate(finalRoom, nextPosition, Quaternion.identity);
levelList.Add (newRoom);
}else{
nextPosition = levelList [i - 1].GetComponent<Room> ().GetRoomPosition () + spawnOffset;
int roomRandom = Random.Range (0, roomList.Count);
GameObject newRoom = Instantiate(roomList[roomRandom], nextPosition, Quaternion.identity);
levelList.Add (newRoom);
}
}
}
}
The spawnOffset is still not random so the dungeon is still a straight line. how can i check if the room next position is already taken by an object in the levelList?
Thank you
Comment
Your answer
Follow this Question
Related Questions
How to instantiate bunch of cubes randomly in particular direction? 0 Answers
How do I generate a grid of random shapes that tile perfectly? 0 Answers
How to make 2D TopDown chunks spawn and delete? 0 Answers
Help with simple generation problem 0 Answers
Generate Random Numbers a Distance Apart From Each Other 1 Answer