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 /
  • Help Room /
avatar image
1
Question by hesteefen · Jul 18, 2021 at 07:13 PM · 2d gamenewbie

I'm currently working on a top-down shooter and I want to make shotgun

I'm currently working on a top-down shooter and I want to make a shotgun but I have no idea how can I make bullet spread these are my scripts, please forgive me if I have some big mistakes:

     public Transform firePoint;
     public GameObject bulletPrefab;
  
     public int maxAmmo = 10;
     public int currentAmmo;
     public float reloadTime = 1f;
     public bool isReloading = false;
  
     public float bulletForce = 20f;
     void Start()
     {
         currentAmmo = maxAmmo;
     }
     void Update()
     {
         if (isReloading)
             return;
         if (currentAmmo <= 0)
         {
             StartCoroutine(Reload());
             return;
         }
         if (Input.GetButtonDown("Fire1"))
         {
             Shoot();
         }
     }
     IEnumerator Reload()
     {
         isReloading = true;
         Debug.Log("Reload.....");
  
         yield return new WaitForSeconds(reloadTime);
         currentAmmo = maxAmmo;
  
         isReloading = false;
     }
  
     void Shoot()
     {
         currentAmmo--;
         GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
         rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
     }
 }

this is the bullet script:

   public Rigidbody2D rb;
     public int damage = 50;
     public float speed = 10f;
  
     private void Start()
     {
         rb.velocity = transform.up * speed;
     }
     private void OnTriggerEnter2D(Collider2D other)
     {
         Enemy enemy = other.GetComponent<Enemy>();
         Destroy(gameObject);
         if(enemy != null)
         {
             enemy.takeDamage(damage);
         }
         Debug.Log(other.gameObject);
     }
 }


Comment
Add comment · Show 2
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 Anonymous620 · Jul 19, 2021 at 12:26 AM 0
Share

You could instantiate the bullets in a specified area or in a range randomly

avatar image hesteefen Anonymous620 · Jul 19, 2021 at 06:35 AM 0
Share

And how can I do that?

3 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by hesteefen · Jul 24, 2021 at 10:30 AM

I still have a problem @Anonymous620 I'm sorry I found this problem very late I just tried out the script

         if (isReloading)
             return;
         if (currentAmmo <= 0)
         {
             StartCoroutine(Reload());
             return;
         }
         if (Input.GetButtonDown("Fire1"))
         {
             Shoot();
         }
     }
     IEnumerator Reload()
     {
         isReloading = true;
         Debug.Log("Reload.....");
 
         yield return new WaitForSeconds(reloadTime);
         currentAmmo = maxAmmo;
 
         isReloading = false;
     }
 
     void Shoot()
     {
         currentAmmo--;
         for (int i = 0; i <= numBullets; i++)
         {
             bulletParent.transform.position = new Vector3(Random.Range(Centre.transform.position.x - range, Centre.transform.position.x + range),
                 Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range);
             Instantiate(bullets, bulletParent.transform.position, bulletParent.transform.rotation);
         }
     }

there is an error at end the of this line Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range);

Comment
Add comment · Show 11 · 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 Anonymous620 · Jul 24, 2021 at 10:34 AM 0
Share

What's the error?

avatar image Anonymous620 · Jul 24, 2021 at 10:39 AM 0
Share

Sorry, I've just realised that Center is already defined in unity. Try changing it to _center.

avatar image hesteefen Anonymous620 · Jul 24, 2021 at 10:44 AM 0
Share

It's the same after I changed the name

avatar image Anonymous620 hesteefen · Jul 24, 2021 at 10:45 AM 0
Share

Does it tell you the error

Show more comments
avatar image Anonymous620 · Jul 29, 2021 at 09:50 AM 0
Share

Future viewers, keep in $$anonymous$$d that this is the right answer to the code fix. this is not the actual code/answer to the question. The answer to the official question is below this with the script called BulletSpread. This problem isn't relevent any more as i have fixed the issue in the original code.

