- Home /
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!
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.
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?
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.
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.
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???
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.
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
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
Follow this Question
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