- Home /
How can I offset blocks in a tower to build a Jenga-like structure?
I'm to the halfway point getting this worked out. Independently, if I build rows that face a singular direction the tower is generated fine. However, as soon as I rotate them - in this case via odd numbers, the tower falls apart... or just explodes. I know I need to place an offset on the x position, but I'm not sure what the best calculation for that would be. The tower structure is built from Vector3.zero in the world.
void buildTower() {
int counter = 0;
for (int y = 0; y < numY; y++) {
for (int x = 0; x < numX; x++) {
Vector3 pos;
Quaternion rot = Quaternion.identity;
Vector3 rotEuler;
//rotEuler = new Vector3(270, 0, 0);
//pos = new Vector3(x, y, 0) * spacing;
if (y % 2 != 0) {
rotEuler = new Vector3(270, 0, 0);
pos = new Vector3(x, y, 0) * spacing;
} else {
rotEuler = new Vector3(270, 270, 0);
pos = new Vector3(x, y, 0) * spacing;
}
rot.eulerAngles = rotEuler;
GameObject plank = Instantiate(plankObject) as GameObject;
plank.transform.parent = Tower.transform;
plankObject.transform.position = pos;
plankObject.transform.rotation = rot;
}
}
}
It could be something as simple as changing the rotation before you change the position, so switching lines 29 and 30 might fix it. It's probably placing the planks then spinning them around, colliding with the others.
Same effect. The issue is the offset here, not the direction they are rotated.
Ah, yeah, when you were stacking them without rotating them, you only had to increase the x value by the width of the planks, but if you have a plank turned, you then need to increase the x to the length of it. At line 20, try replacing x with x + (length of plank - width of plank).
Your answer
![](https://koobas.hobune.stream/wayback/20220613130501im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can customize which prefabs are instantiated in a grid? 1 Answer
Efficient grid system 1 Answer
Problems creating a grid in Runtime 1 Answer
MaK like building script help... 0 Answers