I have a long straight street of houses with a road in front of them, created by lining up my house prefabs and then tilting their container slightly so that the road goes at an angle across the screen. Looks lovely. However, I ultimately need the road to have corners, creating a zigzag pattern like this:
The houses are already broken up into groups in preparation for this. I tried simply alternating the direction that I rotated the different groups, which got me the correct directions but not the correct positioning - the segments going right ascend across the screen while the segments going left descend.
I suspect this is happening because the origin of all those road segments is still (0,0,0), but I’m not sure how to fix it. Offset mathematics is clearly not my strong suit!
Anyway, this is the code I’m working with at the moment:
this.housePrefab = housePrefab;
int stretchCount = 0; // A stretch is the span between two turns
int houseCount = 0;
float offsetX = 0;
bool goingRight = true;
Transform parent = new GameObject().transform;
while (houseCount < 30)
{
// Add house to the current stretch
GameObject house = GameObject.Instantiate(housePrefab, Vector3.right * offsetX, Quaternion.identity, parent);
// Calculate offset for next house
HouseUnit unit = house.GetComponent<HouseUnit>();
MeshRenderer renderer = unit.GetHouseBase().GetComponent<MeshRenderer>();
Vector3 size = renderer.bounds.size;
offsetX += size.x;
// Decide if we're finished with the current stretch
stretchCount++;
if (stretchCount >= 2)
{
int dieRoll = Random.Range(0, 100);
if (dieRoll >= 50)
{
// Rotate current stretch
if (goingRight)
{
parent.rotation = Quaternion.Euler(0, -45f, 0);
}
else
{
parent.rotation = Quaternion.Euler(0, 45f, 0);
}
goingRight = !goingRight;
// Start a new stretch
stretchCount = 0;
}
}
// If we're starting a new stretch, create a new parent to hold it
if (stretchCount == 0)
{
parent = new GameObject().transform;
parent.position = Vector3.zero;
parent.rotation = Quaternion.identity;
}
houseCount++;
}
How do I go about lifting those left turns into their proper place? Ideally they should overlap at the turns so that the road looks properly connected, but if I need to backfill the corners for the sake of keeping the maths simple I’m willing to do that too.