x


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();
more ▼

asked Nov 12, 2012 at 12:29 PM

ina gravatar image

ina
4.4k 1623 1258 1304

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.

Nov 12, 2012 at 12:35 PM fafase

When you don't want to create combo, it is always better to discriminate the inputs. I'll go for the second option (if-else).

Nov 12, 2012 at 02:53 PM Kryptos
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

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.

more ▼

answered Nov 12, 2012 at 12:32 PM

KiraSensei gravatar image

KiraSensei
3.4k 91 93 104

@ina said either up or down so both won't be used at once. so I would go for second.

Nov 12, 2012 at 12:35 PM fafase

@fafase : yes it is my guess too :)

Nov 12, 2012 at 12:39 PM KiraSensei
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1568
x54
x4

asked: Nov 12, 2012 at 12:29 PM

Seen: 951 times

Last Updated: Nov 12, 2012 at 02:54 PM