我一直在这里和Unity网站上寻找主题和解决方案,但自两个星期以来,我一直没有找到与我的问题类似的东西,并且一直在苦苦挣扎,所以请不要将此主题免费评分。 提前谢谢!
如下图所示,我的单元仅具有正交运动:
来自我的寻路脚本中的SymplifyPath()
:
IEnumerator FindPath(Vector3 startPos, Vector3 targetPos){
Vector3[] finalPath = new Vector3[0];
bool pathSuccess = false;
[...]//body
yield return null;
if (pathSuccess){
finalPath = RetracePath(startNode, targetNode);
}
pathQueue.FinishedProcessingPath(finalPath, pathSuccess);
}
Vector3[] RetracePath(Nodes startNode, Nodes endNode){
List<Node> path = new List<Node>();
Node currentNode = endNode;
while (currentNode != startNode){
path.Add(currentNode);
currentNode = currentNode.parent;
}
Vector3[] finalPath = SymplifyPath(path);
Array.Reverse(finalPath);
return finalPath;
}
Vector3[] SymplifyPath(List<Nodes> path){
List<Vector3> finalPath = new List<Vector3>();
Vector2 directionOld = Vector2.zero;
for(int i = 1; i < path.Count; i++){
Vector2 directionNew = new Vector2(path[i - 1].gridX - path[i].gridX, path[i - 1].gridZ - path[i].gridZ);{
finalPath.Add(path[i - 1].worldPosition);
}
directionOld = directionNew;
}
return finalPath.ToArray();
}
在waypoints.Add(path[i - 1].worldPosition);
行中,加上-1
可以生成类似俄罗斯方块的路径。如果没有-1
,路径的运动会更平滑,但是路径不再关心每个节点的中间,而是穿过墙壁。如果没有SimplyPath()
,原始路径将关心对角线,节点中间并且不穿过墙。这正是我在寻找的东西。
是否可以使用SimplifyPath()
将位于RetracePath
中的节点列表转换为数组,而无需创建新的简化路径?
我正在考虑类似:
Vector3[] SymplifyPath(List<Nodes> path){
List<Vector3> finalPath = new List<Vector3>();
Vector2 nodesListPath = Vector2.zero;
for(int i = 1; i < path.Count; i++)
{
finalPath(path[i].worldPosition)
}
finalPath = nodesListPath;
}
return finalPath.ToArray();
}
有什么想法吗?
您可以签出this website或this one将列表转换为数组。