• 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
Question by LeftyTwoGuns · Jun 26, 2017 at 04:43 AM · instantiatecoroutineprojectileienumeratoryield waitforseconds

IEnumerator not looping correctly?

I have a pretty basic burst fire function for a gun- as in shooting three times in a row. Here is the script:

 void Update ()
     {
         timer += Time.deltaTime;
 
         if (Input.GetButton("Fire1") && timer >= fireRate && Time.timeScale != 0)
            {
                if (burstFire)
                   {
                     StartCoroutine(BurstShot());
                   }
             }
     }
 
  IEnumerator BurstShot()
     {

        for(int i = 0; i < burstSize; i++)

         {
             timer = 0f;

             Quaternion bulletRot = Quaternion.Euler(90, 0, 0);

             GameObject newBullet = Instantiate(bulletObj, transform.position, transform.rotation * bulletRot) as GameObject;

             newBullet.GetComponent<Rigidbody>().velocity = bulletSpeed * transform.forward;

             currentAmmo--;

             yield return new WaitForSeconds(burstRate);
         }
        
     }


The problem is, while the first projectile will shoot forward correctly, the next two projectiles will launch upwards in a near perfect arc. And for the life of me I can't figure out why. Why is everything in the coroutine repeating correctly except for the apparent rotation of the last two shots?

Comment

People who like this

0 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 Jwizard93 · Jun 26, 2017 at 11:22 PM 0
Share

Do The last two shots go in the same direction? I'm not sure what you mean by "upwards in a near perfect arc."

avatar image LeftyTwoGuns Jwizard93 · Jun 27, 2017 at 06:17 PM 0
Share

The last two shots are projected in the correct direction, just at a different angle it seems. The last two shots seem to be instantiated at a difference of about 50 degrees on the X axis from the first shot, which causes them to project up into the air instead of directly forward

1 Reply

  • Sort: 
avatar image

Answer by NerdRageStudios · Jun 27, 2017 at 02:44 AM

Perhaps your bullets are colliding with each other when you instantiate them, thus screwing up their rotation?

Comment

People who like this

0 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 LeftyTwoGuns · Jun 27, 2017 at 06:19 PM 0
Share

I had thought this at first but the projectiles are set to ignore collision with themselves in the Project Settings Physics matrix. Also this same code works with my shotgun method, where it instantiates a specific number of projectiles to fire at once

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

84 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 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 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 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 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

yield return new WaitForSeconds () not working 1 Answer

Working projectile scripts do not work due to if condition 1 Answer

Make multiple Projectiles move at the same Time... 0 Answers

WaitForSeconds is breaking out of my IEnumerator 1 Answer

IEnumerator's did not read "bool" after yield return new WaitForSeconds. 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