How to detect if NavAgent has reached hit.point?

void Update()
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
        {
            agent.SetDestination(hit.point);

            if (agent.destination == hit.point)
            {
                print("Destination reached");
            }
        }
    }

The problem is the “if (agent.destination == hit.point)
{
print(“Destination reached”);
}” line. What I want is to be able to register when the agent has reached the hit.point. How do I do it?

Your best bet is to have it s.t. your destination is “reached” when your agent is within certain bounds. Use (transform.position - destination.position).sqrMagnitude < x, where x may vary based on the speed with which your agent approaches your destination. Note that NavMesh Agents also have some properties that allow you to establish how they should target and subsequently approach a point. Consult the docs for this.

Update: new code looks like this
`
float dist;
RaycastHit hit;

void Update()
{
float dist = Vector3.Distance(agent.transform.position, hit.point);

     if (Input.GetMouseButtonDown(1))
     {

     if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
     {
         agent.SetDestination(hit.point);

         if (dist <.1f)
         {
             print("Destination reached");
         }
     }
 }`

And it doesn’t work. The distance isn’t really registered. The message “Destination reached” appears the second I click which is not what I want. The actual distance doesn’t seem to matter at all. I want the message to only appear AFTER the agent has reached the hit.point.

I’m having the same issue, did you manage to solve this?

I just want to measure if the agent has reached, or is close to reaching, the destination.

public class GuardController : MonoBehaviour
{

    public Camera cam;

    public NavMeshAgent agent;

    [SerializeField] private string selectableTag = "Guard";

    GameObject lastHit;

    bool isSelected = false;



    void OnMouseEnter()
    {
        CompareTag(selectableTag);
        isSelected = true;
    }



    // Update is called once per frame
    void Update()

    {


        if (Input.GetMouseButton(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out var hitInfo) && isSelected)
        {

            GameObject.Find("People/Guard/GCube").SetActive(true);
            agent.SetDestination(hitInfo.point);

// IF STATEMENT TO MEASURE IF MOVING AND REACHING LOCATION

            agent.gameObject.GetComponent<Animator>().Play("Walk_Male");

// PLAY IDLE ANIMATION WHEN DESTINATION IS REACHED

        }

        if (Input.GetMouseButtonDown(1))
        {
            GameObject.Find("People/Guard/GCube").SetActive(false);
            isSelected = false;
            agent.gameObject.GetComponent<Animator>().Play("Idle_Generic");

        }

    }
}

,I’ having the same problem. I just want to create an if statement that measures when the agent has hit or is coming close to the hit point:

public class GuardController : MonoBehaviour
{

    public Camera cam;

    public NavMeshAgent agent;

    [SerializeField] private string selectableTag = "Guard";

    GameObject lastHit;

    bool isSelected = false;



    void OnMouseEnter()
    {
        CompareTag(selectableTag);
        isSelected = true;
    }



    // Update is called once per frame
    void Update()

    {


        if (Input.GetMouseButton(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out var hitInfo) && isSelected)
        {

            GameObject.Find("People/Guard/GCube").SetActive(true);
            agent.SetDestination(hitInfo.point);



            agent.gameObject.GetComponent<Animator>().Play("Walk_Male");

        }

        if (Input.GetMouseButtonDown(1))
        {
            GameObject.Find("People/Guard/GCube").SetActive(false);
            isSelected = false;
            agent.gameObject.GetComponent<Animator>().Play("Idle_Generic");

        }

    }
}

So what you do here is

  1. store what the distance was to the target last time you pressed the mouse button and the raycast hit something.
  2. Check if the mouse button is down.
  3. Check if the raycast hit something.
  4. Update the place the agent should travel to.
  5. Check immediately if the agent reached the destination you previously clicked (and only check when the mouse button is clicked).

You will need to repeatedly check if the agent has reached its destination since it moves over time. Something like this:

RaycastHit hit; 
void Update()
{
    float dist = Vector3.Distance(agent.transform.position, hit.point);
    if (dist < .1f)
    {
        print("Destination reached");
    }
    if (Input.GetMouseButtonDown(1))
    {
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),    
                            out hit, Mathf.Infinity))
        {
                agent.SetDestination(hit.point);    
        }
    }
}

Futhuremore, please post the print of hit.point and agent.transform.position, should give you a hint of what went wrong or help me and others tell what is going wrong.

I see now that this is a year old post, so you have proably figured it out by now. Please post your resolution and mark it as answer so that others can learn from you.