Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Misterquick · Jun 08, 2016 at 10:28 AM · coroutinecoroutinescoroutine errors

Coroutine Not working properly.

Hey everyone, I am trying to make an AI shooter script where I want the enemy to shoot once every 7 seconds. Then if the enemy reaches a certain point, he will shoot once every 3 seconds. However, the enemy doesn't wait 7 seconds to shoot, but rather he shoots like 20 bullet in less than a second (no joke). Can someone please tell me what I am doing wrong. I am a beginner btw. Here is my code.

     void Update()
     {
         if (true)
         {
           StartCoroutine(Shoot());
         }
 
 
         if ((bossHealth.currentHealth <= 0) && (bossHealth2.currentHealth >0))
         {
             navMeshAgent2.speed = 10;
         }
 
         if ((bossHealth2.currentHealth <= 0) && (bossHealth.currentHealth > 0))
         {
           //  shootTime = 2f;
         }
 
     }
 
 
     IEnumerator Shoot()
     {
         if (projectile)
         {
             yield return new WaitForSeconds(7);
 
             // Instantiante projectile at the camera + 1 meter forward with camera rotation
             GameObject newProjectile = Instantiate(projectile, transform.position + transform.forward, transform.rotation) as GameObject;
 
             // if the projectile does not have a rigidbody component, add one
             if (!newProjectile.GetComponent<Rigidbody>())
             {
                 newProjectile.AddComponent<Rigidbody>();
             }
             // Apply force to the newProjectile's Rigidbody component if it has one
             newProjectile.GetComponent<Rigidbody>().AddForce(transform.forward * power, ForceMode.VelocityChange);
         }
     }
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 ericbegue · Jun 08, 2016 at 11:06 AM 0
Share

I suggest to learn about Behaviour Tree. It's a good alternative to coroutine to define logics that unfold in time.

Have a look at Panda BT (www.pandabehaviour.com), it's a script-based Behaviour Tree framework. The package contains an example that might interest you: it's a third person shooter with various enemy AIs.

If you have any question about using this tool do manage your AI, you're welcome on this thread.

avatar image Misterquick ericbegue · Jun 12, 2016 at 01:53 AM 0
Share

ok i will read up on it, thank you.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by YoungDeveloper · Jun 08, 2016 at 10:44 AM

      if (true){
        StartCoroutine(Shoot());
      }

You are starting a coroutine every frame. Instead you should be starting one only if there isn't already one working, in your case you need only one at the time. You can do that by simple bool flag. Here's an example.

 private bool corotineActive = false;
 
 void Update(){
     if(!corotineActive){
            Debug.log("update found that no coroutine is running, launch one");
            corotineActive = true;
            StartCoroutine(Shoot());
     }
 }
 
 IEnumerator Shoot(){
     Debug.log("started");
      yield return new WaitForSeconds(7);
      corotineActive = false;
     Debug.log("finished");
 }

Comment
Add comment · Show 1 · 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
avatar image Misterquick · Jun 12, 2016 at 01:55 AM 0
Share

thank you

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers

Unity C# Punching Coroutine not completing 0 Answers

Add points every five seconds 1 Answer

Coroutine cancels for no reason 0 Answers

Array reference being changed without me knowing, or, When a coroutine is run on a monobehavior and then stopped with StopCoroutine (coroutine) it stops the same coroutine being run on another instance of the same monobehavior on a different object. 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