プレイヤーの移動
プラットフォーマー(Platformer)
プラットフォーマーはBasicMovementで問題ないと思う
extends CharacterBody2D
const SPEED = 100.0
const JUMP_VELOCITY = -200.0
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
トップダウン(Top-Down)
TopDownはGravity(重力)の影響を受けないようにするので、Gravityは無し。
extends CharacterBody2D
const SPEED = 100.0
var direction:Vector2
func _physics_process(delta: float) -> void:
direction.x = Input.get_axis("move_left", "move_right")
direction.y = Input.get_axis("move_up", "move_down")
if direction:
velocity = direction.normalized() * SPEED
else :
velocity = Vector2.ZERO
move_and_slide()
グリッド(Grid)
グリッドはタイルサイズを基準に半分だけPositionを進める
extends CharacterBody2D
var tile_size = 16
var inputs = {"move_right": Vector2.RIGHT,
"move_left": Vector2.LEFT,
"move_up": Vector2.UP,
"move_down": Vector2.DOWN}
func _ready():
position = position.snapped(Vector2.ONE * tile_size)
position += Vector2.ONE * tile_size / 2
func _physics_process(delta: float) -> void:
for dir in inputs.keys():
if Input.is_action_just_pressed(dir):
position += inputs[dir] * tile_size