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 /
avatar image
0
Question by majedmadev · Apr 04, 2020 at 09:32 AM · shotgun

ShotGun Spread Unity,How to Add bullet spread to a shotGun

I am trying to make an fps game and I have an inventory system implemented. However when I add the shotgun and then shoot it the bullets do spread but they spread on the horizontal axis and it looks kinda odd. Can some one walk me through how to implement proper shooting with bullet spread. Here is what I have so far. another issue with that is that when the player is looking left the bullets would not spread but instead they would go in line with each other.

     public int BulletsShot; // Total bullets show per Shot of the gun
     public float BulletsSpread; // Degrees (0-360) to spread the Bullets
     public GameObject BulletTemplate; // Bullet to fire
 
     public void Fire()
     {
         float TotalSpread = BulletsSpread / BulletsShot;
         for (int i = 0; i < BulletsShot; i++)
         {
             // Calculate angle of this bullet
             float spreadA = TotalSpread * (i + 1);
             float spreadB = BulletsSpread / 2.0f;
             float spread = spreadB - spreadA + TotalSpread / 2;
             float angle = cam.transform.eulerAngles.y;
 
             // Create rotation of bullet
             Quaternion rotation = Quaternion.Euler(new Vector3(0, spread + angle, 0));
 
             // Create bullet
             GameObject bullet = Instantiate(BulletTemplate, tip.position, tip.rotation);
             bullet.GetComponent<Rigidbody>().AddForce(transform.forward * pelletFireVel);
         }
 
     }

Thanks in advance.

Comment
Add comment · Show 1
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 SmokeyWitch · Apr 04, 2020 at 11:16 AM 0
Share
 public class shoot : $$anonymous$$onoBehaviour
 {
     public int BulletsShot; // Total bullets show per Shot of the gun
     public GameObject BulletTemplate; // Bullet to fire
     public Transform shootTransform;
     public float maxSpread = 90f;
     public void Fire()
     {
         for (int i = 0; i < BulletsShot; i++)
         {
             var randomX = Random.Range(-maxSpread, maxSpread);
             var randomY = Random.Range(-maxSpread, maxSpread);
             var randomZ = Random.Range(-maxSpread, maxSpread);
 
 
             // Create bullet
             GameObject bullet = Instantiate(BulletTemplate, shootTransform.position, shootTransform.rotation);
             bullet.transform.Rotate(randomX, randomY, randomZ);
             bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward * 500);
         }
 
     }
     // Start is called before the first frame update
     void Start()
     {
         Fire();
     }
 
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by n_rusev · Apr 04, 2020 at 09:54 AM

Hello. I think you are overcomplicating the problem. When you consider a real life shotgun the bullets will not spread in billboard fashion (although it might look like that) as you are trying to do, but in a 3d volume instead. Probably just adding some randomness in all directions to your AddForce vector (line 21) will do it just fine. Maybe something like this:

 //Declare public float to edit in unity editor
 public float maxSpread;
 
 //Add randomness to every bullet direction
 Vector3 dir = transform.forward + new Vector3(Random.Range(-maxSpread,maxSpread), Random.Range(-maxSpread,maxSpread), Random.Range(-maxSpread,maxSpread));
 bullet.GetComponent<Rigidbody>().AddForce(dir * pelletFireVel);

Because in real life the buckshot inside the shell will interact with each other and some will slow down, some will shift up, down, left and right, and that is what adding randomness to add directions will do.

Also, name your variables starting with lower case letter - much convenient to read and no problems with var names being the same as type names.

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

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

How to make my gun semi-auto? 1 Answer

Unity 2D shogun,Unity 2D shogun spread? 0 Answers

Shotgun Spread Issue 1 Answer

How to make shotgun spread in Unity 2D (without Raycasting) 2 Answers

Use the line renderer for simulate shotgun shoot 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