Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zwusel · Jun 29, 2016 at 08:26 PM · updatearrayswaitforsecondsturrets

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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! :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

45 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WaitForSeconds in Update() function 4 Answers

Decrease Variable By Second Whilst Button Held 3 Answers

How to create a delay in a function being called in update? 2 Answers

When was the last update to the Unity Online Script Reference? 1 Answer

Activate Animation => Wait => Hide Gameobject 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges