- Home /
Trying to make a procedurally generated dungeon (3D), but rooms won't spawn
I've got the basic logic down, but I'm having trouble implementing it. My idea is that each room/piece has up to 4 spawn points attached to it that indicate where a opening or connector to another room is. Depending on the direction, they're labelled 1-4 for North, South, etc.
I have a box collider attached at the actual openings of the models to check if it's already touching another room, which would mark the opening as being inaccessible and not spawning another room.
extra reference screenshots
much appreciated in advance!
Do you have rigid bodies on the rooms? IsAccessible is false until a trigger occuers and will prevent spawning. Do your objects fall into this matrix properly? https://docs.unity3d.com/Manual/CollidersOverview.html
what is "roomStock" if it's a class, it should be RoomStock (cap).
Answer by fatalityfun · Jul 07, 2021 at 10:58 PM
I found a fix for the issue. My rooms being spawned were being set to an IF statement that essentially never became true due to the setup of my world. Changing the IF statement to a WHILE loop bound to a variable that counted down each loop fixed it - even though I found even more issues later, it fixed the problem.
Answer by THICC_GANDHI · Jul 07, 2021 at 03:35 PM
You write isAccessible = false
and the statement of your while loop checks if isAccessible
is true. You get just one loop and then it stops, so only one room will spawn.
Thank you for that, I hadn't noticed. I edited it to
void Update()
{
if (openingDirection > 0)
{
rand = Random.Range(0, stock.roomList.Length);
while (isAccessible == true)
{
GameObject spawnedRoom = Instantiate(stock.roomList[rand], transform.position, stock.roomList[rand].transform.rotation);
spawnedRoom.transform.eulerAngles = new Vector3(0f, 90f, 0f);
if(rand <= 0)
{
isAccessible = false;
}
}
}
}
However, in either result no rooms spawn at all. Just the basic room I have set in the image above.