How to launch missiles sequentially ?

Hello, I’m making a missile asset that can lock on to targets, it works fine but I’m having a problem figuring out how to fire an array of missiles at different times e.g. all 6 at once but with a second gap between each missile or all 6 individually at the press of a button.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Missle : MonoBehaviour
{
     public GameObject explosionEffect;
     public Rigidbody Rb;
     public Transform target;
     [SerializeField]
     bool Chase;
     [SerializeField]
     bool Eject;
     public FixedJoint FJ;
     public float turn;
     public float rocketVelocity;
     float accel = 3;
   
     // Start is called before the first frame update
     void Start()
     {
         Rb.GetComponent<Rigidbody>();
     }
     // Update is called once per frame
     void FixedUpdate()
     {
         //initiate rocket
         if (Input.GetKey("u") && Chase == false && Eject == false)
         {
             Ejected();
         }
         if (Input.GetKey("i"))
         {
             Explode();
         }
         // fire away from floor
         if (Eject == true)
         {
             Rb.velocity = transform.forward * 10f;
         }
         //lock on to target and chase
         if (Chase == true)
         {
             Rb.velocity = transform.forward * rocketVelocity;
             var rocketTargetRotation = Quaternion.LookRotation(target.position - transform.position);
             Rb.MoveRotation(Quaternion.RotateTowards(transform.rotation, rocketTargetRotation, turn));
         }
     }
     // Start the Eject sequence and call countdown for the rockets lock on
     void Ejected()
     {
         Eject = true;
         Fired();
     }
     //once the timer is over call the chase sequence to get the target and chase it.
     void Fired() { StartCoroutine(CountTillChase()); }
     IEnumerator CountTillChase()
     {
         yield return new WaitForSeconds(1);
         Eject = false;
         yield return new WaitForSeconds(1);
         Chase = true;
         // Code here will be executed after 3 secs
         //Do stuff here
     }
     //Oncollision in the Chase stage call the explosion method
     private void OnTriggerEnter(Collider other)
     {
         if (Chase)
             Explode();
     }
     //Spawns particle effect and destroys the object whilst applying force to nearby rigid bodies
     void Explode()
     {
         Instantiate(explosionEffect, transform.position, transform.rotation);
         Collider[] colliders = Physics.OverlapSphere(transform.position, 10);
         foreach (Collider nearByObjects in colliders)
         {
             Rigidbody ERB = nearByObjects.GetComponent<Rigidbody>();
             if (ERB != null)
             {
                 Rb.AddExplosionForce(10000, transform.position, 10000);
             }
       
         }
       
         Destroy(gameObject);
     }
}

Basically the effect I’m trying to create is multiple rockets on a turret rack that fires them sequentially then re-spawns them all/the used rockets.

You need timer delay and counter.
Time will trigger counter increment.
Number of increments, will trigger consecutive missile.

Sorry, I don’t yet understand how to reference specific objects, so say I add the counter in with an increment that’s pretty straight forward. How would I trigger those rockets specifically? How would I assign a specific number to each rocket?

Store rockets GameObjects references in array or list.
Or if you have store collection, for example list, with pool of unused object, use them.
Is just like having many bullets, and instead destroying their game objects, you put them into pool, to reuse them when needed.
Then simply, when firing, using counter down, from the last available rocket (in your case).
When rocket explodes, or is time to die, just put it back into pool of spare GameObjects of rockets.

just use a for

public int ammo = 6;

public countDown;
private coolDown;

void start {
coolDown = countDown;
}

void Update{
for (int i = 0; i < ammo; i++)
{
         countDown =  countDown - Time.DeltaTime;
        if (countDown <= 0)
{
     //spawn missile
     countDown = coolDown;
}
}
}

I understand what you want, i guess, hopes it will helps

2 Likes

Thank you!

1 Like