C# 在1个方向上绕障碍物寻路时,寻路会导致堆栈溢出

C# 在1个方向上绕障碍物寻路时,寻路会导致堆栈溢出,c#,path-finding,C#,Path Finding,我一直在做一个需要相当基本的寻路的项目,经过相当多的努力和时间,它几乎可以工作了。然而,有一个bug是持久的。当一个单元需要在障碍物上方的路径时,它没有问题,但当它需要在障碍物下方的路径时,它会导致堆栈溢出 守则: 单元班 public void drawMovePath(Tile tarTile) { for (int i = 0; i < moveableTiles.Count; i++) { moveableTiles[i].GetComponent<S

我一直在做一个需要相当基本的寻路的项目,经过相当多的努力和时间,它几乎可以工作了。然而,有一个bug是持久的。当一个单元需要在障碍物上方的路径时,它没有问题,但当它需要在障碍物下方的路径时,它会导致堆栈溢出

守则:

单元班

public void drawMovePath(Tile tarTile)
{
    for (int i = 0; i < moveableTiles.Count; i++) {
        moveableTiles[i].GetComponent<SpriteRenderer>().color = Color.gray;
    }
    path.Clear ();

    List<GameObject> availableTiles = new List<GameObject> ();
    List<int> hValues = new List<int> ();
    calcLists (availableTiles, hValues, tarTile.gameObject);

    curTile.GetComponent<Tile> ().drawMovePathInit (tarTile.gameObject, availableTiles, hValues, path, speed);


    for (int i = 0; i < path.Count; i++) {
        path[i].GetComponent<SpriteRenderer>().color = Color.green;
        print(path[i].GetComponent<Tile>().getHValue());
    }
    pathDrawn = true;
}
public void drawMovePath(瓷砖格子)
{
对于(int i=0;i
在瓷砖类中:

public void drawMovePathInit(GameObject tarTile, List<GameObject> allMoveableTiles, List<int> hValues, List<GameObject> path, int speed, int newHValue = 0)
{

    GameObject curTile = Player.instance.getSelectedUnit ().GetComponent<Unit> ().getCurTile ();
    curTile.GetComponent<Tile> ().setHValue (newHValue);
    path.Add (curTile);

    GameObject nextTile = new GameObject();

    int lowestH = 100000;
    for (int i = 0; i < curTile.GetComponent<Tile>().getSurTiles().Count; i++) {
        int index = allMoveableTiles.IndexOf(curTile.GetComponent<Tile>().getSurTiles()[i]);
        if (index != -1)
        {
            if (hValues[index] <= lowestH)
            {
                lowestH = hValues[index];
                nextTile = allMoveableTiles[index];
            }
        }
    }
    nextTile.GetComponent<Tile> ().drawMovePath (tarTile, allMoveableTiles, hValues, path, speed, curTile.GetComponent<Tile>().getHValue());

}


public void drawMovePath(GameObject tarTile, List<GameObject> allMoveableTiles, List<int> hValues, List<GameObject> path, int speedLeft, int newHValue)
{
    setHValue (newHValue + 10);
    path.Add (this.gameObject);

    if (this.gameObject == tarTile) {
        return;
    }
    if (speedLeft == 0) {
        return;
    }

    GameObject nextTile = new GameObject();

    int lowestH = 100000;
    for (int i = 0; i < surTiles.Count; i++) {
        int index = allMoveableTiles.IndexOf(surTiles[i]);
        if (index != -1)
        {
            if (hValues[index] <= lowestH)
            {
                lowestH = hValues[index];
                nextTile = allMoveableTiles[index];
            }
        }
    }
    nextTile.GetComponent<Tile> ().drawMovePath (tarTile, allMoveableTiles, hValues, path, speedLeft - 1, getHValue());

}
public void drawMovePathInit(游戏对象tarTile,列出所有MoveTableTiles,列出hValue,列出path,int speed,int newHValue=0)
{
GameObject curTile=Player.instance.getSelectedUnit().GetComponent().getCurTile();
curTile.GetComponent().setHValue(newHValue);
添加路径(curTile);
GameObject nextile=新GameObject();
int=100000;
对于(int i=0;iif(hValues[index]你到底在哪一行得到堆栈溢出?你能把你的问题缩小到一个例子吗?这里最明显,但是你试过使用调试器吗?看起来你有一个非常简单的例子,应该很容易找到。我不知道你在寻路时使用的是什么算法,但是*(例如,有关更多信息,请参阅维基百科文章)不会一直沿着一条不是最优的路径走。而且你的图像也不完全清晰。我猜绿色框中的那个人应该是起点,其他绿色框应该是你鼠标指向的地方的路径。对吗?那么你第二个例子中那个人上方的绿色框是怎么回事?@MattBurland drawMovePath()中的递归调用发生堆栈溢出错误.就图像而言,是的,绿色框中的家伙是起点,其他绿色框是路径。第二个示例中家伙上方的绿色框是问题所在,当我阻止路径超过单位速度时,它会显示路径被卡住的位置。在这种情况下,尽管路径显然需要往右转,它向上移动并卡住了。@Frecklefoot是的,我有。这是一次艰难的尝试*。