到处行走
本课属于 Coddy Godot 学习旅程的“输入与移动”单元。第 10 课,共 28 课。
velocity 有两个数字,你可以分别设置它们。velocity.x 控制左右移动。velocity.y 控制上下移动。每个方向都有自己的轴:
velocity.x = Input.get_axis("move_left", "move_right") * speed
velocity.y = Input.get_axis("move_up", "move_down") * speed
move_and_slide()对于 velocity.y,向上方向排在前面,所以按住向上键时,值为 -1。这会让英雄向屏幕上方移动。
按住向上键和向右键,英雄就会同时向两个方向移动。斜着走向柱子,看看它如何滑动。
挑战
简单让英雄也能上下移动。
- 在
velocity.x这一行下面,用Input.get_axis("move_up", "move_down")以相同的方式设置velocity.y。 - 使用相同的
speed。
按“开始”:方向键可以让英雄向各个方向移动。然后点击“检查”。
亲手试试
extends CharacterBody2D
var speed = 200
func _physics_process(delta):
velocity.x = Input.get_axis("move_left", "move_right") * speed
move_and_slide()
输入与移动中的所有课
自己练习: Godot 在线编辑器