How to instantiate bullets in multiple transforms
Hi, can anyone help me. I am tempting to use multiple transforms to instantiate multiple bullets at the same time, but i can't make the script spawn bullets from the array of transforms. Thanks for helping. This is my code:
 public Transform [] gunTip;
     public Rigidbody bullet;
 
     public float fireRate;
     public int Ammo;
     public int curAmmo;
     public int ammoConsumed;
 
     private float nextFire;
 
     void Start(){
         curAmmo = Ammo;
     }
 
     void Update () {
         for (int i = 0; i < gunTip.Length; i++) {
 
 
         if (Input.GetButton("Fire1") && Time.time > nextFire)
         {
             nextFire = Time.time + fireRate;
             if (curAmmo > 0) {
                 Rigidbody bulletInstance;
                     bulletInstance = Instantiate (bullet, gunTip[i].position, gunTip[i].rotation) as Rigidbody;
                     bulletInstance.AddForce (gunTip[i].forward * 500);
 
                 curAmmo -= ammoConsumed;
                 }
             }
         }
     }
 }
 
   
 
              Answer by Hellium · Jan 08, 2018 at 03:36 PM
The for loop should be inside the if (Input.GetButton("Fire1") && Time.time > nextFire) condition. Otherwise Time.time > nextFire is evaluated to false when i=1, since you have assigned the value of nextFire when i was equal to 0
      if (Input.GetButton("Fire1") && Time.time > nextFire)
      {
          nextFire = Time.time + fireRate;
          for (int i = 0; i < gunTip.Length; i++)
          {
              if (curAmmo > 0)
              {
                  Rigidbody bulletInstance;
                  bulletInstance = Instantiate (bullet, gunTip[i].position, gunTip[i].rotation) as Rigidbody;
                  bulletInstance.AddForce (gunTip[i].forward * 500);
  
                  curAmmo -= ammoConsumed;
              }
          }
      }
 
              Your answer
 
             Follow this Question
Related Questions
IndexOutOfRangeException error when getting transform of array object 0 Answers
I got Some Error about how to Transform Postiton Enemy where one point can spawn 2 enemies. 1 Answer
What's the best way to rotate an object relative to another? 0 Answers
Give instantiated object same transform as localPlayer. 1 Answer
Checking if an instance of a Script saved in 3d array is null 1 Answer