• 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 MaskedRecruit · Oct 02, 2021 at 11:26 PM · 2d gamebeginner2d rotationtop down shooter

How do I give my top down game bullet spread?

I'm currently in the process of making my first game, and I'm trying to figure out how to add a small amount of bullet spread to my gun. I tried out a few solutions but they didn't work for me (though that was mostly copying some code and being confused). Would anyone give me a hand?

Here is my code for shooting:

     public Transform firePoint;
     public GameObject bulletPrefab;
 
     public float bulletForce = 20f;
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetButtonDown("Fire1"))
         {
             Shoot();
         }
 
         void Shoot()
         {
             GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
             Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
             rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
         }
 
     }
 }
 

And here is my code for looking at the mouse: public class PlayerMovement : MonoBehaviour { public float moveSpeed = 10f;

     public Rigidbody2D rb;
     public Camera cam;
 
     Vector2 movement;
     Vector2 mousePos;
 
     void Update()
     {
         movement.x = Input.GetAxisRaw("Horizontal");
         movement.y = Input.GetAxisRaw("Vertical");
 
         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
     }
 
     void FixedUpdate()
     {
         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
 
         Vector2 lookDir = mousePos - rb.position;
         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
         rb.rotation = angle;
     }
 }

If you're wondering, all of this code is from Brackey's tutorials so far.

Comment
Add comment · Show 3
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 · Oct 03, 2021 at 12:32 AM 1
Share

Do you need the bullets to spread out over time (rotate the bullets) or do you just need the bullets to be positioned in a spread and go in the same direction?

avatar image MaskedRecruit Anonymous620 · Oct 03, 2021 at 12:43 AM 1
Share

I just need the bullets to be slightly spread out from the start, that way the guns aren't laser accurate.

avatar image Anonymous620 MaskedRecruit · Oct 03, 2021 at 01:09 AM 0
Share

sorry for all the questions but does the gun shoot multiple bullets at a time or is it just a single bullet

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Anonymous620 · Oct 04, 2021 at 12:11 AM

Here is some code that makes the bullet instantiate in a specified range from the centre point. this also allows you to instantiate multiple bullets at a time like a shotgun.

 public GameObject bullets;
     public GameObject bulletParent;
     public GameObject centrePoint;
     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(centrepoint.transform.position.x - range, centrePoint.transform.position.x + range), centrePoint.transform.position.y);
             Instantiate(bullets, bulletParent.transform.position, bulletParent.transform.rotation);
         }
     }

the centrePoint will need to be a child of the player and the bulletParent should be a child of the centrePoint. the numBullets is the number of bullets that you want to have shoot. the range is how far the bullets can instantiate from the centrePoint. let me know if you have any issues with it

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 Anonymous620 · Oct 10, 2021 at 03:06 AM 0
Share

@MaskedRecruit Just checking if this worked for you. I didn't write this code for a top down shooter but rather an fps and only edited it for a top down shooter. so i am not sure that it works for a top down shooter. Can you please let me know if this worked for you. thanks

avatar image MaskedRecruit Anonymous620 · Oct 10, 2021 at 04:26 AM 0
Share

Oh shoot, sorry I forgot that I posted this. I tested out the code, but I don't know if it properly spread them out. I ran into the issue that the bullets all collided with eachother the moment they spawned, so they all just got destroyed instantly

avatar image Anonymous620 MaskedRecruit · Oct 11, 2021 at 09:40 PM 0
Share

If your code destroys the bullets when oncollisionenter or ontriggerenter is triggered, you will need to add an if statement like this:

     if(!collision.gameobject.CompareTag("Bullet Tag"){
             Destroy(this.gameobject);
     }

this if statement means that the destroy code won't run if 2 bullets are colliding.

avatar image MaskedRecruit Anonymous620 · Oct 11, 2021 at 04:23 PM 0
Share

I'm testing it again and now I'm having a different issue. The code is just changing where the bullets spawn, not the angle that they're facing. What I'm trying to change is the angle that they travel.

avatar image Anonymous620 MaskedRecruit · Oct 11, 2021 at 09:31 PM 0
Share

ok i wasn't sure i'll change it

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

220 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

Related Questions

2D fixed cam aim problem 0 Answers

2D top down shooter can only aim to the top right (mouse aim) 0 Answers

Hi Friends, I am a beginner in Unity world also I am crawling child in C#.. I want to Flip My Character According to My Touch Position left or right.. Now I can flip character but not according the touch its just flip whenever i touch.. 0 Answers

I can't seem to rotate my character depending on what side he is running to (2D sidescroller) 0 Answers

top down 2d shooter direction of enemies 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