概要
Godotには押した瞬間、押している間、離した瞬間を判定する3つの入力メソッドがあります。
入力メソッドの種類
is_action_just_pressed()
押された瞬間のみtrue:ジャンプ、攻撃、メニュー開閉など
is_action_pressed()
押されている間ずっとtrue:移動、ダッシュ、チャージなど
is_action_just_released()
離された瞬間のみtrue:チャージ攻撃の発動、長押し終了など
実装例
func _process(delta):
# ジャンプ:押した瞬間のみ
if Input.is_action_just_pressed("jump"):
if is_on_floor():
velocity.y = JUMP_VELOCITY
# 移動:押している間ずっと
if Input.is_action_pressed("move_right"):
velocity.x = speed
else:
velocity.x = 0
# チャージ攻撃:押している間チャージ、離した瞬間に発射
if Input.is_action_pressed("charge"):
charge_power += charge_rate * delta
if Input.is_action_just_released("charge"):
fire_charged_shot(charge_power)
charge_power = 0.0
よくある間違い
# 間違い:ジャンプが連続する
if Input.is_action_pressed("jump"): # ❌ 押している間ずっとジャンプ
velocity.y = JUMP_VELOCITY
# 正しい:1回だけジャンプ
if Input.is_action_just_pressed("jump"): # ✅ 押した瞬間のみ
velocity.y = JUMP_VELOCITY
アナログ入力
get_action_strength():アナログスティックの傾き具合(0.0〜1.0)を取得
func _process(delta):
# アナログスティックの入力強度を取得
var move_strength = Input.get_action_strength("move_forward")
# 入力強度に応じた速度調整
velocity.x = speed * move_strength
使い分け表
メソッド | 用途 |
---|---|
is_action_just_pressed() | ジャンプ、攻撃、メニュー開閉 |
is_action_pressed() | 移動、ダッシュ、チャージ |
is_action_just_released() | チャージ攻撃発動 |
get_action_strength() | アナログ移動 |
補足
単発アクション(is_action_just_pressed)と、連続アクション(is_action_pressed)はケアレスミスをしがちなので注意しましょう。