• 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 SeAhmed · Dec 12, 2020 at 09:11 AM · shootingscriptable objectbulletsspreadshotgun

How to make bullets spread in unity2D?

Hello, I'm trying to make a 2d game that contains shotgun, so I don't know how exactly to make the bullets spread. I tried to write a code that may solve my problem but doesn't work.

My code:

 public class Shooting : MonoBehaviour
      {
          public Transform firePoint;
          public GameObject bulletPrefab;
          public Weapon currentWeapon;
          private float nextTimeOfFire = 0;
      
          void Update()
          {
              if(Input.GetButtonDown("Fire1"))
              {
                  if(currentWeapon)
                  {
                      if(Time.time >= nextTimeOfFire)
                      {
                          for(float x = 0;x < currentWeapon.GetBullets(); x++)
                          {
                              Shoot();
                          }
                          nextTimeOfFire = Time.time + 1 / currentWeapon.fireRate;
                      }
                  }
              } 
          }
      
          void Shoot(){
              Instantiate(bulletPrefab,firePoint.position, Quaternion.Euler(firePoint.rotation.x, firePoint.rotation.y + Random.Range(-currentWeapon.GetSpread(), currentWeapon.GetSpread()), 0));
                  
          }
      } 
 



**Note:**Weapon class is a scriptable object and that's his code:

  [CreateAssetMenu(fileName = "New Weapon", menuName = "Weapon")]
  public class Weapon : ScriptableObject
  {
      public Sprite currentWeaponSpr; 
      public GameObject bulletPrefab;
      public float fireRate;
      public int damage;
      public float bullet;
      public float spread = 0;
  
  
      public float GetFireRate(){
  
          return fireRate;
      }
  
      public int GetDamage(){
  
          return damage;
      }
  
      public float GetBullets(){
  
          return bullet;
      }
  
      public float GetSpread(){
  
          return spread;
      }
  }


if anyone can help me please give me your hand.

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

143 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 avatar image avatar image avatar image avatar image avatar image avatar image 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

Shotgun raycast 1 Answer

Issues with bullet not subtracting properly 1 Answer

Multiple Bullets 2 Answers

Crosshair Prob 1 Answer

change to raycast shooting instead of rigidbody shooting 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges