# Set a minimum distance with navmeshagent

I am totally newb. How to set a minimum distance between AI chasing a player if AI has navmeshagent

        if (Vector3.Distance(transform.position, target.position) > minDistance)
{
enemy.destination = player.position;
}


What I need is it works similar to this code below:

if (Vector3.Distance(transform.position, player.position) > minDistance)
{
transform.position = Vector3.MoveTowards(transform.position, player.position, moveSpeed*Time.deltaTime);
}


But using destination property or SetDestination

• In what way does the behaviour of this code differ from what you want? Code can sometimes behave in ways that aren't obvious from a quick read, so it always helps to be explicit about the observed symptoms. – DMGregory Jul 7 at 19:38
• Sorry if I cant explained very well. I updated the post – Payso Bilverslack Jul 8 at 2:13
• I think I already achieve what I need by adding else part with: agent.velocity = Vector3.zero; But if you can provide me with better code I appreciate it – Payso Bilverslack Jul 8 at 2:49
• Want to share your solution as an Answer below? – DMGregory Jul 8 at 2:50

After DMGregory's comment, I'm rethinking my answer. You are already on a right way, actually.

Let's say some other script assign target (the player) to your NavMeshAgent (enemy). It can be done through public variable or through a method. As your target is assigned, you want to check, what is the distance left to it and if it is smaller than some threshold (minDistanceSqr in example below). If it is - you want to stop your agent from moving. Otherwise - continue following.

Now, if no ideas are present, here's a snippet that SHOULD (not tested) work. In your enemy script you want to add:

public Transform target;
public float minDistanceSqr;

private NavMeshAgent _agent;

private void Awake()
{
_agent = GetComponent<NavMeshAgent>();
}

private void Update()
{
if (target != null)
{
var destPosition = target.position;
var sqrDistance = (transform.position - destPosition).sqrMagitude;

_agent.destination = destPosition;
_agent.isStopped = (sqrDistance <= minDistanceSqr);
}
else
{
_agent.isStopped = true;
}
}


As other answer suggest changing velocity to 0, it actually won't stop agent from calculating path to your target, thus it will be using unnecessary CPU cycles.

### Old (and wrong) answer (leaving for educational purposes)

When setting target position, just add offset to the actual target:

1. calculate direction from your agent to desired target and normalize it
var offsetDirection = (player.transform.position - enemy.transform.position).normalized;

2. have a float type variable which sets the desired offset distance
var offsetDistance = 1f;

3. set destination with offset:
enemy.destination = (player.position + offsetDirection * offsetDistance);

• I think you want to flip the order of your subtraction here — otherwise, the enemy is trying to navigate to a position on the far side of the player from where the enemy is currently positioned. Also note that this can misbehave when the direct line between the enemy and player is obstructed, so the shortest path to the player arrives from a different direction than the direct line, or the offset position is on the other side of a wall from the player, leading the agent to navigate to the wrong place entirely. – DMGregory Jul 9 at 10:39
• @DMGregory good points, will edit my answer soon! 👍 – eLTomis Jul 9 at 13:24

else { agent.velocity = Vector3.zero; }

• This answer would be better if it showed this code in its full context (can't have an else without an if), and explained why the code solves the problem. – DMGregory Jul 8 at 11:22