VladShpilMan
VladShpilMan

Reputation: 33

How can I check if a key is being held down?

How can I check if any key is pressed? The Input.GetKeyDown function is inappropriate. For example, after holding the key, I want my character to raise the shield, and when I release the key, the character lowers the shield. Maybe I'm using Input.GetKeyDown incorrectly. In the Update () function, I wrote the conditions if (Input.GetKeyDown (KeyCode.LeftShift)) { Protection (); Debug.Log ("Key pressed LeftShift"); } And when I click on LeftShift in the console, it really displays the message "Key pressed LeftShift", but when I hold down the LeftShift key, the message "Key pressed LeftShift" is displayed only 1 time.

Upvotes: 1

Views: 13537

Answers (1)

Quickz
Quickz

Reputation: 1846

Use Input.GetKey() instead of Input.GetKeyDown().

What's the difference?

Input.GetKeyDown() - gets triggered once when you press a key down
Input.GetKey() - gets triggered every frame while you keep holding it down

Upvotes: 3

Related Questions