0
\$\begingroup\$

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 GameObject[] dead_point;
public float dist;
public int direction_point;

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

void enemy_dirction()    {
    farPoint = Random.Range(0, dead_point.Length);
    mAgent.destination = dead_point[farPoint].transform.position;
}

// 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 GameObject[] dead_point;
public float dist;
public int direction_point;

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

void enemy_dirction()    {
    farPoint = Random.Range(0, dead_point.Length);
    mAgent.destination = dead_point[farPoint].transform.position;
}

// 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) {
            mAgent.destination = dead_point[farPoint].transform.position;
            print(farPoint);
        }
    }

}// end update
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory May 31 '18 at 20:06
1
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.