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 sloopernine · Jul 14, 2013 at 04:48 PM · shootbulletsshotgun

Shotgun bullet spread

Hi, I have this bit of code, thought it would be best to use a for loop for this one. But how would I best do to spread the bullets but still have all go forward and spread by time?

             if (Input.GetButton("Fire1") && timeStamp <= Time.time)
             {
                 for (int i = 0; i < amountBullets; i++)
                 {
 
                     GameObject shot = GameObject.Instantiate(shotgunShell, transform.position + (transform.forward * 1), transform.rotation) as GameObject;
                     shot.rigidbody.AddForce(transform.forward * bulletSpeed);
 
                 }
      }
 }
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
5
Best Answer

Answer by robertbu · Jul 14, 2013 at 06:03 PM

You have a few technical hurtles in order to make this work. The first is that you need all the projectiles in the spread to ignore each other. If you don't, the projectiles will hit each other and bounce all over the place. You could disable their colliders and use Raycasting for the hits and collisions. You could use Physics.IgnoreCollision() to disable the collisions between the different bullets. If you do use Physics.IgnoreCollision(), it might be best to rewrite your code so that the bullets pool (are reused) rather than instantiating and destroying the bullets.

If you a Google search for "unity3d shotgun spread," you will find a number of hits that include scripts. In looking through them, I was concerned about the pattern of the hits with some of them, and one answer was just wrong. So I'm offering this (untested) function as an alternate method of calculating a spread:

 #pragma strict
 
 function Spread(aim : Vector3, distance: float, variance : float) : Vector3 {
     aim.Normalize();
     var v3 : Vector3;
     do {
         v3 = Random.insideUnitSphere;
     } while (v3 == aim || v3 == -aim);
     v3 = Vector3.Cross(aim, v3);
     v3 = v3 * Random.Range(0.0, variance);
     return aim * distance + v3;
 }

You pass it the aim vector (transform.forward in your case), and it returns a new aim vector that has a maximum radius variance from the original aim position at the specified distance. In other words, at the specified distance, your spread will be no more that 2 * variance across.

Comment
Add comment · Show 4 · 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 robertbu · Jul 14, 2013 at 06:43 PM 0
Share

One solution I've seen for shotguns is to create the decals at the hit points using a Raycast, and not shoot any projectiles at all.

avatar image sloopernine · Jul 14, 2013 at 06:48 PM 0
Share

Nice one! Thanks!

EDIT

Actually could you explain the code a bit with comments or something so I can learn how it works? Even though it works I want to know how it works :D

avatar image robertbu · Jul 14, 2013 at 07:35 PM 0
Share

I wanted to create a vector perpendicular to the aim vector. So I generate an arbitrary vector that that is not the aim vector (lines 6 - 8). The cross product between the aim vector and the arbitrary vector creates the perpendicular vector (line 9). Then the perpendicular vector is sized to the variance (line 10). Finally I create an aim vector with a magnitude of 'distance' and then offset it by the variance.

Note this does not create a uniform distribution across the possible disc. Ins$$anonymous$$d it it will have a higher percentage of the shots towards the center. But that is how I visualize a shotgun pattern, and the pattern should be consistent no matter where the gun is aimed.

$$anonymous$$ost of the answers I reviewed just made a random modification to the x, y, and z rotation of the aim vector. I was concerned these solutions would produce patterns that differed depending on the direction of the aim.

avatar image sloopernine · Jul 14, 2013 at 08:42 PM 0
Share

I am really happy with the bullets as they are, I thought of raycasts aswell, but will only take that road if needed. At the moment I added trails to the bullets and it looks awesome! $$anonymous$$ight test to make some kind of plain object always facing the camera or something later on. At the moment I dont have any path with the game, I just play around and have fun. Unity is like a sandbox game itself (T$$anonymous$$) :D

Thanks! really appreciate your help and comments mate!

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

16 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

Related Questions

Shooting at mouse position 1 Answer

bullets don't go forward 1 Answer

Issues with bullet not subtracting properly 1 Answer

2D Bullet texture follow ray and Bullet Holes 1 Answer

How to make bullets spread in unity2D? 0 Answers


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