参考
本例修改自官方文档”物理学“部分的“使用 KinematicBody2D“一文中的示例。



extends KinematicBody2Dvar speed = 120.0 # 速度 像素/秒var velocity = Vector2.ZERO # 速度向量var gravity = 5000 # 重力加速度var jump_force = 1000.0 # 单次跳跃高度func _process(delta):if is_on_floor():velocity .y = 0if is_on_wall():velocity.y = Input.get_axis("ui_up","ui_down") * speedvelocity.x = Input.get_axis("ui_left","ui_right") * speedif Input.is_action_just_pressed("ui_accept"): # 按空格键velocity.y = -jump_forcemove_and_slide_with_snap(velocity,get_floor_normal(),Vector2.UP)return# 左右移动velocity.x = Input.get_axis("ui_left","ui_right") * speed# 实现下落velocity.y += gravity * delta# 跳跃if Input.is_action_just_pressed("ui_accept"): # 按空格键velocity.y = -jump_forcevelocity = move_and_slide(velocity,Vector2.UP)

