Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vulgarknight · Jul 19, 2013 at 01:02 AM · aicoroutinetiming

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 19, 2013 at 01:19 AM 0
Share

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.

avatar image vulgarknight · Jul 19, 2013 at 01:39 AM 0
Share

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);
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges