- Home /
Shoot 10 bullets at once
Hello Unity3D.I have a problem with making my bullet shoot more than one at once.In my script i can only shot one bullet at once but it doesnt shoot out randomly just diagonally upwards.I dont want that.I want it to shoot 10 bullets all at once and make make each of them move in a different direction.If anyone knows how i can do this.Can someone please tell me how?
Here's the Script
var rocket : Rigidbody;
var rocket2: Rigidbody;
var throwPower : float = 10;
var reloadTime = 10; //time to reload
var timeBetweenShots = 10.0;
var randomNumberX = Random.Range(-strayFactor, strayFactor);
var randomNumberY = Random.Range(-strayFactor, strayFactor);
var randomNumberZ = Random.Range(-strayFactor, strayFactor);
private var nextFire : float =0.0;
private var timestamp = -10.0;
var strayFactor : int;
function Update ()
{
if (Input.GetKeyDown("z")&& Time.time > nextFire)
if (Time.time > timestamp + timeBetweenShots ) {
Debug.Log("I'm firing now");
timestamp = Time.time;
Switch();
}
}
function Switch()
{
yield WaitForSeconds(1);
var bullet = Instantiate(rocket, transform.position, transform.rotation);
bullet.transform.Rotate(randomNumberX, randomNumberY, randomNumberZ);
bullet.rigidbody.AddForce(bullet.transform.forward * 10000);
}
Please reformat the code (at the top) and remove the whitespace so that it is readable :)
Sort of, it is still a mess...
With this :
if (Input.Get$$anonymous$$eyDown("z")&& Time.time > nextFire)
if (Time.time > timestamp + timeBetweenShots ) {
Debug.Log("I'm firing now");
timestamp = Time.time;
Switch();
}
}
Do you mean this :
if (Input.Get$$anonymous$$eyDown("z") && Time.time > nextFire) {
if (Time.time > timestamp + timeBetweenShots) {
Debug.Log("I'm firing now");
timestamp = Time.time;
Switch();
}
}
Answer by FirePlantGames · Dec 16, 2014 at 11:26 PM
var seperation : float = 2.0;
for (var i : int = 0;i < 10; i++) {
Instantiate (rocket, transform.position, transform.rotation + Quaternion(0,i * seperation ));
}
replace the instantiate with the for loop.
Like This?Because unity keeps telling me it wrong?
You shouldn't comment as an answer, and if you did this;
var seperation : float = 2.0;
function Switch()
{
yield WaitForSeconds(1);
for (var i : int = 0;i < 10; i++) {
var bullet = Instantiate (rocket, transform.position, transform.rotation + Quaternion(0,i * seperation ));
}
bullet.rigidbody.AddForce(bullet.transform.forward * 10000);
}
then it should work.