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!
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.
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 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.
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.
– anon16733337As 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.
– anon16733337