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.

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