Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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.                 }