Search Unity

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

    JamesLeeNZ

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

    Guile_R

    Joined:
    Dec 29, 2012
    Posts:
    53
    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,616
    There are no other scripts moving it?
     
  5. Guile_R

    Guile_R

    Joined:
    Dec 29, 2012
    Posts:
    53
    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:
    199
  7. Guile_R

    Guile_R

    Joined:
    Dec 29, 2012
    Posts:
    53
    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:
    199
    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:
    53
    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 Distance;
    2. var target : Transform;
    3. var searchDistance = 45;
    4. var interestDistance = 30;
    5. var chaseRange = 15;
    6. var Damping = 6.0; //smooths rotation
    7.  
    8. private var navMeshComponent: NavMeshAgent;
    9.  
    10. function Start ()
    11. {
    12.     navMeshComponent = gameObject.GetComponent(NavMeshAgent);
    13. }
    14.  
    15. function Update ()
    16. {
    17.  
    18.     Distance = Vector3.Distance(target.position, transform.position);
    19.    
    20.     if (Distance > interestDistance)
    21.     {
    22.         animation.CrossFade("LookAround01");
    23.         navMeshComponent.Stop(true);       
    24.     }
    25.    
    26.     if (Distance <= interestDistance  Distance >= chaseRange)
    27.     {          
    28.         investigate();     
    29.     }
    30.    
    31.     if (Distance < chaseRange)
    32.     {
    33.         chase();       
    34.     }
    35. }
    36.  
    37. //FUNCTIONS
    38.  
    39.  
    40. function investigate ()
    41. {
    42.     navMeshComponent.Resume();
    43.     navMeshComponent.destination = target.position;
    44.     navMeshComponent.speed = 3.0;
    45.     animation.CrossFade("AngryWalk_AngryWalk");
    46. }
    47.  
    48. function chase ()
    49. {  
    50.     navMeshComponent.destination = target.position;
    51.     navMeshComponent.speed = 7.0;
    52.     animation.CrossFade("Running01");  
    53. }
     
    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 Transform target;
    7. public float searchDistance = 20.0f;
    8. public float interestDistance = 15.0f;
    9. public float chaseRange = 10.0f;
    10. public float damping = 5.0f;
    11.        
    12. private NavMeshAgent _navMeshComponent;
    13. private float distance;
    14.     void Start(){
    15.            
    16.         _navMeshComponent = GetComponent<NavMeshAgent>();
    17.     }
    18.    
    19.     void Update(){
    20.            
    21.    
    22.         distance = Vector3.Distance( target.position, transform.position);
    23.  
    24.         if ( distance > searchDistance ){ search(); }
    25.         if ( distance < interestDistance &&  distance > chaseRange ){ investigate(); }
    26.         if ( distance < chaseRange ){ chase(); }
    27.     }
    28.    
    29.     private void search(){
    30.        
    31.         Debug.Log( "search()" );
    32.        
    33.         //animation.CrossFade("LookAround01");
    34.         _navMeshComponent.Stop(true);                
    35.     }
    36.  
    37.     private void investigate(){
    38.            
    39.         Debug.Log( "investigate()" );
    40.        
    41.         _navMeshComponent.Resume();
    42.         _navMeshComponent.destination = target.position;
    43.         _navMeshComponent.speed = 3.0f;
    44.         //animation.CrossFade("AngryWalk_AngryWalk");
    45.     }
    46.    
    47.     private void chase(){
    48.        
    49.         Debug.Log( "chase()" );
    50.        
    51.         _navMeshComponent.destination = target.position;
    52.         _navMeshComponent.speed = 7.0f;
    53.         //animation.CrossFade("Running01");
    54.     }
    55. }