Search Unity

Build a House using code and prefabs via scripting

Discussion in 'Scripting' started by CBam97, Oct 8, 2021.

  1. CBam97

    CBam97

    Joined:
    Jun 26, 2018
    Posts:
    6
    Hey guys, currently trying to devise a script to take user input in terms of length, breadth, height and width of walls and create a house using this. Right now the walls and floor work and give me the desired result however there is a crucial change I must make within the buildExterior method ; Right now the system instantiates 1 wall prefab and manipulated this using scale to provide the desired effect, this creates problems with meshes being stretched etc. Ideally, I would like this to simply spawn a repeated version of the wall prefab... ie a wall of user specified length "40" and breadth "30" should spawn 40 of the 1x1 wall prefabs side by side for length and 30 of them for breadth. Any help adjusting would be appreciated.

    Code (CSharp):
    1.  
    2. public void buildExterior() //Execute on submit button clicked
    3.      {
    4.          setLength();
    5.          setBreadth();
    6.          setHeight();
    7.          setWidth();
    8.          Debug.Log("Length" + length);
    9.          Debug.Log("Breadth" + breadth);
    10.          Debug.Log("Height" + height);
    11.          Debug.Log("Width" + width);
    12.          //Floor
    13.          GameObject floorGo = Instantiate(floorPrefab);
    14.          floorGo.transform.parent = transform;
    15.          Vector3 flooradjustedPosition = floorStartPosition;
    16.          floorGo.transform.Rotate(90, 0, 0);
    17.          //Walls
    18.          for (int i = -2; i < 2; i++)
    19.          {
    20.              GameObject go = Instantiate(wallPrefab);
    21.              go.transform.parent = transform;
    22.              Vector3 scale = Vector3.one;
    23.              Vector3 walladjustedPosition = wallsStartPosition;
    24.              float sign = Mathf.Sign(i);
    25.              if ((i * sign) % 2 == 0)
    26.              {
    27.                  //Breadth Walls
    28.                  walladjustedPosition.x += (length * sign) / 2;
    29.                  scale.x *= breadth + 1;
    30.                  scale.y = height;
    31.                  scale.z *= breadth + width;
    32.                  go.transform.Rotate(0, 90, 0);
    33.              }
    34.              else
    35.              {
    36.                  //Length Walls
    37.                  walladjustedPosition.z += (breadth * sign) / 2;
    38.                  scale.x *= length;
    39.                  scale.y = height;
    40.                  scale.z *= breadth + width;
    41.              }
    42.              walladjustedPosition.y += height / 2;
    43.              go.transform.localScale = scale;
    44.              go.transform.localPosition = walladjustedPosition;
    45.              walls.Add(go);
    46.              //Flooring Scale
    47.              scale.x = length;
    48.              scale.y = height / 2;
    49.              scale.z = breadth;
    50.              floorGo.transform.localScale = scale;
    51.              floorGo.transform.localPosition = flooradjustedPosition;
    52.              flooring.Add(floorGo);
    53.          }
    54.      }
     
    Last edited: Oct 8, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    There are fazillions of ways to approach this, ranging from re-UV-mapping after the stretch to get the textures back reasonable, to even rescaling the root objects and their interior meshes. My MakeGeo project has some example code to drive the UVs back to world scale, located in this file:

    https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/setuvtoworld/SetUVToWorld.cs

    The full MakeGeo project is there, and also presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo