• 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
Question by Richard_Ingalls · Nov 19, 2022 at 07:44 PM · rotationshootingbulletgunspread

How do I fire multiple bullets in an arc?

I have a variable projectileNum and when it is set to one, it works perfectly, but when it is above that, nothing I do gets it to work. If I use a for or while loop, it fires multiple bullets, but they are stacked on top of each other. I want the bullets to spread out in a fan. They will start from a certain point, and fire out at an angle. I need this to work for all integers. If there is a way to do it without using a bunch of if statements, that would be preferred. projectileNum is a float from 1-6, but will always be an integer by design. If you need more information, let me know.

edit: I need it fired based on how many bullets there are. This is my firing code:

void Shoot() { for (int i = 0; i < projectileNumber; i++) { GameObject b = Instantiate(bullet, firepoint.position, firepoint.rotation); b.transform.localScale = new Vector3(projectileSize / 5f, projectileSize / 5f, 0f); Rigidbody2D rb = b.GetComponent<Rigidbody2D>(); rb.AddForce(firepoint.up * projectileSpeed * 6, ForceMode2D.Impulse); GameObject.Destroy(b, range); } }

Comment
kbryson

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Richard_Ingalls · Nov 23, 2022 at 04:31 AM

Ok, I figured it out. I had to scrap the rb.AddForce(firepoint.up * projectileSpeed * 6, ForceMode2D.Impulse); , unfortunately, so now I have a script for the bullets:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { float obj; void Start() { obj = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().projectileSpeed; } void Update() { transform.Translate(0f, obj / 10, 0f); } }

This is what my firing code now looks like:

void Shoot() { if (projectileNumber <= 0f) { projectileNumber = 1f; } float vectZ; float angle = Mathf.Max(5f, (11f - Mathf.Pow(1.05f, projectileNumber - 2f))); if (projectileNumber % 2 == 0) { vectZ = ((projectileNumber / 2) * 10) - 5; for (int i = 0; i < projectileNumber; i++) { CreateBullet(new Vector3(0f, 0f, vectZ)); vectZ -= 10f; } } else { vectZ = ((projectileNumber - 1) / 2) * 10; for (int i = 0; i < projectileNumber; i++) { CreateBullet(new Vector3(0f, 0f, vectZ)); vectZ -= 10f; } } } void CreateBullet(Vector3 offsetRotation) { GameObject b = Instantiate(bullet, firepoint.position, firepoint.rotation); b.transform.localScale = new Vector3(projectileSize / 5f, projectileSize / 5f, 0f); b.transform.Rotate(offsetRotation); GameObject.Destroy(b, range); }

If you're curious as to how I am firing, this is the code for that.

void RefreshInvoke() { CancelInvoke("Shoot"); lastfireRate = fireRate; InvokeRepeating("Shoot", 1 / fireRate, 1 / fireRate); } void FixedUpdate() { if (fireRate != lastfireRate) { RefreshInvoke(); } }

Comment
kbryson

People who like this

1 Show 0 · 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

Answer by logicandchaos · Nov 19, 2022 at 08:06 PM

really depends on how you are moving your bullets, you should really include your code so people can help you, but if you are moving your bullets based on the it's transform.forward, then all you have to do is rotate the bullet and it will go out in a different direction.

Comment

People who like this

0 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 Richard_Ingalls · Nov 19, 2022 at 08:18 PM 0
Share

Ok, but how would I rotate it based on how many bullets there are?

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

146 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

Related Questions

Bullet Rotation faces wrong direction 2 Answers

Problem with Shooting Accuracy 0 Answers

Need help with Transform.LookAt 1 Answer

Shoot bullet in direction which player looks 0 Answers

Gun Shooting Problem 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