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

NavAgent moving very slowly when moved from coroutine

Discussion in '2D' started by BasharAlHotBod, Nov 25, 2021.

  1. BasharAlHotBod

    BasharAlHotBod

    Joined:
    Sep 30, 2020
    Posts:
    10
    Started making AI to move on an isometric grid I made. Currently have player characters moved by passing them a GridCell object by clicking on it, having them pathfind to the center using a nav agent destination, and setting an occupant variable of the GridCell to the character who moves there. If the cell is already occupied they won't do anything.

    Since enemies won't be moved by the player, I need them to move themselves one by one, then set the turn back to the player. I've started very simply by having the enemies move to the cell 5 spaces diagonally down. When the player ends their turn, I run this coroutine to move all the enemies.

    Code (CSharp):
    1. private IEnumerator TakeEnemyTurn()
    2.     {
    3.         foreach(EnemyCombat enemy in enemyCombatants)
    4.         {
    5.             GridCell nextCell = enemy.FindNextCell();
    6.             if (nextCell == null) ;
    7.             else
    8.             {
    9.                 enemy.MoveToCell(nextCell);
    10.                 yield return new WaitForSeconds(10);
    11.             }
    12.         }
    13.         currentTurn = Turn.playerTurn;
    14.         enemyTurnIndicator.gameObject.SetActive(false);
    15.         StartPlayerTurn();
    16.         selectedMerc.myCell.HighlightMovementDistance();
    17.     }
    MoveToCell function works using clicks, as well as when used to find the nearest cell on start, but here's the code anyway.

    Code (CSharp):
    1. public void MoveToCell(GridCell cell)
    2.     {
    3.         //empty current cell
    4.         if (myCell != null) myCell.occupant = null;
    5.  
    6.         //move to and fill new cell
    7.         navAgent.SetDestination(cell.GetCellCenter());
    8.         cell.occupant = this.gameObject;
    9.         myCell = cell;
    10.         grid.UpdateUI();
    11.         offset = false;
    12.  
    13.         myCell.HighlightMovementDistance();
    14.     }
    and here is the FindNextCell function, used only by the AI.

    Code (CSharp):
    1. public GridCell FindNextCell()
    2.     {
    3.         GridCell[] cells = FindObjectsOfType<GridCell>();
    4.         foreach (GridCell cell in cells)
    5.         {
    6.             if (cell.x == myCell.x - 5 && cell.y == myCell.y - 5) return cell;
    7.         }
    8.         return null;
    9.     }
    The enemies successfully choose their new cell, set its occupant to themselves, and vacate their current cell, one by one every 10 seconds, then set it back to my turn. Only issue is they take a very long time to walk there, even with their speed set to the same as the player nav agents. I can open up the inspector and increase the speed, but I feel there is something I'm doing wrong and that would be a bad fix.

    Is there a reason a nav agent whose destination set in a coroutine would move slower? Or is there some logical issue I'm overlooking?
     
  2. BasharAlHotBod

    BasharAlHotBod

    Joined:
    Sep 30, 2020
    Posts:
    10
    Turns out it was a bug with NavMeshPlus. I've encountered it before but didn't realize I wasn't accounting for it. For whatever reason moving a nav agent directly up or down on Y axis doesn't work right with this package.