Why not use NavMeshAgent.SetDestination?

I noticed that when the component NavMeshAgent is returned, the instructor sets direction in the property “direction”.
Why not use NavMeshAgent.SetDestination(hit.point) instead?

thanks in advance and sorry if that’s a silly question.

Here’s the actual code used (from the Github repo)

private void MoveToCursor()
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool hasHit = Physics.Raycast(ray, out hit);
        if (hasHit)
            GetComponent<NavMeshAgent>().destination = hit.point;

What we’re setting is the destination.
You can use either SetDestination(hit.point) or destination=hit.point; The difference is that SetDestination returns true if a path was found, false if not. Setting it as destination=hit.point simply doesn’t return a false if the destination couldn’t be found. You could use SetDestination if you wish, or if you need the result.


This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms