- Home /
Why is this room always spawning on left side?
Hey. I've been coding this dungeon generator and the boss room (the very white room), is always spawning on the right. Here are screenshots of different instances. I mostly get between those two rooms even if i regenerate the dungeon 20 times.
Code:
using UnityEngine; using System.Collections;
public class SpawnRooms : MonoBehaviour {
int spawnerID;
public int maxXDEFAULT;
public int maxYDEFAULT;
public int dungeonSizeXDEFAULT;
public int dungeonSiZeYDEFAULT;
public GameObject room1;
public GameObject room2;
public GameObject room3;
public int DungeonID;
bool shouldSpawnBossRoom;
void Start () {
shouldSpawnBossRoom = true;
InstantiateRooms (maxXDEFAULT,maxYDEFAULT,dungeonSizeXDEFAULT,dungeonSiZeYDEFAULT);
}
void InstantiateRooms(int maxX, int maxY, int dungeonSizeX,int dungeonSizeY){
for(int iX =0; iX<maxX; iX+= dungeonSizeX ){
for(int iY = 0; iY< maxY; iY+= dungeonSizeY){
CheckWhatToSpawn(iX,iY);
}
}
}
void CheckWhatToSpawn(int x, int y){
if (shouldSpawnBossRoom == true) {
DungeonID = Random.Range (1, 4);
} else {
DungeonID = Random.Range(2,4);
}
if (DungeonID == 1 && shouldSpawnBossRoom == true) {
shouldSpawnBossRoom = false;
GameObject instance1 = (GameObject)Instantiate (room1, new Vector3 (x, y, 0), Quaternion.identity);
instance1.name = "Boss Room";
}
if (DungeonID == 2) {
GameObject instance2 = (GameObject)Instantiate (room2, new Vector3(x,y,0), Quaternion.identity);
instance2.name = "Room2";
}
if(DungeonID == 3 ){
GameObject instance3 = (GameObject)Instantiate(room3,new Vector3(x,y,0),Quaternion.identity);
instance3.name = "Room3";
}
}
}
Answer by RudyTheDev · Dec 24, 2014 at 05:17 PM
You are generating rooms sequentially through y and then x coordinates (for x, for y ..). So new rooms are created down first column, then down second, etc. Your boss room selection is the first time a random number is 1..4. After that you disable boss room spawning. So one would expect on average to see it about 70% (4/5 ^5) in the first column, which is what your observations seem to match.
I'm pretty new, but perhaps would randomly choosing two random numbers (random column and random row) and then spawning the boss room at those coordinates and then working out from there be better?
Thanks a lot $$anonymous$$. Your answer was perfect :D
What sdsanft suggests is probably the most straight-forward way to ensure the boss room is truly randomly placed. It does involve a different approach that the current one, as you'd have to remember where you spawned the boss room (or anything else, for that matter) besides "regular" rooms.