# Agent hesitate when choose direction?

I have script for my agent enemy where he should choose one random point and goes to it. But what he did is hesitating !

private NavMeshAgent mAgent;
public float dist;
public int direction_point;

void Start()    {
mAgent = GetComponent<NavMeshAgent>();
player = GameObject.FindWithTag("Player");
} // end start

void enemy_dirction()    {
}

// Update is called once per frame
void Update()    {
dist = Vector3.Distance(transform.position, player.transform.position);
if (dist <= 20)
{
mAgent.destination = player.transform.position;
}
else
{
enemy_dirction();
}

}// end update


update 1, I used this new script and solved my problem.

    private NavMeshAgent mAgent;
public float dist;
public int direction_point;

void Start()    {
mAgent = GetComponent<NavMeshAgent>();
player = GameObject.FindWithTag("Player");
enemy_dirction()
} // end start

void enemy_dirction()    {
}

// Update is called once per frame
void Update()    {
dist = Vector3.Distance(transform.position, player.transform.position);
if (dist <= 20)
{
mAgent.destination = player.transform.position;
}
else
{
if (mAgent.remainingDistance <= 0.5f) {
print(farPoint);
}
}

}// end update

• It's unclear from this edit whether your new script is a solution, or if it still has a problem you want to solve. If it still has a problem, please describe the symptoms. If it's a solution to your problem, please post it as an Answer instead, so it doesn't get confused with the question. – DMGregory May 31 '18 at 20:06

If your agent is not close to the player, then you're asking it to choose a new random destination every frame.

If you want to pick one destination and stick to it for a while, you'll need to add some conditional logic to pick a new destination only sometimes, and otherwise carry on with the old one, eg:

• when you've gotten "close enough" to the last destination you picked

• when enough time has passed since the last frame when you chose a new destination

• when an event changes your character's state

• randomly, with a low probability

etc.

• I read your notes and it's working now. – Omer May 31 '18 at 19:52
• If you want to share a solution, post it as an answer. Comments are not good for sharing code. – DMGregory May 31 '18 at 19:53
• New script it's my solution. Next time I will put it in new comment. Thank you DM. – Omer May 31 '18 at 21:29