启动引擎
本课属于 Coddy Godot 学习旅程的“项目”单元。第 12 课,共 36 课。
按下向上键会启动引擎,并沿着飞船船头指向的方向推动飞船。Vector2.UP 是 (0, -1)。rotated(rotation) 会根据飞船转过的角度旋转这个箭头:
if Input.is_action_pressed("move_up"):
velocity += Vector2.UP.rotated(rotation) * THRUST * delta
position += velocity * delta引擎改变的是速度,而不是位置。松开向上键后,飞船还会继续漂移。要停下来,就掉头并朝相反方向加速。
飞船有一个隐藏的 Flame 子节点。引擎启动时显示它:$Flame.visible = Input.is_action_pressed("move_up")。
挑战
中等让引擎推动飞船。
- 添加
const THRUST = 300。 - 按住 Up 时,将
Vector2.UP.rotated(rotation) * THRUST * delta加到velocity上。 - 每一帧都用
velocity * delta移动飞船,无论是否按住 Up。 - 只有按住 Up 时才显示
$Flame。
按“开始”并按住 Up:火焰会燃烧,松开后飞船仍会继续漂移。然后按“检查”。
亲手试试
extends CharacterBody2D
const TURN_SPEED = 4.0
func _physics_process(delta):
rotation += Input.get_axis("move_left", "move_right") * TURN_SPEED * delta
项目中的所有课
自己练习: Godot 在线编辑器