You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot 4调用move_and_slide()报错:参数过多问题求助

解决Godot 4中CharacterBody2D的move_and_slide()参数错误问题

你的问题确实是Godot 3.X到4的API变更导致的——Godot 4重构了CharacterBody2D的移动逻辑,move_and_slide()方法不再接收运动向量参数,而是需要先设置节点的velocity属性,再调用无参数的move_and_slide()。

修改后的代码如下:

extends CharacterBody2D
var speed = 200
var player = null

func _physics_process(delta):
    velocity = Vector2.ZERO
    if player:
        velocity = position.direction_to(player.position) * speed
    move_and_slide()

func _on_area_2d_body_entered(body):
    print("entered")
    player = body

func _on_area_2d_body_exited(body):
    print("exit")
    player = null

关键修改说明:

  • 移除了原来的motion变量,直接使用CharacterBody2D内置的velocity属性
  • 调用move_and_slide()时不再传入参数,Godot 4会自动使用velocity属性的值处理移动
  • 原代码中的FLOOR_NORMAL常量如果没有用到地面检测逻辑,可以直接删除;如果需要处理斜坡或地面判断,可结合is_on_floor()等方法配合使用

这样修改后,精灵就能正常向玩家移动,同时符合Godot 4的API规范。

内容的提问来源于stack exchange,提问作者Lebouskie Chickpea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 05:25:06