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
1
Question by Jackv949 · Dec 19, 2014 at 02:20 AM · anglebulletspread

Perfect bullet spread for top-down shooter

I am trying to create a player ability where they can shoot multiple bullets at a time, and I would like them to have a perfect spread. I have tried many things, but the closest I could get wasn't exactly perfect. As can be seen in this screenshot: alt text

In order to accomplish this, I made every odd-numbered bullet move the other way - but it just mirrors the bullet trajectory if it had been on the right side.

This is the code I used:

 GameObject FireBullet(GameObject bullet)
     {
         if (bullet && gunMuzzle)
         {
             GameObject obj = (GameObject)Instantiate(bullet, gunMuzzle.transform.position, gunMuzzle.transform.rotation);
             Physics.IgnoreCollision(gameObject.collider, obj.collider);
 
             return obj;
         }
         else
         {
             Debug.Log("Missing bullet prefab and/or muzzle transform!");
             return null;
         }
     }
 
     void FireBullet(GameObject bullet, int amount, float spreadAngle)
     {
         for (int i = 0; i < amount; i++)
         {
             float dir = 1;
 
             if (i % 2 == 0) //Checks if number is even
                 dir = -1f;
 
             GameObject obj = FireBullet(bullet);
             obj.transform.Rotate(Vector3.up, spreadAngle * i * dir);
         }
     }

How can I make a perfect and symmetrical bullet spread?

bulletspread.png (29.2 kB)
Comment
Add comment
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

1 Reply

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

Answer by VesuvianPrime · Dec 19, 2014 at 02:25 AM

  void FireBullet(GameObject bullet, int amount, float spreadAngle)
  {
      // Todo - need a special case for when amount is 1
      float perBulletAngle = spreadAngle / (amount - 1);

      for (int i = 0; i < amount; i++)
      {
          GameObject obj = FireBullet(bullet);
          obj.transform.Rotate(Vector3.up, i * perBulletAngle);
      }
  }
Comment
Add comment · Show 3 · 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 Jackv949 · Dec 19, 2014 at 02:50 AM 0
Share

Thanks for your answer, but this doesn't work the way I need it to. Your code here make all the bullets spread out to the right of the first bullet, but I need them to spread out on both sides (without having the issue shown in the picture).

avatar image VesuvianPrime · Dec 19, 2014 at 02:57 AM 0
Share

Ok, I misunderstood the original problem, lets try this:

   void FireBullet(GameObject bullet, int amount, float spreadAngle)
   {
       // Todo - need a special case for when amount is 1
       float perBulletAngle = spreadAngle / (amount - 1);
       float startAngle = spreadAngle * -0.5f;
  
       for (int i = 0; i < amount; i++)
       {
           GameObject obj = FireBullet(bullet);
           obj.transform.Rotate(Vector3.up, startAngle + i * perBulletAngle);
       }
   }

avatar image Jackv949 · Dec 19, 2014 at 03:19 AM 0
Share

It works! 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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Spreading Fire (Bullets) 3 Answers

Shoot at an angle 2D 2 Answers

Float to angle coordinate? 1 Answer

How to make a bullet spread 1 Answer

Randomly instantiate bullets within a certain angle based on look direction 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