Search Unity

Resolved Why the agent on the last waypoint never stop but make small movements from side to side ?

Discussion in 'Navigation' started by abrahamkrim, May 14, 2020.

  1. abrahamkrim

    abrahamkrim

    Joined:
    May 11, 2020
    Posts:
    9
    This is my script for waypoints. When the flag bool loop is false the agent is moving between all the waypoints and on the last waypoint instead stop still without moving he make small movements like ping-pong on the waypoint. and only the last waypoint he is not moving to other waypoints he just make ping-pong on very small area on the last waypoint.

    That's when loop is false.

    I tried to check and if I set in the Inspector in the agent Nav Mesh Agent component the speed property value to 0 the agent will stop but he will stop where he is not really on the waypoint since the agent make ping pong.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class AgentControl : MonoBehaviour
    8. {
    9.     public List<Transform> points;
    10.     public bool waitTimeToMove = false;
    11.     //notice WaitTime is a float now
    12.     public float WaitTime = 10f;
    13.     public bool randomWaitTime;
    14.     float waitMinTime = 1f;
    15.     float waitMaxTime = 10f;
    16.     public bool loop = false;
    17.  
    18.     private int destPoint = 0;
    19.     private NavMeshAgent agent;
    20.     private Transform originalPos;
    21.     //two vars for handling timer
    22.     private float timer = 0;
    23.  
    24.     void Start()
    25.     {
    26.         agent = GetComponent<NavMeshAgent>();
    27.  
    28.         // Disabling auto-braking allows for continuous movement
    29.         // between points (ie, the agent doesn't slow down as it
    30.         // approaches a destination point).
    31.         agent.autoBraking = false;
    32.  
    33.         if (randomWaitTime == true)
    34.         {
    35.             WaitTime = Random.Range(waitMinTime, waitMaxTime);
    36.         }
    37.  
    38.         //transforms dont exist without A GameObject and a GameObject doesn't exist without a transform
    39.         //create a new GameObject to hold our position
    40.         GameObject originalPositionObject = new GameObject();
    41.         //set the new gameobjects position equal to where the transform is right now
    42.         originalPositionObject.transform.position = transform.position;
    43.         //add this to the points list instead
    44.         points.Add(originalPositionObject.transform);
    45.     }
    46.  
    47.  
    48.     void GotoNextPoint()
    49.     {
    50.         // Returns if no points have been set up
    51.         if (points.Count == 0)
    52.             return;
    53.  
    54.         // Set the agent to go to the currently selected destination.
    55.         agent.destination = points[destPoint].position;
    56.  
    57.         // Choose the next point in the array as the destination,
    58.         // cycling to the start if necessary.
    59.         destPoint = (destPoint + 1) % points.Count;
    60.     }
    61.  
    62.  
    63.     void Update()
    64.     {
    65.         // Choose the next destination point when the agent gets
    66.         // close to the current one.
    67.         if (!agent.pathPending && agent.remainingDistance < 1f)
    68.         {
    69.             //if wait to move is true
    70.             if (waitTimeToMove)
    71.             {
    72.                 //if timer is less than 10
    73.                 if (timer < WaitTime)
    74.                 {
    75.                     //add Time.deltaTime each time we hit this point
    76.                     timer += Time.deltaTime;
    77.                 }
    78.                 //no longer waiting because timer is greater than 10
    79.                 else
    80.                 {
    81.  
    82.                     waitTimeToMove = false;
    83.                 }
    84.             }
    85.             //if we hit here waitToMove is false, so go ahead as usual
    86.             else
    87.             {
    88.                 if (loop == true || destPoint != points.Count - 1)
    89.                 {
    90.                     GotoNextPoint();
    91.                 }
    92.             }
    93.         }
    94.     }
    95. }
    96.  
     
  2. abrahamkrim

    abrahamkrim

    Joined:
    May 11, 2020
    Posts:
    9
    This is a working script. Added some more conditions and now it's working.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class AgentControl : MonoBehaviour
    8. {
    9.     public List<Transform> points;
    10.     public bool waitTimeToMove = false;
    11.     //notice WaitTime is a float now
    12.     public float WaitTime = 10f;
    13.     public bool randomWaitTime;
    14.     float waitMinTime = 1f;
    15.     float waitMaxTime = 10f;
    16.     public bool loop = false;
    17.  
    18.     private int destPoint = 0;
    19.     private NavMeshAgent agent;
    20.     private Transform originalPos;
    21.     //two vars for handling timer
    22.     private float timer = 0;
    23.     private float originSpeed;
    24.  
    25.     void Start()
    26.     {
    27.         agent = GetComponent<NavMeshAgent>();
    28.  
    29.         // Disabling auto-braking allows for continuous movement
    30.         // between points (ie, the agent doesn't slow down as it
    31.         // approaches a destination point).
    32.         agent.autoBraking = false;
    33.  
    34.         originSpeed = agent.speed;
    35.  
    36.         if (randomWaitTime == true)
    37.         {
    38.             WaitTime = Random.Range(waitMinTime, waitMaxTime);
    39.         }
    40.  
    41.         //transforms dont exist without A GameObject and a GameObject doesn't exist without a transform
    42.         //create a new GameObject to hold our position
    43.         GameObject originalPositionObject = new GameObject();
    44.         //set the new gameobjects position equal to where the transform is right now
    45.         originalPositionObject.transform.position = transform.position;
    46.         //add this to the points list instead
    47.         points.Add(originalPositionObject.transform);
    48.     }
    49.  
    50.  
    51.     void GotoNextPoint()
    52.     {
    53.         // Returns if no points have been set up
    54.         if (points.Count == 0)
    55.             return;
    56.  
    57.         // Set the agent to go to the currently selected destination.
    58.         agent.destination = points[destPoint].position;
    59.  
    60.         // Choose the next point in the array as the destination,
    61.         // cycling to the start if necessary.
    62.         destPoint = (destPoint + 1) % points.Count;
    63.     }
    64.  
    65.  
    66.     void Update()
    67.     {
    68.         // Choose the next destination point when the agent gets
    69.         // close to the current one.
    70.         if (!agent.pathPending && agent.remainingDistance < 1f)
    71.         {
    72.             //if wait to move is true
    73.             if (waitTimeToMove)
    74.             {
    75.                 //if timer is less than 10
    76.                 if (timer < WaitTime)
    77.                 {
    78.                     //add Time.deltaTime each time we hit this point
    79.                     timer += Time.deltaTime;
    80.                 }
    81.                 //no longer waiting because timer is greater than 10
    82.                 else
    83.                 {
    84.  
    85.                     waitTimeToMove = false;
    86.                 }
    87.             }
    88.             //if we hit here waitToMove is false, so go ahead as usual
    89.             else
    90.             {
    91.                 if (loop == false && destPoint == points.Count - 1)
    92.                 {
    93.                     agent.speed = 0;
    94.                 }
    95.                
    96.                 if (loop == true || destPoint != points.Count - 1)
    97.                 {
    98.                     agent.speed = originSpeed;
    99.                     // Not working if setting back to loop = true in the inspector
    100.                     // After it was not loop and agent in last waypoint
    101.                     // When setting to loop = true it's not continue to check why
    102.                     // Should continue the loop if loop true !
    103.                     // Loop = true is not working when game is running only on Start
    104.  
    105.                     GotoNextPoint();
    106.                 }
    107.             }
    108.         }
    109.     }
    110. }
    111.