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
0
Question by brian 1 · Oct 25, 2010 at 05:03 PM · bulletmultiplespreadshotgun

Multiple Bullets

I was wondering how i can instantiate more then 1 bullet when the fire button is pressed. I currently have this code which just shoots a projectile wherever the player is faced:

var speed : int; var cratePrefab:Transform;

//static var canShoot = false;

function Update() { if(Input.GetButtonDown("Fire1")) { //only shoots if player has ammo if(Collisions.GRENADE_AMMO > 0) { Collisions.GRENADE_AMMO -= 1; //GameObject.Find("g_count").guiText.text=""+Collisions.GRENADE_AMMO; var crate = Instantiate(cratePrefab, GameObject.Find("shootpoint").transform.position, Quaternion.identity); //crate.rigidbody.AddRelativeForce(Vector3.forward speed); crate.rigidbody.velocity = transform.forward speed; } } }

After i get more then 1 bullet to be instantiated, i need it to spread like a shotgun effect. It's a 2D shooter by the way...thanks in advance!

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

2 Replies

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

Answer by skovacs1 · Oct 25, 2010 at 07:34 PM

To instantiate more than one of something, you simply call instantiate more than once.

To spread out your instantiated objects, you would spread out their instantiated positions and/or rotations as needed.

Here's an example with a random spread:

var bulletPrefab : Transform; var muzzle : Transform; var spread : float = 60.0f; var numberOfBullets : int = 6; var muzzleVelocity : float = 50.0f;

function Update() { if(Input.GetButtonDown("Fire1")) { var spreadRange : float = spread / 2.0f; for(var i : int = 0; i < numberOfBullets; i++) { //If you want a fixed spread, then use that here var variance : float = Random.Range(-spreadRange, spreadRange); var rotation : Quaternion = Quaternion.AngleAxis(variance, transform.up); var bullet : Transform = Instantiate(bulletPrefab, muzzle..position, rotation muzzle.rotation) as Transform; //Beware of bullets overlapping with each other and the gun. //This can generate collisions and cause all kinds of problems. //Using Physics.ignoreCollision or Physics.ignoreLayerCollision //should help with this. bullet.rigidbody.velocity = bullet.forward muzzleVelocity; } } }

There are some things in your posted code that could use improvement:

  • Not really wrong, but a stylistic choice to lessen indentation - you have two separate if statements, one inside the other with nothing else inside the enclosing if and this could be shortened with the use of AND if(Input.GetButtonDown("Fire1") && Collisions.GRENADE_AMM > 0)
  • you use GameObject.Find which is very slow. You should try GameObject.FindWithTag to find tagged objects which is much faster. In most cases, you can store the object and you would only have to find it once or very seldom at worst. If you store the variable as I have done above, then you can just set it in the editor.
  • You are Instantiating with Quaternion.identity. Because of this, the AddRelativeForce would always be in the same direction. If you wanted to align your instantiated object to the world, then that's the correct choice. If you want to fire in the direction of the gun or whatever, then you should use the gun's rotation.
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 brian 1 · Oct 25, 2010 at 09:14 PM 0
Share

i cleaned up the script with your help...but when i try to use the code you provided im getting a number of errors telling me to "insert semicolon at the end"...the script context looks good...what can be the problem?

avatar image skovacs1 · Oct 26, 2010 at 02:30 PM 0
Share

Yeah. Not sure about the semi-colon errors. The only problem I found was the improper var declaration for the loop control variable which I have corrected in the post.

avatar image skovacs1 · Oct 26, 2010 at 02:40 PM 0
Share

As for the bunching at the sides - This is your bullets colliding with each other. As mentioned in the comments of the code, you should disable collisions between your bullets either explicitly or by having them on a layer for which self-collisions have been disabled. Using Debug.DrawRay(muzzle.position, bullet.forward * 1000.0f, Color.yellow); you can easily see that the spread is fine. As for why it seems most present around the sides is still a bit of a mystery, but I would likely say it has something to do with the implementation of Unity's physics.

avatar image brian 1 · Oct 26, 2010 at 04:18 PM 0
Share

I changed my bullet instance to a particle...i like the effect a lot more and it seems like im not having problems with the bunching up on the sides...but my $$anonymous$$AIN problem is collision detection against the walls...i've been trying multiple ways to have the particles collide with my walls and destroy themselves but thats not the case....any ideas on that subject???

avatar image skovacs1 · Oct 26, 2010 at 10:52 PM 0
Share

As said, it was a problem of collisions. As particles are not affected by the same self-collisions, they don't have that problem. Your $$anonymous$$AIN problem is really a separate question and should be considered as such. I'm fairly certain that question has already been asked, but don't have the time atm to seek it out for you. The simple answer is in a collision function attached to one of the colliding objects put 'Destroy(bullet.gameObject);' or something to that effect. With particles, it becomes a little trickier - you need to get the particles, find and remove the one that hit and re-assign them.

Show more comments
avatar image
0
Best Answer

Answer by brian 1 · Oct 26, 2010 at 01:48 AM

I got the script to work and it fires in a good random spread i really appreciate it...only one thing that i can't find out...when my character turns 90 degrees (exact right or left), the bullets are stacked together when they are shot...

but when my player is facing up or down, the spread works fine

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 skovacs1 · Oct 26, 2010 at 02:33 PM 0
Share

This isn't an answer to the question, but rather a comment/question and should have been added to the actual answer posted or as an edit to the question. If the posted answer to the question was the correct one, please mark it as the correct answer. If you have questions or comments on the posted answer, please group them with the answer rather than adding them as another separate answer.

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

No one has followed this question yet.

Related Questions

How to make bullets spread in unity2D? 0 Answers

How to make a bullet spread 1 Answer

Perfect bullet spread for top-down shooter 1 Answer

Shoot in proper direction with spread? 1 Answer

How to do bullet spread in 2D? 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