How to fire bullets from multiple guns at the same time?
I have a fighter jet which has 6 guns in it. I want to fire bullets with all six guns at the same time when i hit fire. The script used to fire bullet with 1 gun is attached below:
public class bullet_fire : MonoBehaviour {
public GameObject Bullet_Emitter;
public GameObject Bullet;
public float Bullet_Forward_Force;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown("Fire1"))
{
GameObject temp_bullet_hsndler;
temp_bullet_hsndler = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
temp_bullet_hsndler.transform.Rotate(Vector3.left * 90);
Rigidbody temp_rigidbody;
temp_rigidbody = temp_bullet_hsndler.GetComponent<Rigidbody>();
temp_rigidbody.AddForce(transform.forward * Bullet_Forward_Force);
Destroy(temp_bullet_hsndler, 3.0f);
}
}
}
There is no need to have 6 of these scripts attached to each gun. Ins$$anonymous$$d, just attach this to your jet, and have a GameObject array, filled with the gun objects. Then when firing, just use a for loop to get each gun's position/rotation for spawning bullets.
Hope this helps! :)
Answer by thor348k · Jun 21, 2016 at 09:21 AM
I'd just have a Jet prefab, and a Gun prefab. Attach the shooting script to the Jet prefab, and use an array, which you can store the gun into (6 times). You'll need an array of positions to match each gun too, but that way, you can have many different types of jets, with many different types of guns!
If you're only wanting to use one type of Jet, and one type of Gun, just have 6 Gun prefabs as children on the Jet prefab, and assign them to the array in the shooting script attached to the Jet. That way you can avoid assigning positions.
Answer by mughalprince36 · Jun 21, 2016 at 07:41 AM
and if i use a prefab for this?
A Jet prefab? Just make the Gun array public, and assign them via the Inspector.
I am currently using gun prefab. in which each gun has the same script assigned. Is it better or using array is better?
Your answer
Follow this Question
Related Questions
2D projectiles not rotating 0 Answers
Basic FPS help! 2 Answers
Projectile trajectory based on angle 0 Answers
how I can control projectile shooting limit??? 0 Answers
How to instantiate projectile at the right height? 2 Answers