Question by
cameron_balmer · Oct 07, 2021 at 01:26 PM ·
scripting problemprefabscriptingbasicsprefab changing at runtimedimensions
Build a House using code and prefabs
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 textures etc and meshes. Ideally, I would like this to simply spawn a repeated version of the wall prefab... ie a wall of specified length "40" should spawn 40 wall prefabs side by side. Any help adjusting would be appreciated.
public void buildExterior() //Execute on submit button clicked
{
setLength();
setBreadth();
setHeight();
setWidth();
Debug.Log("Length" + length);
Debug.Log("Breadth" + breadth);
Debug.Log("Height" + height);
Debug.Log("Width" + width);
//Floor
GameObject floorGo = Instantiate(floorPrefab);
floorGo.transform.parent = transform;
Vector3 flooradjustedPosition = floorStartPosition;
floorGo.transform.Rotate(90, 0, 0);
//Walls
for (int i = -2; i < 2; i++)
{
GameObject go = Instantiate(wallPrefab);
go.transform.parent = transform;
Vector3 scale = Vector3.one;
Vector3 walladjustedPosition = wallsStartPosition;
float sign = Mathf.Sign(i);
if ((i * sign) % 2 == 0)
{
//Breadth Walls
walladjustedPosition.x += (length * sign) / 2;
scale.x *= breadth + 1;
scale.y = height;
scale.z *= breadth + width;
go.transform.Rotate(0, 90, 0);
}
else
{
//Length Walls
walladjustedPosition.z += (breadth * sign) / 2;
scale.x *= length;
scale.y = height;
scale.z *= breadth + width;
}
walladjustedPosition.y += height / 2;
go.transform.localScale = scale;
go.transform.localPosition = walladjustedPosition;
walls.Add(go);
//Flooring Scale
scale.x = length;
scale.y = height / 2;
scale.z = breadth;
floorGo.transform.localScale = scale;
floorGo.transform.localPosition = flooradjustedPosition;
flooring.Add(floorGo);
}
}
Comment