Search Unity

Tiles for room spawning are not correctly spawning walls in the right direction

Discussion in 'Scripting' started by Frostbite23, Nov 14, 2015.

  1. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Heller, I'm currently making a room spawning system. Currently right now I'm working on the individual tile for specific reasons. In the past my room spawning system worked nicely thought it was only spawning a rectangular room of any width and length, however some of the walls would always change position no matter what i did to them. If the room is 2 tiles wide the walls might be at 1 tile wide, if the room is 6 tiles length the walls would spawn really far from where they are supposed to be. I tried to figure out a formula so if the room was any length or width, the walls would just basically adapt and spawn where they should be. I had no luck with it. This room spawning however was not entirely what i wanted since I wanted irregularly shaped rooms, not big rectangle ones.

    So I came up with the solution to just spawn the walls per tile, its not the ideal solution (for some reason, you guys are very picky on something). The way it works is that I have an individual tile, and then I have triggers on the front, back, left, and right of the tile that will detect wether or not the tile is next to another tile or next to nothing. If one of the triggers don't detect any tile then it will spawn a wall at that specific direction. If the triggers don't detect anything at all then that means the tile is by itself and then it will destroy itself. If the triggers detect a tile in every direction then no walls will be spawned.

    The way its set up is that each of the triggers have a script, and if they detect something then it will set a variable in the main tile script.

    So the problem I have is that if I have a tile next to my main one, it will spawn and get rid of the wall in-between them. Doing it like a should in this image

    Screen Shot 2015-11-14 at 12.21.34 PM.png

    However, If I add 3 or more tiles, the tile next to the 3rd one will spawn a wall instead of not spawning one. Heres an Image to show the current issue (hard to explain)

    Screen Shot 2015-11-14 at 12.21.57 PM.png

    Now it took me a while to realize what was going on, until I moved around and saw this

    Screen Shot 2015-11-14 at 12.26.12 PM.png

    The walls are not spawning the right direction, I thought they were all just spawning but from here, it looks like the triggers are not properly setting the right booleans to the main tile. I checked each direction one by one in the main tile script and they are all in the right direction. Im stumped I cannot figure out what is going on.

    Heres my tile script
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var left = false;
    4. var right = false;
    5. var back = false;
    6. var forward = false;
    7.  
    8. var tileTriggers : GameObject[];
    9.  
    10. var wallBoundary : GameObject;
    11. var floor : GameObject;
    12. var XAndZOffset : float = 0.0f;
    13. var YOffset : float = 4.92f;
    14.  
    15. private var leftWall : GameObject;
    16. private var rightWall : GameObject;
    17. private var backWall : GameObject;
    18. private var fowardWall : GameObject;
    19.  
    20. Invoke("BuildWalls", 0.5);
    21.  
    22. function BuildWalls () {  
    23.     for(var i = 0; i < tileTriggers.Length; i++){
    24.         tileTriggers[i].SetActive(false);
    25.     }
    26.  
    27.     if(!left){
    28.         leftWall = new GameObject("leftWall");
    29.         var l1 : GameObject = Instantiate(wallBoundary, transform.position + Vector3(-XAndZOffset, YOffset, -5.99), Quaternion.Euler(90,90,0));
    30.         var l2 : GameObject = Instantiate(wallBoundary, transform.position + Vector3(-XAndZOffset, YOffset, 5.99), Quaternion.Euler(90,90,0));
    31.         leftWall.transform.parent = transform;
    32.         l1.transform.parent = leftWall.transform;
    33.         l2.transform.parent = leftWall.transform;
    34.     }
    35.  
    36.     if(!right){
    37.         rightWall = new GameObject("rightWall");
    38.         var r1 : GameObject = Instantiate(wallBoundary, transform.position + Vector3(XAndZOffset, YOffset, -5.99), Quaternion.Euler(90,-90,0));
    39.         var r2 : GameObject = Instantiate(wallBoundary, transform.position + Vector3(XAndZOffset, YOffset, 5.99), Quaternion.Euler(90,-90,0));
    40.         rightWall.transform.parent = transform;
    41.         r1.transform.parent = rightWall.transform;
    42.         r2.transform.parent = rightWall.transform;
    43.     }
    44.  
    45.     if(!back){
    46.         backWall = new GameObject("backWall");
    47.         var b1 : GameObject = Instantiate(wallBoundary, transform.position + Vector3(-5.99, YOffset, -XAndZOffset), Quaternion.Euler(90,0,0));
    48.         var b2 : GameObject = Instantiate(wallBoundary, transform.position + Vector3(5.99, YOffset, -XAndZOffset), Quaternion.Euler(90,0,0));
    49.         backWall.transform.parent = transform;
    50.         b1.transform.parent = backWall.transform;
    51.         b2.transform.parent = backWall.transform;
    52.     }
    53.  
    54.     if(!forward){
    55.         fowardWall = new GameObject("forwardWall");
    56.         var f1 : GameObject = Instantiate(wallBoundary, transform.position + Vector3(-5.99, YOffset, XAndZOffset), Quaternion.Euler(90,180,0));
    57.         var f2 : GameObject = Instantiate(wallBoundary, transform.position + Vector3(5.99, YOffset, XAndZOffset), Quaternion.Euler(90,180,0));
    58.         fowardWall.transform.parent = transform;
    59.         f1.transform.parent = fowardWall.transform;
    60.         f2.transform.parent = fowardWall.transform;
    61.     }
    62.  
    63.     //if(!left && !right && !back && !forward)
    64.         //DestroyObject(gameObject);
    65.      
    66.     //var floor : GameObject = Instantiate(floor, transform.position + Vector3(0, -1.21, 0), Quaternion.Euler(0,0,0));
    67.  
    68. }
    and the script on my Triggers

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var leftt = false;
    4. var rightt = false;
    5. var backk = false;
    6. var forwardd = false;
    7. var tile : GameObject;
    8. private var base : Tile;
    9.  
    10. function Start () {
    11.     base = tile.GetComponent(Tile);
    12. }
    13.  
    14. function OnTriggerStay (other : Collider) {
    15.     if(leftt)//left
    16.         base.left = true;
    17.     else
    18.         base.left = false;
    19.  
    20.     if(rightt)//right
    21.         base.right = true;
    22.     else
    23.         base.right = false;
    24.  
    25.     if(backk)//back
    26.         base.back = true;
    27.     else
    28.         base.back = false;
    29.  
    30.     if(forwardd)//forward
    31.         base.forward = true;
    32.     else
    33.         base.forward = false;
    34. }
     
    Last edited: Nov 14, 2015
  2. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
  3. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
  4. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458