Input
Input Actions
Configura in Project Settings → Input Map:
func _process(delta):
# Controlla se action è premuta
if Input.is_action_pressed("move_right"):
position.x += 100 * delta
# Just pressed (singolo frame)
if Input.is_action_just_pressed("jump"):
jump()
# Just released
if Input.is_action_just_released("shoot"):
stop_shooting()
Keyboard
func _input(event):
if event is InputEventKey:
if event.pressed and event.keycode == KEY_SPACE:
print("Space pressed!")
func _process(delta):
# Input diretto
if Input.is_key_pressed(KEY_W):
position.y -= 100 * delta
Mouse
func _input(event):
if event is InputEventMouseButton:
if event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
print("Left click at: ", event.position)
if event is InputEventMouseMotion:
print("Mouse moved to: ", event.position)
func _process(delta):
# Posizione mouse
var mouse_pos = get_global_mouse_position()
# Guarda verso mouse
look_at(mouse_pos)
Gamepad
func _process(delta):
# Analog stick
var axis_x = Input.get_axis("gamepad_left", "gamepad_right")
var axis_y = Input.get_axis("gamepad_up", "gamepad_down")
velocity = Vector2(axis_x, axis_y) * SPEED
# Bottoni
if Input.is_action_just_pressed("gamepad_button_a"):
jump()
Touch
func _input(event):
if event is InputEventScreenTouch:
if event.pressed:
print("Touch at: ", event.position)
if event is InputEventScreenDrag:
print("Drag to: ", event.position)
Input Axis
func _process(delta):
# Get axis (-1 a 1)
var horizontal = Input.get_axis("move_left", "move_right")
var vertical = Input.get_axis("move_up", "move_down")
velocity = Vector2(horizontal, vertical) * SPEED
Best Practices
- Usa Input Actions per flessibilità
- Supporta keyboard, mouse e gamepad
_input()per eventi singoli_process()per input continuo- Usa
get_axis()per movimento