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
is attached to the Main Camera in Text example scene ofChapter4, The Event System and Programming for UI, and 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 pressed, and GetButtonUp()
returns true only once...