Search Unity

NavMeshAgent not moving to destination

Discussion in 'Navigation' started by gragest, Feb 6, 2018.

  1. gragest

    gragest

    Joined:
    Apr 22, 2016
    Posts:
    3
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class Agent : MonoBehaviour {
    8.  
    9.     public Transform target; // the target location that the agent moves to
    10.     private Enemy enemy; // the enemy object that is connected to the agent
    11.     private NavMeshAgent meshAgent; // the nav mesh agent that is connected to the agent object
    12.  
    13.     // Use this for initialization
    14.     void Start () { // assigns the variables and sets the destination
    15.         meshAgent = this.GetComponent<NavMeshAgent> ();
    16.         enemy = gameObject.GetComponent<Enemy> ();
    17.         if (!target) {
    18.             target = Storage.playerController.transform;
    19.         }
    20.         setDestination ();
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () { // sets the new destination of the agent every frame
    25.         if (enemy) {
    26.             if (enemy.isDead ()) {
    27.                 meshAgent.SetDestination (this.transform.position);
    28.             } else {
    29.                 setDestination ();
    30.             }
    31.         } else {
    32.             setDestination ();
    33.         }
    34.     }
    35.  
    36.     private void setDestination(){ // funtion for setting the destination of the navMeshAgent
    37.         if (target) {
    38.             meshAgent.SetDestination (target.position);
    39.         } else {
    40.             target = Storage.playerController.transform;
    41.             setDestination ();
    42.         }
    43.     }
    44. }
    45.  
    My NavMeshAgents don't seem to be moving to the destination that is being set. When they are spawned they just move in random directions and don't pursue the player in any manner at all. I have baked the scene's navmesh with obstacles and tried several things and nothing seems to work. Any solutions?? Only happens sometimes, other times they chase the player just fine.
     

    Attached Files:

    • Agent.cs
      File size:
      1.2 KB
      Views:
      1,034
    Last edited: Feb 12, 2018
    Roger-W likes this.
  2. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,335
  3. gragest

    gragest

    Joined:
    Apr 22, 2016
    Posts:
    3
    Thank you lol
     
    laurentlavigne likes this.
  4. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,335
    you're moving to your position :rolleyes:
     
  5. gragest

    gragest

    Joined:
    Apr 22, 2016
    Posts:
    3
    yes, that is intended since the navmesh agent is usually connected to an enemy object and when the enemy is dead is the only time that is called and i have already used debug to test if that is where the problem originates from and it is not a target problem. The problem is with the agent moving to the target destination and only occurs when the enemies are spawned by a door object and for some reason does not occur when spawned independently. Regarding that line of code if there is a better way to write it i am more than open to learn because i have only been coding for just over a year so i am somewhat of a novice still. ty
     
  6. JacobRWhite

    JacobRWhite

    Joined:
    May 13, 2020
    Posts:
    1
    did you ever figure it out? im having the same problem
     
    Rsrc10 likes this.