Key inputs play a significant role in games to trigger certain events, control the characters, and are even helpful in debugging purposes. The input system in Unity provides various functions to detect specific key actions.
Input.GetKey
methodYou can use Input.GetKey
to check whether a specific keyboard key is held down by the player and then perform the actions accordingly. Input.GetKey
is used within the Update
method to check the input once per frame.
Note:
Input.GetKey
detects the input in each frame.If you want to perform specific actions when a specific key is pressed down, you need to call this method inside a loop.
You can also choose to update your game logic once a key is held down.
Given below is a guide on how to use various Input.GetKey
methods in your C# scripts.
Detect key press by Input.GetKeyDown
: Such method detects the exact frame when a key is initially pressed down.
Use case: Start a jump action or fire a weapon where each action occurs only once when the key is pressed.
if (Input.GetKeyDown(KeyCode.Space)){// the code runs only once when the key is pressed initially.}
Detect key release by Input.GetKeyUp
: This method detects when a specific key is released after being pressed.
Use case: Stop the character movement or end the charged attach when the specified key is released.
if (Input.GetKeyUp(KeyCode.Space)){// the code runs only when the player releases the space key.}
Detect key release by Input.GetKey
: This method determines if a specific key is being held down during multiple frames. It is mainly used for the continuous movements.
Use case: Accelerate a vehicle or charge an attack as long as the specified key is held down.
if (Input.GetKey(KeyCode.Space)){// the code runs every frame as long as the specified key is held down.}
Note: You can replace
Space
inKeyCode.Space
with any key of your choice.
Given below is a simple C# script to demonstrate the use of Input.GetKey
method.
using System.Collections;using System.Collections.Generic;using UnityEngine;public class KeyInputExample : MonoBehaviour{public KeyCode keyToDetect = KeyCode.W;public void Update(){// Check if the specified key is pressed downif (Input.GetKeyDown(keyToDetect)){Debug.Log("Key Down: " + keyToDetect);}// Check if the specified key is releasedif (Input.GetKeyUp(keyToDetect)){Debug.Log("Key Up: " + keyToDetect);}// Check if the specified key is being held downif (Input.GetKey(keyToDetect)){Debug.Log("Key Held: " + keyToDetect);}}}
Lines 1–3: These lines import all the required namespaces.
Line 5: A class is declared named KeyInputExample
. It inherits methods from the class MonoBehaviour
, a base class in Unity for scripts attached to the specific GameObjects.
Line 7: It declares a public
variable keyToDetect
of type KeyCode
. It allows you to set the key you want to detect. It is initialized with KeyCode.W
.
Line 9: The Update
method is declared here which is called once per frame.
Lines 12–15: These lines first check if the specified key (W
) is pressed down on the current frame using the Input.GetKeyDown()
. If it is true
, the specified code gets executed and logs a message to the Unity console.
Lines 18–21: These lines first check if the specified key (W
) is re;eased on the current frame using the Input.GetKeyDown()
. If it is true
, the specified code gets executed and logs a message to the Unity console.
Lines 24–27: These lines first check if the specified key (W
) is being held down on the current frame using the Input.GetKeyDown()
. If it is true
, the specified code gets executed and logs a message to the Unity console.
The sample demonstrates the cube which is being controlled by the arrow keys.
Input.GetKey
is used to make a continuous movement along the
Input.GeyKeyDown
is used to make single movement in the
Input.GetKeyUp
is used to trigger a single movement in the
import React from 'react'; require('./style.css'); import ReactDOM from 'react-dom'; import App from './app.js'; ReactDOM.render( <App />, document.getElementById('root') );
Free Resources