I was stuck on spawning a new room as the player leaves the current room through a door using a node/grid system. I figured out how to properly instantiate a room in the neighbor node. now I’m having a problem on how to make the room rotate to line up the doors that are in the room.
So how I have it setup is a list of prefab rooms all the same size 10x10. I have a separate GameObject with 4 children of n,e,s,w doors placed off the center point which lines up with the room center point. On the room I have another NoDoorsSystem of just colliders that trigger the door system and if any noDoors collide with door that door object is disabled. Now my problem is getting the room to rotate so that the doors that ARE active will line up with the door that the player is exiting from.
Heres my Spawn Room Code…
void SpawnRandomRoom(int whichRoom, List<GameObject> roomList)
{
// Find current node that room is on and get neighbours
Vector3 curRoomWorldPos = transform.position;
//Debug.Log("world pos " + curRoomWorldPos.x+", "+ curRoomWorldPos.y+", "+curRoomWorldPos.z);
Node curRoomNode = GridBase.singleton.GetNodeFromWorldPosition(curRoomWorldPos);
//Debug.Log("Current node is:" + curRoomNode.x + "," + curRoomNode.y + "," + curRoomNode.z);
// Depending on which door Collider the player touches spawn room in the direction.
if (gameObject.transform.parent.name == "Door West" && roomList[whichRoom] != null)
{
Vector3 nodeWorldCoordinates = GridBase.singleton.GetWorldCoordinatesFromNode(curRoomNode.x - 1, curRoomNode.y, curRoomNode.z);
//Debug.Log("The World Coordinates from node position is: " + nodeWorldCoordinates);
// Instantiate Door system minus door closest to current door
GameObject newRoomDoors = Instantiate(Resources.Load("Doors/Door System"), new Vector3(nodeWorldCoordinates.x, nodeWorldCoordinates.y, nodeWorldCoordinates.z), Quaternion.identity) as GameObject;
newRoomDoors.name = Resources.Load("Doors/Door System").name;
newRoomDoors.transform.Find("Door East").gameObject.SetActive(false);
// Spawn new random room from list
GameObject newRoom = Instantiate(roomList[whichRoom], new Vector3 (nodeWorldCoordinates.x, nodeWorldCoordinates.y, nodeWorldCoordinates.z), Quaternion.identity) as GameObject;
newRoom.name = roomList[whichRoom].name;
List<GameObject> noDoorObj = new List<GameObject>();
bool foundDoor = false;
while (!foundDoor)
{
GameObject noDoorSystem = newRoom.transform.Find("NoDoorsSystem").gameObject;
GameObject[] noDoorObjects = noDoorSystem.GetComponentsInChildren<GameObject>();
//foreach (GameObject child in noDoorObjects)
//{
// if (child.activeSelf)
// {
// noDoorObj.Add(child);
// }
//}
for (int i = 0; i < noDoorObj.Count; i++)
{
if (noDoorObjects*.activeSelf)*
{
newRoom.transform.eulerAngles = new Vector3(0, 90, 0);
}
}
}
//// Rotate room to match door entrance
//GameObject noDoorEast = GameObject.Find(“NoDoorEast”);
//if (noDoorEast.activeSelf)
//{
// newRoom.transform.eulerAngles = new Vector3(0, 90, 0);
// Debug.Log(newRoom.name + " was rotated 90 degress");
//}
// Add collider in center of room for unit position labeling
newRoomDoors.transform.parent = newRoom.transform;
}
else if (gameObject.transform.parent.name == “Door East” && roomList[whichRoom] != null)
{
Vector3 nodeWorldCoordinates = GridBase.singleton.GetWorldCoordinatesFromNode(curRoomNode.x + 1, curRoomNode.y, curRoomNode.z);
//Debug.Log("The World Coordinates from node position is: " + nodeWorldCoordinates);
// Instantiate Door system minus door closest to current door
GameObject newRoomDoors = Instantiate(Resources.Load(“Doors/Door System”), new Vector3(nodeWorldCoordinates.x, nodeWorldCoordinates.y, nodeWorldCoordinates.z), Quaternion.identity) as GameObject;
newRoomDoors.name = Resources.Load(“Doors/Door System”).name;
newRoomDoors.transform.Find(“Door West”).gameObject.SetActive(false);
GameObject newRoom = Instantiate(roomList[whichRoom], new Vector3(nodeWorldCoordinates.x, nodeWorldCoordinates.y, nodeWorldCoordinates.z), Quaternion.identity) as GameObject;
newRoom.name = roomList[whichRoom].name;
// Rotate room to match door entrance
GameObject noDoorWest = GameObject.Find(“NoDoorWest”);
if (noDoorWest.tag == “NoDoor”)
{
newRoom.transform.eulerAngles = new Vector3(0, 90, 0);
Debug.Log(newRoom.name + " was rotated 90 degress");
}
newRoomDoors.transform.parent = newRoom.transform;
}
else if (gameObject.transform.parent.name == “Door South” && roomList[whichRoom] != null)
{
Vector3 nodeWorldCoordinates = GridBase.singleton.GetWorldCoordinatesFromNode(curRoomNode.x, curRoomNode.y, curRoomNode.z - 1);
//Debug.Log("The World Coordinates from node position is: " + nodeWorldCoordinates);
// Instantiate Door system minus door closest to current door
GameObject newRoomDoors = Instantiate(Resources.Load(“Doors/Door System”), new Vector3(nodeWorldCoordinates.x, nodeWorldCoordinates.y, nodeWorldCoordinates.z), Quaternion.identity) as GameObject;
newRoomDoors.name = Resources.Load(“Doors/Door System”).name;
newRoomDoors.transform.Find(“Door North”).gameObject.SetActive(false);
GameObject newRoom = Instantiate(roomList[whichRoom], new Vector3(nodeWorldCoordinates.x, nodeWorldCoordinates.y, nodeWorldCoordinates.z), Quaternion.identity) as GameObject;
newRoom.name = roomList[whichRoom].name;
// Rotate room to match door entrance
GameObject noDoorNorth = GameObject.Find(“NoDoorNorth”);
if (noDoorNorth.tag == “NoDoor”)
{
newRoom.transform.eulerAngles = new Vector3(0, 90, 0);
Debug.Log(newRoom.name + " was rotated 90 degress");
}
newRoomDoors.transform.parent = newRoom.transform;
}
else if (gameObject.transform.parent.name == “Door North” && roomList[whichRoom] != null)
{
Vector3 nodeWorldCoordinates = GridBase.singleton.GetWorldCoordinatesFromNode(curRoomNode.x, curRoomNode.y, curRoomNode.z + 1);
//Debug.Log("The World Coordinates from node position is: " + nodeWorldCoordinates);
// Instantiate Door system minus door closest to current door
GameObject newRoomDoors = Instantiate(Resources.Load(“Doors/Door System”), new Vector3(nodeWorldCoordinates.x, nodeWorldCoordinates.y, nodeWorldCoordinates.z), Quaternion.identity) as GameObject;
newRoomDoors.name = Resources.Load(“Doors/Door System”).name;
newRoomDoors.transform.Find(“Door South”).gameObject.SetActive(false);
GameObject newRoom = Instantiate(roomList[whichRoom], new Vector3(nodeWorldCoordinates.x, nodeWorldCoordinates.y, nodeWorldCoordinates.z), Quaternion.identity) as GameObject;
newRoom.name = roomList[whichRoom].name;
// Rotate room to match door entrance
GameObject noDoorSouth = GameObject.Find(“NoDoorSouth”);
if (noDoorSouth.tag == “NoDoor”)
{
newRoom.transform.eulerAngles = new Vector3(0, 90, 0);
Debug.Log(newRoom.name + " was rotated 90 degress");
}
newRoomDoors.transform.parent = newRoom.transform;
}
// Destroy this script once used to generate room as to not generate again on next player movement
Destroy(this);
// Remove Spawned room from list of rooms
roomList.RemoveAt(whichRoom);
// Update LevelManager Room List
LevelManager leveManager = LevelManager.singleton;
leveManager.LoadObstacles(GridBase.singleton);
}