Overview
To create a game that responds accurately to player input, understanding key input "states" is essential. Is it "the entire time the key is held" or "just the moment the key is pressed"? Using these distinctions correctly will dramatically improve your character's responsiveness.
Godot provides three convenient methods for this input detection:
is_action_pressed()is_action_just_pressed()is_action_released()
This article explains the differences between these methods and their optimal use cases with concrete examples.
The Three Input Detection Methods
These methods are called through the Input singleton (a convenient object accessible from anywhere). The argument is the action name set in InputMap (e.g., "ui_accept", "move_right").
| Method | Detection Timing | Primary Uses |
|---|---|---|
is_action_pressed() | While action is pressed | Character movement, rapid fire |
is_action_just_pressed() | Only the first frame when pressed | Jump, confirm, single shot |
is_action_released() | Only the first frame when released | Charged shot release, jump height adjustment |
Use these within functions called every frame like _process or _physics_process to check the input state at that moment.
Practical Example: Character Movement and Jumping
Let's see how to use these methods with a 2D platformer character as an example.
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
# Get gravity from project settings
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
# Apply gravity
if not is_on_floor():
velocity.y += gravity * delta
# Jump processing: Execute only once at the "moment" pressed
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Left/right movement: Execute continuously "while" pressed
var direction = Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
# Charged attack release: Execute at the "moment" released
if Input.is_action_released("charge_attack"):
print("Charged attack released!")
move_and_slide()
In this code:
- Left/right movement uses
Input.get_axis()which hasis_action_pressedbehavior. This allows the character to keep moving while the key is held. - Jump uses
is_action_just_pressed. This ensures only one jump even if the button is held. If you usedis_action_pressedhere, the character would try to jump every frame while on the ground. - Charged attack uses
is_action_released. This enables logic where the attack triggers the moment the button is released.
Summary
Input detection is a crucial element that determines your game's "feel." Using Godot's three provided methods correctly achieves more intuitive, responsive controls.
- For continuous actions like movement:
is_action_pressed() - For single actions like jumping or confirming:
is_action_just_pressed() - For special actions like charge release:
is_action_released()
Remember these basics and apply them to your game.