avatar image
0

Answer by Anonymous620 · Jul 19, 2021 at 11:26 AM

 public class BulletSpread : MonoBehaviour
 {
     public GameObject bullets;
     public GameObject bulletParent;
     public GameObject Centre;
     public float numBullets;
     public float range;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             SpreadBullets();
         }
     }
     void SpreadBullets()
     {
         for (int i = 0; i <= numBullets; i++)
         {
             bulletParent.transform.position = new Vector3(Random.Range(Centre.transform.position.x - range, Centre.transform.position.x + range), Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range));
             Instantiate(bullets, bulletParent.transform.position, bulletParent.transform.rotation);
         }
     }
 }
 

see if that works

Comment
Add comment · Show 6 · 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 rage_co · Jul 19, 2021 at 12:49 PM 0
Share

i think the position should not be random.....only the direction

avatar image hesteefen rage_co · Jul 19, 2021 at 06:33 PM 0
Share

that is not a problem.

thank you for helping me :)

avatar image rage_co hesteefen · Jul 20, 2021 at 02:52 AM 0
Share

Well no problem, if youl ike it that way, you can easily omit any of the two......also @Anonymous620 it would be better if you could convert your comment to an answer so that hesteefen can accept it

Show more comments
avatar image Ciphon_Gaming · Aug 04, 2021 at 05:39 AM 0
Share

is the bullet parent meant to be a child for the player or is it a enitre different gameobject?

avatar image rage_co Ciphon_Gaming · Aug 04, 2021 at 05:53 AM 0
Share

It is a child of the player gameObject, but it can be anything

avatar image
0

Answer by hesteefen · Jul 24, 2021 at 11:06 AM

I still have a question

 void Shoot ()
 {
     currentAmmo--;
     for (int i = 0; i <= numBullets; i++)
     {
         bulletParent.transform.position = new Vector3(Random.Range(Centre.transform.position.x - range, Centre.transform.position.x + range), Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range);
         Instantiate(bullets, bulletParent.transform.position, bulletParent.transform.rotation);
     }
 }

I'm not very sure why but there is an error at the end of this line

 bulletParent.transform.position = new Vector3(Random.Range(Centre.transform.position.x - range, Centre.transform.position.x + range), Random.Range(Centre.transform.position.y - range, Centre.transform.position.y + range);

I'm sorry I asked this problem a week later I was a little bit busy

Comment
Add comment · Show 5 · 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 andrew-lukasik · Jul 24, 2021 at 11:08 AM 0
Share

Vector3 constructor requires 3 floats for xyz and you provided 2.

 bulletParent.transform.position = new Vector3(
     Random.Range( Centre.transform.position.x - range , Centre.transform.position.x + range ) ,
     Random.Range( Centre.transform.position.y - range , Centre.transform.position.y + range ,
     0f
 );
avatar image Anonymous620 andrew-lukasik · Jul 24, 2021 at 11:09 AM 0
Share

Why don't I get that error???

avatar image andrew-lukasik Anonymous620 · Jul 24, 2021 at 11:14 AM 0
Share

Technically this is not an error, because Vector3 has 3 constructor variants:

 Vector3 a = new Vector3( 1f , 2f , 3f );
 Vector3 b = new Vector3( 1f , 2f );// new Vector3( 1f , 2f , 0 ) equivalent
 Vector3 c = new Vector3();// new Vector3( 0 , 0 , 0 ) equivalent
Show more comments

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

166 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

Related Questions

How to get Y-Axis movement in 2D space shooter. 0 Answers

Need 2D help - Want to have object move towards point when it touches a trigger. 0 Answers

my camera wont stay with my player after assigning the player as a target 0 Answers

How to make a stationary camera into a moving camera that follows the player when entering a new scene? 0 Answers

I have got bugs when i jump 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