1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

Another NavMeshAgent not stopping, could you please have a look?

Discussion in 'Scripting' started by Guile_R, May 1, 2013.

  1. Guile_R

    Guile_R

    Joined:
    Dec 29, 2012
    Posts:
    50
    Hi guys,

    Apologies for making yet another thread on NavMeshAgents but believe me, I have looked at most topics reading each one of them over and over and none of the solutions I've found were able to help me out. Yes I am a noob, maybe not to scripting but a big yes to Unity and Java.

    Well the deal is, my Agent slides or rather moves for apparently a fixed distance before it stops. I have tried several NavMeshAgent methods, Stop(), enable(), ResetPath(), SetPath() to itself but it keeps moving towards the target or just straight if I ResetPath(). I am suspicious that this is not something with my code, maybe something with one of the NavMeshAgent's variables even thoughI have also changed accelaration et al to several super-small or large values?

    If any of you would be kind enough to give me pointers it would be immensely appreciated. It may well be something silly and I will embarrass myself yet again on a different forum.

    Code (csharp):
    1. var searchDistance = 45.0;
    2. var interestDistance = 30.0;
    3. var chaseRange = 15.0;
    4. var Damping = 6.0; //smooths rotation
    5.  
    6. private var navMeshComponent: NavMeshAgent;
    7.  
    8. function Start ()
    9. {
    10.     navMeshComponent = gameObject.GetComponent(NavMeshAgent);
    11. }
    12.  
    13. function Update ()
    14. {
    15.  
    16.    
    17.     if (Distance > searchDistance)
    18.     {
    19.         animation.Play("LookAround01");
    20.        
    21.     }
    22.    
    23.         if (Distance < searchDistance  Distance > interestDistance)
    24.     {
    25.         navMeshComponent.ResetPath(); // it does indeed reset the path, it stops following
    26.         navMeshComponent.SetDestination(transform.position); // keeps sliding
    27.         navMeshComponent.Stop(true); // keeps sliding      
    28.         search();      
    29.     }
    30.    
    31.     if (Distance < interestDistance  Distance > chaseRange)
    32.     {          
    33.         investigate();     
    34.     }
    35.    
    36.     if (Distance < chaseRange)
    37.     {
    38.         chase();       
    39.     }
    40. }
    41.  
    42. //FUNCTIONS
    43.  
    44. function search ()
    45. {      
    46.     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
    47.     animation.Play("LookAround01");
    48.    
    49. }
    50.  
    51. function investigate ()
    52. {
    53.     navMeshComponent.destination = target.position;
    54.     navMeshComponent.speed = 3.0;
    55.     animation.CrossFade("AngryWalk_AngryWalk");
    56. }
    57.  
    58. function chase ()
    59. {  
    60.     navMeshComponent.destination = target.position;
    61.     navMeshComponent.speed = 7.0;
    62.     animation.CrossFade("Running01");  
    63. }
    Many thanks!
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,608
  3. Guile_R

    Guile_R

    Joined:
    Dec 29, 2012
    Posts:
    50
    Thank you for you reply James, yes I've tried that by setting:

    Code (csharp):
    1. navMeshComponent.updatePosition = false;
    And the agent still moves towards me, it shifts to the "LookAround01" animation in the search() function when I reach the specified distance but it keeps moving towards me using this animation which actually looks like if he's ice skating but to be honest this stopped being funny a few hours ago.

    I have no idea what is going on. My acceleration value is set to 80, played with other settings just to see if the agent was taking its time before stopping but I am yet to be successful on this.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,608
    There are no other scripts moving it?
     
  5. Guile_R

    Guile_R

    Joined:
    Dec 29, 2012
    Posts:
    50
    Nope, no other scripts. It seems that nothing within this if clause is working:

    Code (csharp):
    1.     if (Distance < searchDistance  Distance > interestDistance)
    2.  
    3.     {
    4.  
    5.         navMeshComponent.ResetPath(); // it does indeed reset the path, it stops following
    6.  
    7.         navMeshComponent.SetDestination(transform.position); // keeps sliding
    8.  
    9.         navMeshComponent.Stop(true); // keeps sliding      
    10.  
    11.         search();      
    12.  
    13.     }
    Maybe I'm too tired to spot something wrong here? I have no idea why it isn't calling 'search()'. I've disabled my if clause that plays 'LookAround01' and now I see that the animation is not being called on the search() nor if I place the animation in the if clause above.
     
  6. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    198
  7. Guile_R

    Guile_R

    Joined:
    Dec 29, 2012
    Posts:
    50
    Thanks Sajid, I have tried .enable and I have read your topic many times. It was something with my if clause and I just can't figure it out. I have eliminated the if clause and worked around it and now I am getting the expected behavior. Not ideal I have to say but at least I can move forward. Thanks to you and James for taking the time to reply to this thread.
     
  8. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    198
    Glad to hear you got it working.

    Mind posting the script so people who come across this thread later on can figure it out as well?
     
  9. Guile_R

    Guile_R

    Joined:
    Dec 29, 2012
    Posts:
    50
    Sure, but I really haven't done much to be honest. I have eliminated one of my if clauses and used the .Stop and .Resume functions which were not working within the eliminated if clause for some obscure reason, I've used .Stop(true) when the player gets too far and the .Resume() in the investigate() function called when the player gets closer.

    Here it is:

    Code (csharp):
    1. var searchDistance = 45;
    2. var interestDistance = 30;
    3. var chaseRange = 15;
    4. var Damping = 6.0; //smooths rotation
    5.  
    6. private var navMeshComponent: NavMeshAgent;
    7.  
    8. function Start ()
    9. {
    10.     navMeshComponent = gameObject.GetComponent(NavMeshAgent);
    11. }
    12.  
    13. function Update ()
    14. {
    15.  
    16.    
    17.     if (Distance > interestDistance)
    18.     {
    19.         animation.CrossFade("LookAround01");
    20.         navMeshComponent.Stop(true);       
    21.     }
    22.    
    23.     if (Distance <= interestDistance  Distance >= chaseRange)
    24.     {          
    25.         investigate();     
    26.     }
    27.    
    28.     if (Distance < chaseRange)
    29.     {
    30.         chase();       
    31.     }
    32. }
    33.  
    34. //FUNCTIONS
    35.  
    36.  
    37. function investigate ()
    38. {
    39.     navMeshComponent.Resume();
    40.     navMeshComponent.destination = target.position;
    41.     navMeshComponent.speed = 3.0;
    42.     animation.CrossFade("AngryWalk_AngryWalk");
    43. }
    44.  
    45. function chase ()
    46. {  
    47.     navMeshComponent.destination = target.position;
    48.     navMeshComponent.speed = 7.0;
    49.     animation.CrossFade("Running01");  
    50. }
     
    Last edited: May 2, 2013
  10. OregonOx

    OregonOx

    Joined:
    Mar 27, 2010
    Posts:
    12
    In your first IF statement in Update(), It should be ( Distance > searchDistance )

    This code works as expected:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NavMeshComponent : MonoBehaviour {
    5.  
    6. public float searchDistance = 20.0f;
    7. public float interestDistance = 15.0f;
    8. public float chaseRange = 10.0f;
    9. public float damping = 5.0f;
    10.        
    11. private NavMeshAgent _navMeshComponent;
    12. private float distance;
    13.     void Start(){
    14.            
    15.         _navMeshComponent = GetComponent<NavMeshAgent>();
    16.     }
    17.    
    18.     void Update(){
    19.            
    20.    
    21.         distance = Vector3.Distance( target.position, transform.position);
    22.  
    23.         if ( distance > searchDistance ){ search(); }
    24.         if ( distance < interestDistance &&  distance > chaseRange ){ investigate(); }
    25.         if ( distance < chaseRange ){ chase(); }
    26.     }
    27.    
    28.     private void search(){
    29.        
    30.         Debug.Log( "search()" );
    31.        
    32.         //animation.CrossFade("LookAround01");
    33.         _navMeshComponent.Stop(true);                
    34.     }
    35.  
    36.     private void investigate(){
    37.            
    38.         Debug.Log( "investigate()" );
    39.        
    40.         _navMeshComponent.Resume();
    41.         _navMeshComponent.destination = target.position;
    42.         _navMeshComponent.speed = 3.0f;
    43.         //animation.CrossFade("AngryWalk_AngryWalk");
    44.     }
    45.    
    46.     private void chase(){
    47.        
    48.         Debug.Log( "chase()" );
    49.        
    50.         _navMeshComponent.destination = target.position;
    51.         _navMeshComponent.speed = 7.0f;
    52.         //animation.CrossFade("Running01");
    53.     }
    54. }