在这种情况下,玩家使用四个方向键移动(包括对角线)
extends CharacterBody2D
var speed = 400 # speed in pixels/sec
func _physics_process(delta):
var direction = Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
move_and_slide()
方案 2:旋转和移动
在这种情况下,左/右操作可以旋转角色,而上/下操作则可以让角色朝任何方向前进或后退。这有时被称为 "小行星式 "移动。
extends CharacterBody2D
var speed = 400 # move speed in pixels/sec
var rotation_speed = 1.5 # turning speed in radians/sec
func _physics_process(delta):
var move_input = Input.get_axis("down", "up")
var rotation_direction = Input.get_axis("left", "right")
velocity = transform.x * move_input * speed
rotation += rotation_direction * rotation_speed * delta
move_and_slide()