Search Unity

NavMeshAgents Stop Moving When Target Changes

Discussion in 'Navigation' started by bwaite87, Sep 13, 2018.

  1. bwaite87

    bwaite87

    Joined:
    Nov 23, 2017
    Posts:
    18
    I've been stuck and looking at this for so long that I've had dreams about this code, please help!

    So brief background:

    Agents chase the player for 10 seconds, and then return to a random 'beacon' after the allotted time.
    The agents will randomly just stop moving when retreating back to their beacons. Most of them don't, but an occasional group of them will (group's are instantiated together when the player shines a light on the beacon.) But the point is their movement is always perfect when chasing the player.

    I've tried everything my noob skills allow.
    Note: In the editor, if I disable and re-enable the "stuck" agents their movement starts working again.

    Here's my code:

    Any suggestions?
    Code (CSharp):
    1. NavMeshAgent agent;
    2.     Transform player;
    3.     GameMaster gameMaster;
    4.     public bool destinationSet;
    5.     Vector3 destination;
    6.     public float timeForDestinationChange = 0.15f;
    7.  
    8.     void Awake () {
    9.         agent = GetComponent<NavMeshAgent> ();
    10.         player = GameObject.FindGameObjectWithTag ("Player").transform;
    11.         gameMaster = GameObject.FindGameObjectWithTag ("GameManager").GetComponent<GameMaster> ();
    12.         destinationSet = false;
    13.         // StartCoroutine(SetDestination());
    14.        
    15.      
    16.     }
    17.    
    18.  
    19.     void GoToBeacon()
    20.     {
    21.         if (gameMaster.beaconList.Count > 0) //if there are still active beacons that haven't been lit
    22.         {
    23.             if (!destinationSet) { //ensure destination is only set once
    24.                 destination = gameMaster.beaconList [Random.Range (0, gameMaster.beaconList.Count-1)].transform.position;
    25.                 destinationSet = true;
    26.                
    27.             }
    28.  
    29.             agent.SetDestination(destination);
    30.            
    31.  
    32.         }
    33.         else //if all beacons have been lit, you are chased.
    34.         {
    35.             destination = player.transform.position;
    36.             agent.SetDestination(destination);
    37.             GameMaster.baddieIsChasingPlayer = true;
    38.         }
    39.     }
    40.  
    41.     void OnEnable()
    42.     {
    43.         destinationSet = false; //reset destinationSet bool so agent movement works appropriately
    44.        // StartCoroutine(SetDestination());
    45.        
    46.     }
    47.  
    48.  
    49.    
    50.  
    51.     private void Update()
    52.     {
    53.  
    54.         if (GameMaster.baddieIsChasingPlayer)
    55.         {
    56.             destination = player.transform.position;
    57.             agent.SetDestination(destination);
    58.         }
    59.         else
    60.         {
    61.             GoToBeacon();
    62.         }
    63.     }
    64.    
    65. }
    66.  
     
  2. bwaite87

    bwaite87

    Joined:
    Nov 23, 2017
    Posts:
    18
    So I'm dumb.
    The problem was with the destinationSet bool. It was never being set back to false if another beacon was activated, so the code in "if (!destinationSet)" was never being called.

    Let this be a lesson to all!! Don't be dumb like me ;P