Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Map Generation from Prefab using Connectors?

Discussion in 'Scripting' started by JakeBilbe, Jun 13, 2015.

  1. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    I started off with a Tile Generator, but realized I couldn't achieve the effect I wanted. So I've created prefabs, two at the moment. A room with 3 "Connectors" and a Corridor with 2 "Connectors".

    The Connectors are basically GameObjects highlighted in red with a script attached, the script has a string for the type of connector, a bool to say if it's connected and a collisionDetection method to check if it's colliding with another Connector and sets connected to true.

    I have one Room already in the scene on runtime, I've set it so that when I press E it runs an IEnumerator that checks for all Connectors and then spawns and attaches the required part to the connector. The script only runs for Connectors that aren't connected to anything, because each prefab is 5x5 things got a bit confusing as the first Room is at position 0, 0, 0 but the first corridors need to be at 5, 0, 0 then 10, 0, 0 and 15, 0, 0. I'm able to spawn 3 sections how I want to before things go haywire because of the code I have.

    To start with I was setting the spawned pieces position to the connectors position then adding half of the spawned prefabs position, this worked for two sections but then for the third I had to use the parents position. But now by the time I reach the fourth section it's doubling the position... Where I need it to be 15, 0, 0 it's becoming 25, 0, 0 because the last connector is at position 12.5, 0, 0.

    Code (csharp):
    1.  
    2. void Generate() {
    3.         for (int i=0; i < Connectors.Length; i++) {
    4.             foreach(ConnectorType c in Connectors[i].GetComponentsInChildren<ConnectorType>()) {
    5.                 if(!c.isConnected) { //Check if the selected Connector isn't connected to anything
    6.                     string type = c.Connector; //Set type to equal the type of Connector
    7.                     if(type == "Room") { //If the current connector is a Room type, focus on Corridor
    8.                         //Spawn Corridor
    9.                         Vector3 cP = c.transform.position;
    10.  
    11.                         GameObject cC = Instantiate(Corridor[0], cP, Quaternion.identity) as GameObject;
    12.                         cC.transform.position = cP * 2;
    13.                         cC.transform.rotation = c.transform.rotation;
    14.                         c.isConnected = true;
    15.                     }
    16.  
    17.                     if(type == "Corridor") { //If the current connector is a Corridor type, focus on Room
    18.                         //Spawn Room
    19.                         Vector3 cP = c.transform.parent.position;
    20.  
    21.                         GameObject cC = Instantiate(Room[0], cP, Quaternion.identity) as GameObject;
    22.                         cC.transform.position = cP * 2;
    23.                         cC.transform.rotation = c.transform.rotation;
    24.                         c.isConnected = true;
    25.                     }
    26.                 }
    27.             }
    28.         }
    29.     }
    30.  
    The corridor to the far left needs to be at Position(15, 0, 0) not 25 but because the Connector is located at 12.5, 0, 0 it's multiplying by two.

    The problem is getting the objects to spawn a the right position so that the Connectors are connected to the correct placements, I think I'm going the wrong way about this by setting the position but I'm not too sure on how else to go about it. Because I'm setting the spawned Prefab to the world position of the connector then multiplying by 2 it's only going to cause a larger gap, if I was able to take away 2.5f from the right x/z it would work but I'm unable to.
     
    Last edited: Jun 13, 2015
  2. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    Spent a few hours on it, I've gotten it working now just need to add some cleanup code to delete duplicates and block off unconnected ends.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    Sounds like you're making progress. I want to throw out an idea: when you make your rooms (which I presume are made of Unity primitive cubes?), be sure to hang them all onto a hierarchy of GameObjects that are NOT scaled or rotated. Only scale/rotate/offset the final geometry "leaf" of each chunk prefab.

    That way you can also include empty game objects as individual leafs for the link points, and confidently back out the positions and offsets in a way that does not get conflated with scale/rotation, which would only affect the final geometry piece.