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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NavMeshAgent.SetDestination FAULT

Discussion in 'Scripting' started by pixelone, May 29, 2015.

  1. pixelone

    pixelone

    Joined:
    Apr 16, 2010
    Posts:
    157
    The code below is in an Update method. When I click this bit of code is executed immediately versus when the agent gets to the stopping point. It does me no good. I need for when the agent gets to where I click, this bit of code fires.

    Code (CSharp):
    1. if(agent.remainingDistance <= agent.stoppingDistance){
    2.                 print (" We have arrived!");
    3.  
    4.             }
    Can Anyone help me with this. Its very strange. I wish there was a bool call built in so when the agent reaches its destination its set to "True".


    Code (CSharp):
    1. ///Move the puppy where we first tap
    2.         RaycastHit hit;
    3.         if(Input.GetMouseButtonDown(0)){
    4.             anim.SetBool("Walk", true);
    5.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.             if(Physics.Raycast(ray, out hit))
    7.             {
    8.                 agent.SetDestination(hit.point);
    9.  
    10.             }
    11.  
    12.             if(agent.remainingDistance <= agent.stoppingDistance){
    13.                 print (" We have arrived!");
    14.  
    15.             }
    16.  
    17.         }
     
  2. MysterySoftware

    MysterySoftware

    Joined:
    Sep 18, 2014
    Posts:
    46
    Try using something along those lines to check if you reached your target:
    Code (CSharp):
    1. if (Vector3.Destination (transform.position, target.position) <= agent.stoppingDistance) {
    2.  
    3. }
     
    pixelone likes this.
  3. pixelone

    pixelone

    Joined:
    Apr 16, 2010
    Posts:
    157
    Thanks MysterySoftware.. fixed a minor correction in your suggestion:

    Code (CSharp):
    1. if(Vector3.Distance(transform.position, WAYPOINT.position) <= agent.stoppingDistance){
    2.  
    3.  
    4.                 }