Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

how to check the navmesh agent is reach the destination or not?

Discussion in 'Navigation' started by UNITY3D_TEAM, Oct 1, 2016.

  1. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    how to check the navmesh agent is reach the destination or not?


    public Transform target;

    NavMeshAgent agent;
    // Use this for initialization
    void Start ()
    {

    agent = GetComponent<NavMeshAgent> ();

    }

    // Update is called once per frame
    void Update ()
    {
    agent.SetDestination (target.position);
    }


    if(reached )

    ///do some action


    so how can i check if reached the target?
     
  2. rblkwa

    rblkwa

    Joined:
    Jun 20, 2013
    Posts:
    61
    I would rather set the agent's destination every x frames instead of every update. Anyway; this should get you going (untested though)

    Code (CSharp):
    1.  
    2. [SerializeField]float destinationReachedTreshold;
    3. Vector3 target;
    4.  
    5. void SetTarget(Vector3 position) {
    6.   target = position;
    7.   agent.SetDestination(target);
    8. }
    9.  
    10. void Update() {
    11. CheckDestinationReached();
    12. }
    13.  
    14. void CheckDestinationReached() {
    15.   float distanceToTarget = Vector3.Distance(transform.position, target);
    16.   if(distanceToTarget < destinationReachedTreshold)
    17.   {
    18.    print("Destination reached");
    19.   }
    20. }
     
    hagamee likes this.
  3. samfarhan

    samfarhan

    Joined:
    Oct 14, 2012
    Posts:
    14
    You should be using Vector3.squareMagnitude rather than Vector3.Distance() as it's much more efficient. Especially important when it's running each frame.
     
  4. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720


    ya thanks for this answer right now im using Vector3.Distance().right now im changing to Vector3.squareMagnitude as your suggestion
     
  5. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720

    thanks for the reply its helped me alot
     
    unity3bhs and rblkwa like this.
  6. Jakob_Unity

    Jakob_Unity

    Unity Technologies

    Joined:
    Dec 25, 2011
    Posts:
    269
    You should use the .remainingDistance property instead of the Euclidean distance - the path will typically be longer.
    To illustrate a problem with using the Euclidean distance : if the target is on the other side of a wall right next to your character the distance might be 1 unit - but the path to get there might be much longer (e.g. because you have to go through a door to get there)
     
    io-games, hippocoder and LaneFox like this.
  7. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720

    ok i got it thank u ! im started using now .remainding distance now
     
unityunity