- Home /
AI Coroutines not behaving properly
I am working on NPC AI involving a loiter function. The function is pieced together by coroutines that manipulate booleans to act as a decision tree. The system works like this: NPC is set to spawn by a boolean, this boolean unlocks the spawn coroutine which spawns the NPC in and then calls a sleep coroutine to let the NPC settle physically, after the sleep timer finishes, the next coroutine, the target find coroutine, is unlocked in the update block, this coroutine locates a point based on a set range random number, this coroutine finishes and unlocks the next coroutine to check if the current position = the target position, if not, the coroutine to move the NPC is called and we check again to see if the position is reached, if it is reached, the NPC sleeps again and we start over. The problem is that the NPC never waits, it just constantly, instantly travels to the next point. I set the wait time with
yield return new WaitForSeconds(5.0f);
Am I maybe just setting a time too low? Does the 5 not represent 5 seconds? Any insight in this would be great. I can post specific areas of code as well if needed.
From your description it's not clear at all how your coroutines look like, how you actually start them and if you're waiting for one of your coroutines or not. Can you post a little bit more code than the most basic waiting instruction in a coroutine?
Btw: Yes, the line you've posted would stop the coroutine for 5 seconds and would continue after the instruction when the 5 seconds are over. $$anonymous$$eep in $$anonymous$$d that Time.timeScale affects WaitForSeconds as well.
Let's do this.
void Update () {
if (spawnWait == true) {
StartCoroutine (spawnCorout());
}
if (isIdling && spawnWait == false) {
StartCoroutine(targetCorout());
}
}
IEnumerator spawnCorout()
{
Debug.Log("spawnCoroutine()");
//StartCoroutine(sleepCorout());
yield return new WaitForSeconds(5.0f);
spawnWait = false;
isIdling = true;
}
IEnumerator sleepCorout()
{
Debug.Log("sleepCoroutine()");
yield return new WaitForSeconds(0.0f);
}
IEnumerator targetCorout()
{
Debug.Log("targetCorout()");
currentPos = transform.position;
targetPos = new Vector3(currentPos.x + (Random.Range(-idleDistance, idleDistance)), currentPos.y, currentPos.z + (Random.Range(-idleDistance, idleDistance)));
targetSet = true;
transform.LookAt(targetPos);
Vector3 eulerAngles = transform.rotation.eulerAngles;
eulerAngles.x = 0;
eulerAngles.z = 0;
transform.rotation = Quaternion.Euler(eulerAngles);
if (isIdling && spawnWait == false && targetSet == true) {
StartCoroutine(targetCheck());
}
yield return new WaitForSeconds(0.0f);
}
IEnumerator moveEnemyCorout() {
Debug.Log("moveEnemyCoroutine()");
transform.position = targetPos;
targetSet = false;
spawnWait = true;
yield return new WaitForSeconds(0.0f);
}
IEnumerator targetCheck()
{
Debug.Log("targetCheckCoroutine()");
if (transform.position == targetPos) {
targetSet = false;
spawnWait = true;
}
else {
StartCoroutine(moveEnemyCorout());
}
yield return new WaitForSeconds(0.0f);
}
Answer by Owen-Reynolds · Jul 19, 2013 at 03:34 AM
Coroutines aren't like animations, where you can Play every frame and it will ignore all but the first. If a coroutine is running, you can still run more and more copies of it.
In your case, Update will start 300 copies of spawnCorot, 1 every frame for 5 seconds (until spawnWait is set false.) Then, after 5 seconds, those 300 will "land," 1 per frame. Isn't it spraying those "started this coroutine" debugs (Collapse is unchecked?)
But, checking whether to run a coroutine every frame in Update sort of defeats the purpose. Might be easier to have a single "do X; wait a bit, do Y ... " coroutine.
Your answer
Follow this Question
Related Questions
Update SetDestination only every 0.2s with coroutines ? 5 Answers
AI script making unity no longer respond. 1 Answer
AI Coroutine or ScriptableObject ? 1 Answer
Character AI in ScriptableObjects? 1 Answer
Problems in a footstep coroutine 1 Answer