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
0
Question by spencerym · Aug 18, 2021 at 03:14 AM · shootingshotgun

How to make shotgun bullet spread?, How to make bullet spread for shotgun?

Please Help! I'm making a trap shooting practice game and I am trying to make my shotgun bullet spread. My Mouse Shoot code looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(AudioSource))]
 public class MouseShoot : MonoBehaviour
 {
     public float range = 5000;
     public float damage = 10;
     public AudioClip fireSoundEffects;
 
     private Camera fpsCam;
     private AudioSource source;
 
     void Start()
     {
         fpsCam = Camera.main;
         source = GetComponent<AudioSource>();
     }
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             source.PlayOneShot(fireSoundEffects);
             Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
 
             RaycastHit hit;
             if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, range))
             {
                 if (hit.collider.GetComponent<IDamagable>() != null)
                 {
                     hit.collider.GetComponent<IDamagable>().TakeDamage(damage);
                 }
             }
         }
     }
 }

IDamagable code looks like this:

 public interface IDamagable
 {
     void TakeDamage(float damageAmount);
 }
 
   
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
0

Answer by Acdia · Aug 18, 2021 at 06:51 AM

Hi! You shoot the raycast in the direction fpsCam.transform.forward . So you could add fpsCam.transform.right and .up after multiplying them with some small random number (-0.1 to +0.1). (You could take the desired angle and use the sine function on it to get the max value for the factor.) This would result in a rectangel for the spray.

Here is the modified code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(AudioSource))]
 public class MouseShoot : MonoBehaviour
 {
     public float range = 5000;
     public float damage = 10;
     public float sprayFactor = 0.1f;
     public int numberOfProjectiles = 10;
     public AudioClip fireSoundEffects;
 
     private Camera fpsCam;
     private AudioSource source;
 
     void Start()
     {
         fpsCam = Camera.main;
         source = GetComponent<AudioSource>();
     }
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             source.PlayOneShot(fireSoundEffects);
             Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
 
             for(int i = 0; i < numberOfProjectiles; i++)
             {
 
                 Vector3 direction = fpsCam.transform.forward;
                 direction += fpsCam.transform.up * Random.Range(-sprayFactor, sprayFactor);
                 direction += fpsCam.transform.right * Random.Range(-sprayFactor, sprayFactor);
 
                 RaycastHit hit;
                 if (Physics.Raycast(rayOrigin, direction.normalized, out hit, range))
                 {
                     if (hit.collider.GetComponent<IDamagable>() != null)
                     {
                         hit.collider.GetComponent<IDamagable>().TakeDamage(damage);
                     }
                 }
             }
         }
     }
 }

If you want it to be a circle, you can set a maximum value for the spread. Next you get a random number, which is smaller, than that max value (and use it as the factor for the first direction - up or right), square it, and subtract it from the square of the max value. Form the root of the result and you got your second factor, for the other direction (up or right). root(maxValue² - firstFactor²) = second factor.

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 spencerym · Aug 19, 2021 at 12:39 AM 0
Share

Hi, can you please change my code for this? I am completely new to unity and C# so I'm not quite sure what to do. Thank you so much!

avatar image Acdia spencerym · Aug 19, 2021 at 09:49 PM 0
Share

Sure! But I'll need to do it tomorrow when my brain is awake :)

avatar image spencerym Acdia · Aug 20, 2021 at 01:33 AM 0
Share

Thank you so much! This would help a lot, Thank you!

avatar image spencerym · Aug 20, 2021 at 11:55 PM 0
Share

Thank you so much! This would help a lot!

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 do I get AddRelativeForce to work? 0 Answers

Shotgun raycast 1 Answer

How to make bullets spread in unity2D? 0 Answers

Raycast shooting with prefab 0 Answers

Stop shooting infinite bullets with touch 3 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