Unity: Input.GetKey vs Input.GetButton

When I started with unity (1 year ago) I always asked to myself which of this two is better if Input.GetKey or Input.GetButton, so I created this blog to explain in the most easy way how works each one and give you an example of each one.

1. Similarities between Input.GetKey and Input.GetButton

In essence both are very similar, if you go to the Unity manual, you will find the following similarities between these two options:

  • Both are static bool
  • Have to be called in Update function
  • Only works well in invoking actions like events
  • Have three “variants”

In the next section you will find the variants for each one and how they works. Also a small example script of how they are used.

2.Input.GetKey

This require to specify a key from your keyboard it can be space, up, down, … whatever you like.
For Input.GetKey the variants are Input.GetKey, Input.GetKeyDown  and  Input.GetKeyUp.

Input.GetKey: in this case when your press the key, it return true and continue doing this as long you press the key
Input.GetKeyDown: it only come true once and this is in the first frame after you press the key
Input.GetKeyUp: for this case it only come true once and this is in the first frame after you release the key

3. Input.GetButton

This require to specify a button from your project settings>input manager the name of your button is set by you.
For Input.GetButton the variants are Input.GetButton, Input.GetButtonDown  and  Input.GetButtonUp.

Input.GetButton: in this case when your press the button the it return true and continue doing this as long you press the key
Input.GetButtonDown: it only come true once and this is in the first frame after you press the button
Input.GetButtonUp: for this case it only come true once and this is in the first frame after you release the button

4. Differences between Input.GetKey and Input.GetButton

The real problem with Input.GetKey is that you have to be careful when you write the name of the key in the script.

And the advantage of Input.GetButton is the capacity of customize the buttons.

If you find this blog useful let us a comment, and follow us in our Instagram @tortugamedev and Twitter @Totuas2.

And don’t forget to check out the rest of the blogs!!

Leave a Comment

Your email address will not be published.