• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 03, 2018 at 07:43 PM by technorocker for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by technorocker · Aug 01, 2018 at 08:47 PM · collidersrotate objectdoorsrooms

Room rotation for door alignment

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. alt text

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[i].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);
         }

room-setup.png (9.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by technorocker · Aug 03, 2018 at 07:42 PM

I reached out to a developer that had a dungeon generation code that had his rooms line up in it and he shared his snippet of code that did that. I would share but didn't ask if that he would be ok with that.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need to make a door open only when the player's essence has reached a certain level 0 Answers

physics.OverlapSphere colliders 1 Answer

Opening Multiple Doors 2 Answers

looking for help with item pickups 1 Answer

How do I rotate an object around a certain point? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges