Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ETeeski's FPS1.35 AI Pathfinding problem

Discussion in 'Scripting' started by ChrisSch, Mar 5, 2014.

  1. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    I'm assuming its telling me the array is going into the negative when it says out of bounds, but I don't understand why, I followed it to the letter and then resorted to just copy pasting the script and still I get it. And the enemy can't find the goal door. I double and triple and quadruple checked the triggers and tags and connections.

    I won't paste the full enemy code since there's more than just the tutorial stuff, I'm trying to implement it into my own game, and luckily the targeting is pretty much the same so no issues there. I'll copy paste the Update() part and the line where the error is.

    EnemyAI.js

    Code (csharp):
    1. function Update()
    2. {
    3.     //pathfinding
    4.     if(waitToStart <= 0)
    5.     {
    6.         playerCell = playerScript.currentCell;
    7.         for(var doorCheckingNow : GameObject in currentCell.GetComponent(AIPathCell).doors)
    8.         {
    9.             for(var i : int = 0; i <= doorCheckingNow.GetComponent(AIPathDoor).cells.length - 1; i++);
    10.             {
    11.                 if (doorCheckingNow.GetComponent(AIPathDoor).cells[i] == playerCell)
    12.                 if (doorCheckingNow.GetComponent(AIPathDoor).doorsToCells[i] < shortestPathSoFar)
    13.                 {
    14.                     goalDoor = doorCheckingNow;
    15.                     shortestPathSoFar = doorCheckingNow.GetComponent(AIPathDoor).doorsToCells[i];
    16.                 }
    17.             }
    18.         }
    19.         shortestPathSoFar = Mathf.Infinity;
    20.     }
    21.     waitToStart -= 1;
    22.    
    23.     if(goalDoor  currentCell != playerCell)
    24.     {
    25.         target = goalDoor.transform;
    26.     }
    27.     if(playerCell == currentCell)
    28.     {
    29.         target = targetOnHold;
    30.     }
    31. }
    Error Line:
    Code (csharp):
    1. if (doorCheckingNow.GetComponent(AIPathDoor).doorsToCells[i] < shortestPathSoFar)
    And Update function of the AIPathDoor if it matters.
    AIPathDoor.js

    Code (csharp):
    1. function Update ()
    2. {
    3.     if (testForCells  waitToTestCells <= 0)
    4.     {
    5.         for (var imediateCell : GameObject in imediateCells)
    6.         {
    7.             for (var i : int = 0; i <= cells.length - 1; i++)
    8.             {
    9.                 if (cells[i] == imediateCell)
    10.                     doorsToCells[i] = 1;
    11.             }
    12.         }
    13.        
    14.         for (stage = 2; stage <= cells.length; stage++)
    15.         {
    16.             for (i = 0; i <= cells.length - 1; i++)
    17.             {
    18.                 if (doorsToCells[i] == stage - 1)
    19.                 for (var checkDoor : GameObject in cells[i].GetComponent(AIPathCell).doors)
    20.                 {
    21.                     if (checkDoor != gameObject)
    22.                     {
    23.                         for (var checkCell : GameObject in checkDoor.GetComponent(AIPathDoor).imediateCells)
    24.                         {
    25.                             for (var j : int = 0; j <= cells.length - 1; j++)
    26.                             {
    27.                                 if(cells[j] == checkCell  doorsToCells[j] == null)
    28.                                 doorsToCells[j] = stage;
    29.                             }
    30.                         }
    31.                     }
    32.                 }
    33.             }
    34.         }
    35.         testForCells = false;
    36.     }
    37.     if(waitToTestCells > -10)
    38.     {
    39.         waitToTestCells -= 1;
    40.     }
    41. }
    Thanks in advance!
     
    Last edited: Mar 5, 2014