2Dトップダウン脱出ゲームチュートリアル Part 2: プレイヤーキャラクターの移動と操作の実装
Godot Engine バージョン4.3
Godot 4.3を使って、2Dトップダウン脱出ゲームのプレイヤーキャラクターの移動や操作を実装します。このチュートリアルでは、CharacterBody2Dを使用した移動、アニメーション、アイテムの取得、そして検知エリアを通じてオブジェクトとインタラクションする方法を学びます。
このパートでは、プレイヤーキャラクターがゲーム内で移動し、アイテムを取得し、オブジェクトとインタラクションする仕組みを作ります。移動は
CharacterBody2D
を使って実装し、アニメーションと検知エリアを通じて、プレイヤーの動きを表現します。ステップ1: ノード構成
プレイヤーキャラクターには以下のノード構成が必要です:
CharacterBody2D
: プレイヤーの基本的な移動と物理処理を行います。
CollisionShape2D
: プレイヤーの衝突判定用のシェイプです。
AnimatedSprite2D
: プレイヤーのアニメーションを表示します。
Camera2D
: プレイヤーを中心にカメラが動くようにします。
Area2D(DetectionArea)
: プレイヤーの前方にオブジェクトが接触する領域を作成します(アイテム検知)。
CollisionShape2D
: 領域の範囲判定用のシェイプです。
Project Settings
のInput Map
で全てのキーのアクションを設定します。これはお好みですが、ゴドチューの場合は下記になります。
AnimatedSprite2D
にアニメーションを設定します。下記のように設定します。
ステップ2: スクリプトの設定
まず、プレイヤーのスクリプトを作成します。以下はそのサンプルスクリプトです:
extends CharacterBody2D
@export var speed = 50.0
@onready var animation = $AnimatedSprite2D # アニメーション用ノード@onready var detection_area = $DetectionArea # プレイヤーの前方にある検知エリア
var dir = "down" # プレイヤーの向き(初期は下)var is_moving = false # 移動中かどうかのフラグvar object_interactable: Interactable # 触れられるオブジェクトの参照var can_move = true # プレイヤーが移動可能かどうかのフラグ
# 毎フレームの物理処理func _physics_process(delta): # 移動不可状態 if !can_move: is_moving = false animate() return # 移動処理 if Input.is_action_pressed("left"): velocity = Vector2.LEFT * speed detection_area.rotation_degrees = 90 # 左向き is_moving = true dir = "left" elif Input.is_action_pressed("right"): velocity = Vector2.RIGHT * speed is_moving = true detection_area.rotation_degrees = 270 # 右向き dir = "right" elif Input.is_action_pressed("up"): velocity = Vector2.UP * speed is_moving = true detection_area.rotation_degrees = 180 # 上向き dir = "up" elif Input.is_action_pressed("down"): velocity = Vector2.DOWN * speed is_moving = true detection_area.rotation_degrees = 0 # 下向き dir = "down" else: velocity = Vector2.ZERO is_moving = false
move_and_slide() animate()
# アニメーション制御func animate(): if is_moving: animation.play(dir) # 移動中のアニメーション else: animation.play("idle_" + dir) # 静止中のアニメーション
# 検知エリアにオブジェクトが入った際の処理func _on_detection_area_body_entered(body): if body is Interactable: object_interactable = body
# 検知エリアからオブジェクトが出た際の処理func _on_detection_area_body_exited(body): object_interactable = null
# アイテムを取得する処理func pickup(): if object_interactable.is_pickable: object_interactable.queue_free() object_interactable = null # プレイヤーの移動を停止するfunc player_move_stop(): can_move = false
# プレイヤーの移動を再開するfunc player_move_start(): can_move = true
ステップ3: プレイヤーキャラクターの移動
このスクリプトでは、
_physics_process
メソッドでプレイヤーの入力を検出し、移動方向に応じてvelocity
を設定しています。move_and_slide()
で実際の移動を行い、animate()
でアニメーションを制御します。
ステップ4: アニメーションの制御
animate()
メソッドでは、移動中は方向ごとのアニメーションを再生し、移動していない場合はidle_方向
のアニメーションを再生します。これにより、プレイヤーが移動しているときと静止しているときで異なるアニメーションを表示できます。
ステップ5: 検知エリア
Area2D
(DetectionArea
)はプレイヤーの前方に設置され、アイテムやインタラクティブオブジェクトを検出します。_on_detection_area_body_entered
と_on_detection_area_body_exited
で接触したオブジェクトを検出し、アイテムの取得やインタラクションを行います。
ステップ6: アイテムの取得
アイテムを取得するために、
pickup()
メソッドを使用します。Interactable
タイプのオブジェクトに接触したときにアイテムをインベントリに追加し、拾える場合はオブジェクトを削除します。
これで、プレイヤーキャラクターの移動と操作の実装が完了しました!次回もお楽しみに!
1
100%