Unity3D [中级] 如何将一个List<nodes>.toArray转换? (寻找路径)

Unity3D [intermediate] how to convert a List<nodes> .toArray? (pathfinding)

[编辑] 我的问题并没有通过将 List .toArray 转换来解决,但是您在@KBaker 给出的答案中找不到解决方法。祝你好运!

---------------------------------------- ---------------------------------------------- --------------------------------------

我一直在此处和 Unity 网站上寻找主题和解决方案,但我没有找到与我的问题类似的东西,两周以来我一直在苦苦挣扎,所以请不要免费给这个主题打分。 提前致谢!

如下图所示,我的单元只有正交运动:

来自我的 Pathfinding 脚本中的 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 or this one 将您的列表转换为数组。