VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/input-handling-keyboard-mouse-in-unity/

⇱ Input Handling (Keyboard, Mouse) In Unity - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Input Handling (Keyboard, Mouse) In Unity

Last Updated : 4 May, 2026

Games are interactive because they respond to player actions. Whether pressing a key to jump, moving the mouse to aim, or touching the screen to shoot, handling input is what makes a game feel alive. Unity provides the Input class to detect player input across all devices.

Input Class

The Input class is Unity's gateway to all player input. It provides methods and properties to check the state of keyboards, mice, touch screens, controllers, and joysticks.

  • Available anywhere in your scripts (just type Input).
  • Works across all platforms automatically.
  • Provides both discrete (press/release) and continuous (axis) input.
πŸ‘ Input-Handling-In-Unity
Inputs Handling In Unity

Handling Keyboard Input

Unity provides three main methods to detect keyboard input, each serving a different purpose.

GetKeyDown() – One-time press detection

  • Returns true for exactly one frame when the key is pressed
  • Perfect for jumping, shooting, interacting, or any single-action event

GetKey() – Continuous hold detection

  • Returns true every frame as long as the key remains pressed
  • Perfect for running, walking, or any continuous action

GetKeyUp() – Release detection

  • Returns true for exactly one frame when the key is released
  • Perfect for releasing a charged attack or stopping an action

Common KeyCode Values

Movement keys:

  • KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D
  • KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.LeftArrow, KeyCode.RightArrow

Action keys:

  • KeyCode.Space: Jump
  • KeyCode.LeftShift: Run
  • KeyCode.E: Interact
  • KeyCode.R: Reload
  • KeyCode.Tab: Inventory
  • KeyCode.Escape: Pause menu

Mouse keys:

  • KeyCode.Mouse0: Left click
  • KeyCode.Mouse1: Right click
  • KeyCode.Mouse2: Middle click

Using Input Axes for Smooth Movement

For smooth movement like walking or looking around, axes are better than individual key checks.

Predefined axes in Unity:

  • Horizontal: A/D and Left/Right arrows (left = -1, right = +1)
  • Vertical: W/S and Up/Down arrows (down = -1, up = +1)
  • Mouse X: Mouse horizontal movement
  • Mouse Y: Mouse vertical movement
  • Mouse ScrollWheel: Scroll wheel input

GetAxis() vs GetAxisRaw():

  • GetAxis(): Smooth, gradual values (good for movement)
  • GetAxisRaw(): Instant -1, 0, or 1 (good for snapping)

Checking Any Key or Key Combination

Sometimes you need to check if any key was pressed or detect combinations.

Handling Mouse Cursor

Controlling the cursor is important for different game types.

Cursor lock states:

  • CursorLockMode.None: Cursor moves freely, can leave window
  • CursorLockMode.Locked: Cursor locked to center, hidden
  • CursorLockMode.Confined: Cursor confined to game window

Common Errors and Solutions

  • Input won’t work in Update() if the script is disabled or the game isn’t running.
  • Using GetKey for jumping causes continuous jumps; use GetKeyDown instead.
  • Missing Time.deltaTime with axis movement makes it frame rate dependent.
  • Cursor may stay locked after the game ends; unlock it on disable or game over.
  • Axis values not resetting may require adjusting Gravity in the Input Manager.
Comment
Article Tags:
Article Tags:

Explore