我使用嵌套的for循环来创建六边形的网格。这将创建一个正方形的网格:
for (int z = 0; z < gridSize; z++)
{
for (int x = 0; x < gridSize; x++)
{
// creates verts for a hexagon shape which later form a mesh
// x and z form the basis of the Vector3 position of the center
// of each hexagon
CreateCell(x, z);
}
}
我已经在图像上绘制了z和x的起始和结束值。我希望的是将网格本身也呈六边形形状:
我想已经找到了x的限制:
int greaterThan = Mathf.RoundToInt(gridSize/ 3) - 1;
int lessThan = width - greaterThan;
我认为x只有在z等于gridSize的一半向上取整时(例如示例中的0和6),才应该达到最小值和最大值,但我可能是错的!
我尝试在循环中放置了许多if语句,但很快就变得过于复杂了,我认为必须有更“数学”的方法来解决它,但可惜我不太懂数学!
你有什么办法可以编写一个循环以形成所需的模式吗?