Search Unity

Check distance for Walkie (NavMeshAgent)

Discussion in 'Scripting' started by Givago, May 6, 2017.

  1. Givago

    Givago

    Joined:
    Oct 22, 2015
    Posts:
    27
    Hi,

    I'm always trying to check before walking at the set points. (Using NavMeshAgent),
    But I wanted check every step if it is not far to walk using the NavMeshAgent.

    Code (CSharp):
    1.  float dist = Vector3.Distance(path[currentPoint].position, transform.position);
    2.         float distPlayer = Vector3.Distance(player.transform.position, transform.position);
    3.  
    4.        
    5.         if (distPlayer < 50 ) {
    6.  
    7.             navcomponent.speed = speed;
    8.             navcomponent.SetDestination(path[currentPoint].transform.position);          
    9.  
    10.  
    11.             if (dist <= reach)
    12.             {
    13.                 currentPoint++;
    14.             }
    15.  
    16.             if (currentPoint >= path.Length)
    17.             {
    18.                 currentPoint = 0;
    19.             }
    20.         }
    21.         else
    22.         {
    23.  
    24.         }
    But, after he walks, he just checks when he arrives on the spot (until he's farther away than he should),
    I know I told him to go using the:
    navcomponent.SetDestination(path[currentPoint].transform.position);

    I already tried to use SamplePosition, but failed.

    I needd help :D