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 Sam_da_dev · Sep 27, 2020 at 12:28 PM · 2d2d gameunity 2dunityeditorshotgun

how to make a bullet spread ( shotgun ) in unity 2D

okay so I am working on a 2D shooting game and I made a script which makes the gun aimed at the direction of the mouse cursor, and I have succeeded into making a pistol and an assault rifle but I don't really know how to make a shotgun and I don't know how to make the bullets spread randomly in the direction of the mouse cursor | here is the code of the basic pistol

 public class Pistol : MonoBehaviour
 {
 
     public Transform firePoint;
     public GameObject bulletPrefab;
     public GameObject reloadingText;
 
     public int maxAmmo = 10;
     private int currentAmmo;
     public float reloadTime = 1f;
     private bool isReloading = false;
 
     void Start()
     {
         currentAmmo = maxAmmo;
     }
 
     void Update()
     {
 
         if (isReloading)
             return;
 
         if (currentAmmo <= 0)
         {
             StartCoroutine(Reload());
             return;
         }
         
             if (Input.GetButtonDown("Fire1"))
             {
 
                 
                 Shoot();
             }
         
     }
 
     IEnumerator Reload()
     {
         isReloading = true;
 
         reloadingText.SetActive(true);
 
         yield return new WaitForSeconds(reloadTime);
 
         reloadingText.SetActive(false);
 
         currentAmmo = maxAmmo;
         isReloading = false;
     }
     void Shoot()
     {
 
         currentAmmo--;
 
         Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
     }
 }


and here is the code of the bullet

 public class Bullet : MonoBehaviour
 {
 
     public float speed = 20f;
     public Rigidbody2D rb;
     public GameObject bulletCollisionEffect;
     public int damage = 40;
 
     // Start is called before the first frame update
     void Start()
     {
         rb.velocity = transform.right * speed;
     }
 
     void OnTriggerEnter2D (Collider2D hitInfo)
     {
         Enemy enemy = hitInfo.GetComponent<Enemy>();
         if (enemy != null)
         {
             enemy.TakeDamage(damage);
         }
         Destroy(gameObject);
         Instantiate(bulletCollisionEffect, transform.position, Quaternion.identity);
         
        
        
     }
   
 }
 

help would be really appreciated.

edit: I forgot to put the code where I make the gun aim at the mouse cursor :

 public class Pivot : MonoBehaviour
 {
 
     public GameObject myPlayer;
 
     // Update is called once per frame
     private void FixedUpdate()
     {
         Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
 
         difference.Normalize();
 
         float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
 
         transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
 
         if (rotationZ < -90 || rotationZ > 90)
         {
             if (myPlayer.transform.eulerAngles.y == 0)
             {
                 transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
 
             } else if (myPlayer.transform.eulerAngles.y == 180) {
 
                 transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
 
             }
         }
     }
 }
 

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

3 Replies

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

Answer by N-8-D-e-v · Sep 27, 2020 at 01:32 PM

Check this out

Comment
Add comment · Show 7 · 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 SpeckledGecko · Sep 27, 2020 at 03:33 PM 0
Share

N8 you started to answer a question I asked ages ago, i do still need help with it so , you know, just sayin

avatar image N-8-D-e-v SpeckledGecko · Sep 27, 2020 at 05:53 PM 0
Share

sorry! Would you $$anonymous$$d linking the question?

avatar image SpeckledGecko N-8-D-e-v · Sep 28, 2020 at 08:01 PM 0
Share

https://answers.unity.com/questions/1769950/transfor$$anonymous$$g-and-playing-particle-effects.html

Show more comments
avatar image Bloodhunteryt SpeckledGecko · Sep 27, 2020 at 05:56 PM 0
Share

oof thats a fat rip

avatar image Sam_da_dev · Sep 28, 2020 at 06:45 PM 0
Share

i am in 2D not in 3D

avatar image N-8-D-e-v Sam_da_dev · Sep 28, 2020 at 10:55 PM 0
Share

just set the rotations to different things using a for loop

avatar image
2

Answer by Reid_Taylor · Sep 27, 2020 at 07:21 PM

Well looking over your code I don't see anywhere where the bullets look toward the mouse but im guessing its something like this:

 public void Shoot ( )
 {
      firePoint.transform.LookAt(Input.mousePosition);
      Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
 } 

For a shot gun I would do something like this:

 public void Shoot ( )
 {
      firePoint.transform.LookAt(Input.mousePosition);
 
      int bulletCount = 10;
      float spread = 1;
      Quaternion newRot = firePoint.rotation;
 
      for (int i = 0; i < bulletCount; i++)
      {
           float addedOffset =  (i - (bulletCount / 2) * spread;
 
           // Then add "addedOffset" to whatever rotation axis the player must rotate on
           newRot = Quaternion.Euler(firePoint.transform.localEulerAngles.x,             
           firePoint.transform.localEulerAngles.y, 
           firePoint.transform.localEulerAngles.z + addedOffset);
 
           Instantiate(bulletPrefab, firePoint.position, newRot);
      }
 } 

Note: this isn't tested and I don't have a lot of experience in 2d lemme know if somethings wrong :)

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 Sam_da_dev · Sep 28, 2020 at 05:36 PM 0
Share

oh sorry, here is the code where I make the gun aim at the mouse cursor

 public class Pivot : $$anonymous$$onoBehaviour
 {
 
     public GameObject myPlayer;
 
     // Update is called once per frame
     private void FixedUpdate()
     {
         Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
 
         difference.Normalize();
 
         float rotationZ = $$anonymous$$athf.Atan2(difference.y, difference.x) * $$anonymous$$athf.Rad2Deg;
 
         transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
 
         if (rotationZ < -90 || rotationZ > 90)
         {
             if (myPlayer.transform.eulerAngles.y == 0)
             {
                 transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
 
             } else if (myPlayer.transform.eulerAngles.y == 180) {
 
                 transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
 
             }
         }
     }
 }
 
avatar image Sam_da_dev · Sep 28, 2020 at 08:56 PM 0
Share

and yeah there is a problem, I added the code, and when I try to shoot, the bullet doesn't go forward and it just activates the particle system at the fire point @Reid_Taylor

alt text

screenshot-4.png (15.8 kB)
avatar image Reid_Taylor Sam_da_dev · Sep 29, 2020 at 07:05 PM 0
Share

Like @vishwah13 said try using eulerAngles instead of localEulerAngels

avatar image
0

Answer by vishwah13 · Sep 28, 2020 at 03:18 PM

@Reid_Taylor
i found it it works use firePoint.trasform.eulerAngles instead of localEulerAngles

My code looks like this

  if (timeBetweenShots <= 0)
         { 
             quaternion newRot = gunPoint.rotation;
             
             for (int i = 0; i < bulletCount; i++)
             {
                 float addedOffset = (i - (bulletCount / 2) * spread);
                 
                 newRot = Quaternion.Euler(gunPoint.eulerAngles.x,gunPoint.eulerAngles.y,gunPoint.eulerAngles.z + addedOffset);
                 Instantiate(projectile,gunPoint.position,newRot);
             }
             timeBetweenShots = startTimeBetweenShots;
         }
         else
         {
             timeBetweenShots -= Time.deltaTime;
         }

Comment
Add comment · Show 1 · 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 Reid_Taylor · Sep 29, 2020 at 07:04 PM 0
Share

Oh yah sorry my bad

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

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

overhead game in 2d world? 0 Answers

Accurate Text 0 Answers

Unity 2D - Use Digital Painting as Terrain 1 Answer

i have a problem with reloading with Time.time 2d game c# Unity 2020.2 1 Answer

Input Field Characer Validation not changing 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