我试图让Node成为世界空间中的一个位置。节点以网格方式排列
下面是节点网格每个大网格编号为0 - 3的图像 由50 x 50网格节点组成。
每个网格中的红点表示在点击世界中的某个点时检查了哪些节点。
对于标记为0的大网格,每个节点都可以正确访问
网格1具有正确的X值(从左到右位置),但Z被夹在网格的顶部
网格2具有相反的位置,其中X值被钳位到顶部范围并且Z值是正确的
以下是来自世界位置的网格代码
public Node NodeFromWorldPoint(Vector3 worldPosition)
{
//Gets the Large grid number 0-3
float Largex = worldPosition.x / (4 * gridWorldSize.x);
float LargeZ = worldPosition.z / (4 * gridWorldSize.z);
Largex = Mathf.Clamp01(Largex);
LargeZ = Mathf.Clamp01(LargeZ);
int lx = Mathf.RoundToInt((2 - 1) * Largex);
int lz = Mathf.RoundToInt((2 - 1) * LargeZ);
int tilenumber = lx * 2 + lz;
//get the position of the smaller grid
float percentX = worldPosition.x / (gridWorldSize.x * 2 );
float percentZ = worldPosition.z / (gridWorldSize.z * 2);
percentX = Mathf.Clamp01(percentX);
percentZ = Mathf.Clamp01(percentZ);
int x = Mathf.RoundToInt((gridSizeX - 1 ) * percentX);
int z = Mathf.RoundToInt((gridSizeZ - 1) * percentZ);
var gc = grids.ElementAt(tilenumber);
return gc[x, z];
}
答案 0 :(得分:0)
如果我理解正确,那么这应该对你有用。它包括节点的下边界并排除上边界..即如果你给出世界点2,0那么它将返回第二个节点。
public Node NodeFromWorldPoint(Vector3 worldPosition)
{
//Gets the Large grid number 0-3
int LargeX = (int)worldPosition.x / (2 * gridWorldSize.x);
int LargeZ = (int)worldPosition.z / (2 * gridWorldSize.z);
int tilenumber = LargeX*2 + LargeZ;
//get the position of the smaller grid
float percentX = worldPosition.x/gridWorldSize.x - LargeX;
float percentZ = worldPosition.z/gridWorldSize.z - LargeZ;
int x = (int)percentX/gridSizeX;
int x = (int)percentZ/gridSizeZ;
var gc = grids.ElementAt(tilenumber);
return gc[x, z];
}