- Home /
Fire several Turrets one after another with delay.
Hi there,
i have a spaceship with several turrets that i manage with an array. I had it like go through the array and fire (bool colled fired set to true after shot), the wait for some time (coroutine that sets the bool to false again so turrets can fire again). That makes my turrets fire simultaniously. now i want a second fire mode where the turrets fire one after another with that delay. I tried different approaches but i wont get it right somehow.
void ControlTurrets()
{
if (Input.GetKeyDown(KeyCode.F))
{
if (openFire)
{
LockedUI.content = "Locked on target";
openFire = false;
}
else
{
openFire = true;
}
OpenFireUI.openFire = openFire;
}
for (int i = 0; i < turrets.Length; i++)
{
if (turrets[i] != null)
{
Turret _turret = turrets[i].GetComponent<Turret>();
if (Cursor.visible)
{
targetController.SelectTarget(_turret);
}
GameObject _target = _turret.Target;
if (_turret.Active)
{
if (BurstUI.burst.isOn)
{
_turret.FireRate = fireRateBurst;
}
else
{
_turret.FireRate = fireRateNormal;
}
_turret.MaxSpread = _turret.FireRate / 2;
if (_target != null)
{
Turn(_turret, _target);
Pitch(_turret, _target);
if (!health.Destroyed)
{
if (openFire)
{
if (!_turret.Fired)
{
if (Locked(_turret.TurretHead))
{
if (InRange(_turret))
{
if (!HitSelf(_turret))
{
if (energy.DrawEnergy(_turret.EnergyConsumed))
{
Shoot(_turret);
_turret.Fired = true;
StartCoroutine(Wait(_turret));
}
}
}
}
}
}
}
}
}
}
else
{
Debug.LogError("No turret referenced.");
}
}
}
Thats the way I have it right now...firing simultanious. The Function is called inside the Update function. Does anyone have a simple example on how to do it?
Answer by hyperi0n · Jun 30, 2016 at 11:59 AM
sorry I didn't have time to fully go through your code, so my proposition below is not adapted to your variable names etc., hope it helps you anyway
the rough idea is to fire one turret, then wait for a preset delay, fire the next etc. once all turrets have fired, the burst is ended.
//in update function:
/*
requires the following variables:
firingBurst (boolean)
nextBurstShot (float)
burstShotDelay (float)
currentTurret (int)
*/
if (firingBurst){ //the command to open fire was given somewhere else, setting this boolean to true and setting "current turret = 0".
if(Time.time >= nextBurstShot){ // wait for the a preset delay between the individual shots in a burst
turrets[currentTurret].Fire(); // tell the currently "selected" turret to shoot -- this is where you would put everything that happens when (or is checked before) a turret is fired
nextBurstShot += burstShotDelay; // wait with the next shot until the delay between shots has passed
currentTurret ++; //cycle through to the next turret
}
if (currentTurret > turrets.length){ //if all turrets have fired...
firingBurst = false; //tell the burst to end
}
}
You could also use a coroutine instead of the timer based approach used here. Hope you can somehow adapt this idea to work for you! :)
Answer by DroidifyDevs · Jun 30, 2016 at 01:14 PM
You could just use an IEnumerator coroutine to make the delay between firing the turrets. That's the easiest way.
http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612083929im_/https://answers.unity.com/themes/thub/images/avi.jpg)