- Home /
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?
Answer by syclamoth · Oct 11, 2013 at 07:37 AM
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.
Hmmm, this seems it could work for me. So theoretically the jump force will still be applied in one frame only since "GetButtonDown && GetButton" will only be true for one frame is that right?
Yes, because triggering the action requires at least one 'GetButtonDown' to return true, and GetButtonDown only activates once per key-press.
Great! I'll test this out when I get the chance. Thanks a lot
I had to add a timer, since in 1 frame a character doesn't jump anywhere. So to get it working the two keys need to trigger the timer first during which the force can be applied, if that makes sense. Thanks for your help
Answer by Hamesh81 · Oct 11, 2013 at 04:31 PM
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 ;)
Your answer
Follow this Question
Related Questions
Detecting if 2 Buttons are pressed at the same time 3 Answers
Having GetButton act like GetKeyDown 1 Answer
Simultaneous button presses 2 Answers
C# Problem returning from crouch to stand 1 Answer
Modifying properties of a buttonName 1 Answer