- Home /
Ensuring that a button is released before checking that it's pressed?
Evening, guys.
I'm attempting to implement a fancy attack combo thing where, when you press the attack button during an attack, it'll execute the next attack in the chain once the first is finished. In order to do so, I'm wanting to set the boolean comboQueued to true if it detects that the attack button is pressed. This is executed in the following code:
function normalSwing () {
if(Input.GetButtonDown("Attack"))
comboQueued = true;
}
Unfortunately, that very code is activated by a press from the same button that I want to activate the function, so it just sets comboQueued to true all the time.
Any way to make sure that this only executes when the attack key's been released and pressed a second time?
Answer by nesis · Jan 28, 2014 at 10:10 PM
GetButtonDown() is the way to go. What you're doing is purporting this check on the same frame as when the attack starts... so of course it'll return true! The solution for what you're wanting is to track what stage of the combo the player is up to, and check if GetButtonDown() returns true during that stage.
So, make an int that counts the stage the combo is up to. When a combo ends, reset that int to zero. When it continues, increment it. Then in your update method, have a switch statement / if statement to handle different values of the int (ie, different stages).
Answer by xt-xylophone · Jan 28, 2014 at 10:14 PM
Combos are normally done by saving the most recent say 5 moves in a queue data structure and then you can just check it to see if the player has pressed a combo.
In your case though are you hoping to have the player press a button DURING their first attack? In that case you will need some sort of timeout for the first attack.
Going like this:
Press attack, initiate attack and set the combo bool to true
Use an Invoke function(check the docs) which turns that bool to false after X seconds, maybe 0.5, whatever length you want
When an attack is pressed, check that bool, if its true it was pressed right after the last one and do your combo