- Home /
Question by
Heffalus · Mar 05, 2015 at 10:32 AM ·
c#airandomnavmeshagentpathfinding
AI pathfinding waypoints
Ive implemented a simple script for random movement between waypoints with a random wait time between each, the problem is the AI seems a little "indecisive", at the start of movement the ai doesnt decide right away which waypoint to go to. It's starting to walk in multiple directions before it decides "thats the waypoint im going to".
anyone see a fix for this?
this is the script:
private bool isDeciding;
void Start () {
agent = GetComponent<NavMeshAgent> ();
}
// Update is called once per frame
void Update () {
if (GetComponent<NpcTalk> ().isTalking ) {
agent.Stop();
agent.transform.LookAt(GameObject.FindGameObjectWithTag("Player").transform.position);
}
if (!GetComponent<NpcTalk> ().isTalking) {
agent.Resume();
}
if (isWaiting) {
if(!isDeciding)
StartCoroutine(WaitOrMove());
}
}
if (agent.remainingDistance <= agent.stoppingDistance) {
//StartCoroutine(WaitOrMove());
isMoving = false;
isWaiting = true;
}
//agent.SetDestination (GameObject.FindGameObjectWithTag ("Player").transform.position);
}
private IEnumerator WaitOrMove () {
isDeciding = true;
yield return new WaitForSeconds (Random.Range (1, 3));
int r = Random.Range (0, 6);
agent.SetDestination(waypoints[r].transform.position);
isMoving = true;
isWaiting = false;
isDeciding = false;
}
EDIT: edited the script with answer
Comment
Best Answer
Answer by Cherno · Mar 05, 2015 at 12:27 PM
Add another bool "isDeciding" and set it to true before the yield WaitforSeconds line. Only call WaitOrMove if isDeciding is set to false.