- Home /
Action executed with more than one key?
Hey guys.
I've been implementing some key command stuff, and have hit a roadblock. I'm wanting to set it up so that the character will perform a dodge action when you hold down Shift and then press one of the directional keys, but I'm not really sure how to do it.
I had tried using a While loop containing an If statement like this:
function Update () {
while (Input.GetButton("Action"));
if (Input.GetAxis("Horizontal") != 0) {
print ("Dodging!");
}
}
But that makes Unity freeze as soon as Shift (Action) is pressed, as well as interfering with the other command assigned to Shift. What else is there?
Answer by Jesse Anders · Nov 09, 2010 at 04:57 AM
I think GetButton() and similar functions will only ever return one value in a single update; that is, if it returns true at all, it'll return true every time you call it until the next update occurs. So in your example, once the shift key is pressed, the program will enter an infinite loop that basically equates to 'while (true)'.
Also, it doesn't look like your 'while' loop has a body (you have what's probably an unintentional semicolon after the 'while' statement). I'd also suggest always using brackets to delineate blocks of code (the block of code that I think you intended to associate with the 'while' statement in this case), as it'll make things more clear.
If you want to have one thing happen when the horizontal axis is non-zero and the action key is down and another thing to happen when the horizontal axis is non-zero and the action key is not down, you can just write (untested):
if (Input.GetAxis("Horizontal") != 0) {
if (Input.GetButton("Action")) {
// Do one thing
} else {
// Do the other thing
}
}
Ah, thanks for the fast response. It worked perfectly, too!