Input functions for buttons and key presses
There are quite a few ways to access key and button presses via code. How you do this depends on whether you have the key specified as an axis in the Input Manager and whether you want the key to register once or continuously. I’ll discuss a few in this text, but you can find a full list of the functions at https://docs.unity3d.com/ScriptReference/Input.html.
A script named KeyPresses.cs
is attached to the Main Camera
in the Chapter8
example scene we were reviewing earlier in this chapter. The KeyPresses.cs
script contains all the code demonstrated in this section if you’d like to play around with key presses.
GetButton
If you have a button defined as an axis in the Input Manager, you can use GetButton()
, GetButtonDown()
, and GetButtonUp()
to determine when a button has been pressed.
GetButton()
returns true
while the button is being held, GetButtonDown()
returns true
only once, on the frame that the button is initially...