How can I use multiple GetButtonDown keys simultaneously?

I have a strange dilemma. I am working on a type of “jump” functionality where I want the jump distance/force to be the same every time the player presses the input, I DON’T want the jump distance/force to increase as the player holds the button. GetButtonDown seems the ideal solution for this, but the problem is that I am using 2 keys (need to be pressed simultaneously) to trigger the jump, and naturally having to press 2 keys within the same frame is very bad for player usability reasons.

So, I need the input window to be long enough to make it “usable” to press 2 keys at once, but at the same time trigger the jump force only once. I’ve tried using a combination of GetButton & GetButtonDown to trigger the jump but the results have been inconsistent. Any suggestions for this?

Try doing something like this:

if((GetButtonDown("KeyOne") && GetButton("KeyTwo"))
 || (GetButtonDown("KeyTwo") && GetButton("KeyOne"))
{
    // Do jump!
}

This allows the action to be performed when you are already holding one of the buttons, and you press down the second one.

For additional tweaking, add a time factor as well:

//Make sure you have a float 'buttonPressedTime'
// Also, this has to happen *after* the other check, or it will invalidate the time!
if(GetButtonDown("KeyOne") || GetButtonDown("KeyTwo")) {
    buttonPressedTime = Time.time;
}

Then, make sure that Time.time is within some short window of ‘buttonPressedTime’ when the player does the two-key action to ensure that the buttons were pressed close together.

Just want to post here the solution I actually ended up using for anyone else with a similar problem. After further testing I found the above solution still inconsistent at times with the occasional misfire of the button inputs. The solution is a very simple one:

if (GetButton("KeyOne") && GetButton("KeyTwo") {
    doJump = true;
}

if (doJump) {
    //Do Jump, addforce etc...
    //Then turn bool off
    doJump = false;
}

I simply used GetButton for both keys which means there is no chance of misfiring like there is with GetButtonDown. Once both keys are being held a boolean is set to true. This is the key part. The boolean is listening for the first instance when both keys are pressed, and because it is a boolean, no matter how long/short you hold both keys for it will always apply the same amount of force to the jump because it is only applying it once. Hence it works like a GetButtonDown, but for multiple keys at once :wink: