int index = 0; RaycastHit hit; Vector3 startPos = new Vector3(0, 0, 0); GameObject cube = new GameObject(); cube.name = "cube"; cube.transform.localScale = new Vector3(this.m_GridSize, this.m_BaseHight, this.m_GridSize); cube.transform.SetParent(node.transform); for (int i = GirdLength-1; i >= 0; i--) { Vector3 pos = startPos; for (int j = 0; j < GirdWidth; j++) { cube.transform.localPosition = pos; pos.x += m_GridSize; Ray ray = new Ray(cube.transform.position + Vector3.up * 1000, Vector3.down); bool res = Physics.Raycast(ray, out hit, 1000f); if (res) { if (hit.transform.gameObject.layer == 10) { var height = ((hit.point.y - m_BaseHight) * HEIGHT_FLOAT_VAL); mapHeightArr[index++] = (short)height; } else { mapHeightArr[index++] = 0; } } else { mapHeightArr[index++] = 0; } IsFinsh = true; } startPos.z += m_GridSize; } 这段代码如何改成从左上角(0,0,0,)开始遍历格子
时间: 2024-03-16 22:46:45 浏览: 102
Unity3D脚本:快速判断手势是上下左右2
可以将代码中的两层循环的顺序颠倒一下,即将外层循环从 `GirdLength-1` 到 0,内层循环从 0 到 `GirdWidth-1`,这样就可以从左上角开始遍历格子了。
改动后的代码如下:
```csharp
int index = 0;
RaycastHit hit;
Vector3 startPos = new Vector3(0, 0, 0);
GameObject cube = new GameObject();
cube.name = "cube";
cube.transform.localScale = new Vector3(this.m_GridSize, this.m_BaseHight, this.m_GridSize);
cube.transform.SetParent(node.transform);
for (int i = 0; i < GirdLength; i++)
{
Vector3 pos = startPos;
for (int j = 0; j < GirdWidth; j++)
{
cube.transform.localPosition = pos;
pos.x += m_GridSize;
// 射线检测
Ray ray = new Ray(cube.transform.position + Vector3.up * 1000, Vector3.down);
bool res = Physics.Raycast(ray, out hit, 1000f);
if (res)
{
if (hit.transform.gameObject.layer == 10)
{
var height = ((hit.point.y - m_BaseHight) * HEIGHT_FLOAT_VAL);
mapHeightArr[index++] = (short)height;
}
else
{
mapHeightArr[index++] = 0;
}
}
else
{
mapHeightArr[index++] = 0;
}
IsFinsh = true;
}
startPos.z += m_GridSize;
}
```
阅读全文