- Home /
The question is answered, right answer was accepted
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
Have you tried with GetMouseButtonDown(0) and GetMouseButton(0)?
Yes, I have. The same thing as with other ones
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
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.
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.