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 gaia2222 · Sep 24, 2018 at 08:54 AM · functiondelay

Can I add a delay function into an independent function but not in the update?


What should I do if I want to add a delay function after case AIState.attacking?


Can I add a delay function into an independent function but not in the update?

 E.g. I want to shoot the bullet object (let me call it X) 3 times, which would be like this:
 X_X_X (symbol "_" representing a delay, let it be 0.5 second)
 Let LoopA = "X_X_X" 
 I want to have this loop for 3 times, so it should be like this:
 X_X_X - X_X_X - X_X_X, where the symbol " - " representing another delay I need to have.
 I've already made this to be a function(let it be function Y) that will be called by switch cases.
 Therefore, I need to add delay function into function Y. What should I do?

Here is the code:

 function MakeDecision(){

         switch(state){

             case AIState.wandering:
                 Wander();
                 break;

             case AIState.chasing:
                 Chase();
                 break;

             case AIState.attacking:
                // add delay here
                 Attack();
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
0
Best Answer

Answer by CoryButler · Sep 24, 2018 at 02:26 PM

Use WaitForSeconds() to delay the bursts of bullets and the firing of each bullet in a burst.

 private void MakeDecision()
 {
     switch(state)
     {
         case AIState.wandering:
             Wander();
             break;
         case AIState.chasing:
             Chase();
             break;
         case AIState.attacking:
             AttackAfterDelay(3, 0.5);
             break;
         default:
             break;
     }
 }
 
 // Delay the first attack (in this case, firing 3 bullets).
 // Use same delay between attacks.
 private IEnumerator AttackAfterDelay(int numAttacks, int delay)
 {
     while (numAttacks > 0)
     {
         yield return new WaitForSeconds(delay);
         FireBullets(3, 0.5)
         numAttacks--;
     }
 }
 
 // Fire 1 bullet immediately.  Then delay the firing of all following bullets.
 private IEnumerator FireBullets(int numBullets, int delayBetweenBullets)
 {
     while (numBullets > 0)
     {
         FireBullet();
         numBullets--;
         yield return new WaitForSeconds(delayBetweenBullets);
     }
 }
 
 // Fire a single bullet.
 private void FireBullet()
 {
     // TODO: Fire a single bullet.
 }
Comment
Add comment · Show 2 · 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 gaia2222 · Sep 25, 2018 at 05:56 AM 0
Share

where am i put the $$anonymous$$akeDecision()? I put it in the upDate function but it is not work...

avatar image CoryButler gaia2222 · Sep 25, 2018 at 12:31 PM 0
Share

You may need to add another AIState. Try adding AIState.startAttack. In $$anonymous$$akeDecision(), change AIState.attacking to AIState.startAttack. Then, in AttackAfterDelay(), start by setting state to AIState.attacking, and end the method by setting state to whatever is appropriate (AIState.wandering or AIState.chasing);

  private void $$anonymous$$akeDecision()
  {
      switch(state)
      {
          case AIState.wandering:
              Wander();
              break;
          case AIState.chasing:
              Chase();
              break;
          case AIState.startAttack: // Start the attack.
              AttackAfterDelay(3, 0.5);
              break;
          case AIState.attacking:
              // Do not add anything here. AttackAfterDelay() handles this AIState.
              break;
          default:
              break;
      }
  }
  
  // Delay the first attack (in this case, firing 3 bullets).
  // Use same delay between attacks.
  private IEnumerator AttackAfterDelay(int numAttacks, int delay)
  {
      state = AIState.attacking; // The attack has started.  Don't allow a new attack until this one is done.
      while (numAttacks > 0)
      {
          yield return new WaitForSeconds(delay);
          FireBullets(3, 0.5)
          numAttacks--;
      }
      state = AIState.chasing; // Or whatever AIState is appropriate.  Just let the program know the attack is over.
  }
avatar image
0

Answer by gaia2222 · Sep 24, 2018 at 08:35 PM

This is exactly what I want. Thank 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

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

92 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 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 avatar image avatar image

Related Questions

Better way to delay a function for a few seconds? Javascript 1 Answer

Function in Update delaying involuntarily 2 Answers

How do I tell another script to skip its routine for one frame? 1 Answer

Does failed RPC affects anything? 0 Answers

can i delay the script in the 'function update'? 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