Using remainingDistance instead of Vector.Distance?

Hi there,

I just wanted to ask, if there is a reason, why we’re calculating the distance with Vector.Distance instead of just using the NavMeshAgent.remainingDistance?

I did it like this and it seemed to work just fine:

private void Update()
{
if(target != null)
{
GetComponent().MoveTo(target.position);

            if(GetComponent<NavMeshAgent>().remainingDistance < weaponRange)
            {
                GetComponent<Mover>().StopMove();
            }
        }
    }

Another handy thing with this approach was, that I didn’t get the NullPointerException that happend in the next lesson.

Please let me know if I’m overlooking something. Except the fact that I have to use the UnityEngine.AI namespace … Or is there a downside in using it?

Best regards,
Manuel

The only potential issue is that sometimes the physical distance (Vector3.distance) can be different from the NavMeshAgent.remainingDistance, as remainingDistance is a calculation what what actually needs to be travelled to get to the location.
Consider an archer who is standing on one side of the river, while the target is on the other… Vector3.distance may be 8 meters, and in range (because the archer can shoot across the river), but the remaining distance might be 30 meters (because the bridge is 15 meters away from the archer).

2 Likes

Ah ok, I wouldn’t have thought of that at all!

Thank you!

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

Privacy & Terms