- Home /
if-else for Input.GetKey
For a binary case, where something can either go up or down, is it better to use
if(Input.GetKey(KeyCode.UP)) GoUp(); if(Input.GetKey(KeyCode.DOWN)) GoDown();
or with the else
if(Input.GetKey(KeyCode.UP)) GoUp(); else if(Input.GetKey(KeyCode.DOWN)) GoDown();
With the first one, even though you would press up, the second if will be checked. In the second case, it only happens is the first returned false. So I would go for the second but don't expect big changes though.
When you don't want to create combo, it is always better to discri$$anonymous$$ate the inputs. I'll go for the second option (if-else).
Answer by KiraSensei · Nov 12, 2012 at 12:32 PM
In my opinion, since a keyboard can handle 3 keys simultaneously, this will not have the same behavior. The first example will allow your object to go up and down, in the second one this will be go up OR go down, and if both are used, it will only go up.
@ina said either up or down so both won't be used at once. so I would go for second.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083735im_/https://answers.unity.com/themes/thub/images/avi.jpg)