Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Mar 31, 2021 at 06:18 AM by CORNER-ED for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by CORNER-ED · Mar 30, 2021 at 04:14 PM · if-statementsgetkeydown

GetKeyDown doesn't work in if statement, but GetKey does

Hello, I have been trying to create a combat system and I ran into a problem with input. If I use GetKey it works,but if I use GetKeyDown it stops working. I checked if my if statements were working and the one with input stops working the moment I change GetKey to GetKeyDown. Any idea why this might be happening? Code: (It is my first time asking a question here and it kept marking my code red and saying I need to correct the form but idk what's wrons so the code is just in text sorry)

 void Update()
 {   
      if(timeBtwAttack <= 0)
     {   
         if(Input.GetKey(KeyCode.Mouse0))
         {       
                 Debug.Log("Mouse0Down") ;
                 playerAnim.SetTrigger("Attack");
                 Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position,attackRange,whatIsEnemy);
                 for (int i = 0; i< enemiesToDamage.Length; i++){
                 enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
                  }
                 Collider2D[] enemyProgectiles = Physics2D.OverlapCircleAll(attackPos.position,attackRange,whatIsProjectile);
                 for (int i = 0; i< enemyProgectiles.Length; i++){
                 enemyProgectiles[i].GetComponent<PatterProjectile>().DestroyThis();}
             }
             timeBtwAttack = startTimeBtwAttack;
         } 
         else 
         {  // Debug.Log("Mouse0Up") ;
              timeBtwAttack-=Time.deltaTime;
            
             playerAnim.SetBool("AttackExit", true);
         }
     
     
 
 }

Edit: I have tried using Get(Mouse)ButtonDown and Get(Mouse)Button and it is exactly the same as GetKey stuff

Comment
Add comment · Show 4
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 KoenigX3 · Mar 30, 2021 at 05:01 PM 0
Share

Have you tried with GetMouseButtonDown(0) and GetMouseButton(0)?

avatar image CORNER-ED KoenigX3 · Mar 30, 2021 at 06:13 PM 0
Share

Yes, I have. The same thing as with other ones

avatar image MikeNewall · Mar 30, 2021 at 08:05 PM 0
Share

How are you initialising "timeBtwAttack" I see you assign startTimeBtwAttack when the button is pressed? What is that value

you are only checking the input when timeBtwAttack is

Try logging the value of timeBtwAttack and see when it is

avatar image Bunny83 · Mar 31, 2021 at 08:38 AM 1
Share

I highly recommend you take more care about proper indention. Using a proper IDE should have indented your code differently because the way you have aligned your code it looks like your else statement belongs to your GetKey if statement which is doesn't. We use indention to make code more readable. The compiler doesn't care about indention at all. So if you do indent (which you should), do it properly ^^-

Otherwise you could write your code as:

 void Update() {if(timeBtwAttack <= 0){if(Input.GetKey(KeyCode.Mouse0)){Debug.Log("Mouse0Down");playerAnim.SetTrigger("Attack");Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position,attackRange,whatIsEnemy);for (int i = 0; i< enemiesToDamage.Length; i++){enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);}Collider2D[] enemyProgectiles = Physics2D.OverlapCircleAll(attackPos.position,attackRange,whatIsProjectile);for (int i = 0; i< enemyProgectiles.Length; i++){enemyProgectiles[i].GetComponent<PatterProjectile>().DestroyThis();}}timeBtwAttack = startTimeBtwAttack;}else{timeBtwAttack-=Time.deltaTime;playerAnim.SetBool("AttackExit", true);}}

which is your original code, just with the newlines and spaces stripped out. It does still the same thing, but is impossible to read or modify ^^.


Also, as KoenigX3 did, avoid convoluted and nested if-else constructs if they are not necessary. Keeping the nesting to a $$anonymous$$imum makes it a lot easier to read and reason about the code. If nesting can not be avoided, you may want to source out the inner code into a seperate function.


Finally keep in $$anonymous$$d that there are quite different common game mechanics out there how to handle attack buttons. Some require you to release and press the attack button for each shot, if you press too early, nothing happens. Others have a single "queued" action. So if you release and press the button (and you hold it down) while still in the cooldown time, it will fire the action again once the cooldown is over, but only once and not repeatedly. So you are still required to release and press again for every shot / action, but it's more fault tolerant. One example is the game Quake3. When you jump, release the jump key and press (and hold) it again while you are still in air, you essentially queued a new jump once you land, but only one jump. So constantly holding down the button does nothing. Of course releasing the button would cancel any "queued" jump.

It depends on what mechanic you want.

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by KoenigX3 · Mar 30, 2021 at 08:21 PM

I think the problem is that you are setting timeBtwAttack back to startTimeBtwAttack when it reaches 0 or negative. Using GetMouseButtonDown or GetKeyDown would work only if you pressed the button in that particular frame - which is very unlikely.

Try this:

 void Update()
 {
     if (timeBtwAttack > 0)
     {
         timeBtwAttack -= Time.deltaTime;
     }
 
     if (Input.GetMouseButtonDown(0))
     {
         if (timeBtwAttack <= 0)
         {
             Debug.Log("Mouse0Down");
             playerAnim.SetTrigger("Attack");
             Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemy);
             for (int i = 0; i < enemiesToDamage.Length; i++)
             {
                 enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
             }
             Collider2D[] enemyProgectiles = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsProjectile);
             for (int i = 0; i < enemyProgectiles.Length; i++)
             {
                 enemyProgectiles[i].GetComponent<PatterProjectile>().DestroyThis();
             }
 
             timeBtwAttack = startTimeBtwAttack;
         }
     }
     else if (Input.GetMouseButtonUp(0))
     {
         playerAnim.SetBool("AttackExit", true);
     }
 }


Note that i'm checking timeBtwAttack twice, but it shouldn't be a problem.

Comment
Add comment · Show 1 · 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 CORNER-ED · Mar 31, 2021 at 03:56 AM 0
Share

Thank you! It works now!

Follow this Question

Answers Answers and Comments

117 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

Related Questions

How to get holding key to prioritize 1 Answer

Update GetKey If Statements both playing instantly 1 Answer

How to get an Objects position per axis? 1 Answer

Help with if statement 1 Answer

TimeSpan does the difference automatically count the minutes 1 Answer


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