Hello ladies and gentlemen, the problem i face today is that i have a building that has its interiors randomly spawned (the buildings overall shape is not important) and by interiors i mean corridors, rooms, etc. The issue is that these “interiors” spawn into another one. That is to say that it goes around a corner and spawns a corridor through another corridor. my initial plan is to detect when this happens and to remove the offending corridor and place a wall or something to end the corridor nicely so that no colliding occurs. i just have no idea how to detect if the interiors have collided as soon as they are placed. i know that as soon as i find the solution i can round of the topic nicely. Also i code in C#.
Thanks for your time guys
You are developing a system very similar to what I have recently got working after about 7 iterations and 3 months of working on it.
In simple terms, in my game I have various rooms which are constructed procedurally with doors in them. Opening a door chooses a new random room to instantiate.
I very quickly got to the point in developing this where my rooms would occlude each other because an existing room already was where a door opened up to. (and this is a 3D game, so that’s a real GDMF’R of a problem to solve!) Two of MANY related issues I had to solve were what to do when an existing door which has not been opened yet is occluded by a room spawned into valid space, and what to do when a newly spawned door happens to match the other side of a door which exists in the game.
To solve the room occlusion problem, since I am already constructing ‘skeleton’ prefabs with placement markers for tiles, from which floor, ceiling and wall tiles are placed, I just created a collider for my skeleton prefab. Since the skeleton prefab doesn’t look like anything in the game I just go ahead and spawn it in and check for occlusion of its collider. If it occluded, I don’t Build() the room and instead delete the skeleton prefab and try the next one until either a room is found which fits or the door breaks and fails to open at all (don’t want the player stepping off the level). Another part of this was choosing when to spawn doors intelligently - when the code comes to determining if a door should be spawned on a door marker, it iterates through all available prefabs valid to the current theme and if it does not find any room at all that will fit, the game doesn’t place a door there that will inevitably fail.
Let me know if you have specific questions regarding procedural, room-based level generation. As of today I’ve solved every problem with it including having doors randomly available but also ensuring that there is always a door for the player to open.
The names of some functions I have written may be helpful;
CheckForSpace(prefab) - checks to see if space is available for a prefab by instantiating it and checking for occlusion of its collider - you could write a version of this which uses a prefab where the visible objects are disabled, but the collider is not, and turn the visible objects on when you find the prefab that fits. If your game is at all like mine then the player can’t see from behind the door anyway and even in my game with a lot, lot to do when a door panel is activated the whole processes of deciding on a room and instantiating and destroying dozens of prefabs takes a couple hundred MS max and that includes recursively performing that operation on every door which could potentially be spawned in the room that was finally chosen to determine if that’s a valid location for a door. A lot to do! but Unity is fast.
CheckForExistingDoors(GameObject, float range) - checks for doors near a GO within a range
A final word of caution - do NOT go down the road of using OverlapBox to check for space! You will make yourself absolutely crazy, trust me because version 5 or so was functional with that methodology but it’s INCREDIBLY unscalable (what the hell to do when there’s a room that’s not a single square?) and INTENSELY difficult to debug (you can NEVER see the overlap box itself and the box is placed by CENTER omg kill me even thinking about it)! Use prefabs with existing colliders and proper parent placement and rotation instead.
I wish you luck on your journey down this road. I’m at a couple thousand lines of code now but that includes things like themes, door insurance, and the entire fully-functional procedural room + procedural level + procedural props for both types system + AI enemies + AI companions + player weapons, health, wind, fire, all that kind of thing. And I’m twice that at least from the finished game and I don’t even have an inventory or items in it yet!
I am guessing that for your prefabs of the interiors that you are spawning you can add an extra collider on them which has the ‘Is Trigger’ checkbox checked.
Then do something like this.
void OnTriggerStay(Collider corridor)//Or maybe just OnTriggerEnter
{
Instantiate (wall);
Destroy (corridor.gameObject);
}
But I think that will only detect the collisions as long as your instantiated interiors aren’t both kinematic?