Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 AngelFeather0 · Jul 16, 2021 at 12:04 AM · coroutinevelocitywaitforsecondsattackdash

A way to run code during WaitForSeconds?

Hello everyone!

I currently have a character in my game that can attack by pressing a certain button. When that button is pressed, i also want him to dash in that direction for a bit, before i let the player control him again.

This is how i fix it right now:

  if (Input.GetKeyDown(KeyCode.Z))
             {
                 Attack();              
  
                 StartCoroutine(Dash(moveX, moveY, dashSpeed, dashTime));
             }

 void Attack()
     {
         Collider2D[] hitenemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, 
         enemyLayers);
 
         foreach(Collider2D enemy in hitenemies)
         {
             Debug.Log("I hit " + enemy.name);
             enemy.GetComponent<EnemyScript>().TakeDamage(attackDamage);
         }
     }

 
 IEnumerator Dash(float moveX, float moveY, float dashSpeed, float seconds)
     {
         //canMove allows the player to move
         GetComponent<PlayerMovement>().canMove = false;
 
         //This makes the player dashes at the right direction
         playerrb.velocity += new Vector2(moveX * dashSpeed, moveY * dashSpeed);
 
         yield return new WaitForSeconds(seconds);
 
         GetComponent<PlayerMovement>().canMove = true;
     }

(I left out a bit of code that i did not think was necessary, but feel free to correct me in the comments)

However, the attack function is only called at the very beginning. Meaning that, when the player dashes, it does not registers if an enemy is in the "attack circle" and thus, does not attack it.

So, my problem is this. Is there a way to somehow run this Attack function during a waitforseconds? Or can i solve this problem in a better way?

Feel free to ask questions about the code or the game, if you need it

Thanks in advance!

Comment
Add comment · Show 1
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 rage_co · Jul 16, 2021 at 02:20 AM 0
Share

make a bool that you set true just before the wait for seconds part and set it false immediately afterwards......then simply put a condition in the update method that while the bool is true.....you can dash

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Namey5 · Jul 16, 2021 at 03:41 AM

What you can do is replace WaitForSeconds with a manual poll loop and call your attack function while that loop is running, i.e.

 IEnumerator Dash(float moveX, float moveY, float dashSpeed, float seconds)
 {
     //canMove allows the player to move
     GetComponent<PlayerMovement>().canMove = false;
  
     //This makes the player dashes at the right direction
     playerrb.velocity += new Vector2(moveX * dashSpeed, moveY * dashSpeed);
  
     // Accumulate the amount of time that has passed per frame until we reach the desired time
     for (float t = 0f; t < seconds; t += Time.deltaTime)
     {
         // Wait for the next frame
         yield return null;
         // Run the attack function now on the new frame
         Attack ();
     }
  
     GetComponent<PlayerMovement>().canMove = true;
 }
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 AngelFeather0 · Jul 16, 2021 at 11:23 AM 0
Share

Sick! It worked! Thanks a bunch!

avatar image rage_co · Jul 16, 2021 at 12:14 PM 0
Share

Hmm right, this is actually a more efficient method than $$anonymous$$e

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

134 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 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

How to do a "dash"-movement, set distance on key-input? 0 Answers

Applying Damage 5 Answers

Need Help with a Null Reference 1 Answer

How can i get this Delay script to work? 1 Answer

Waitforseconds not working? 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