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

Godot C#中Area3D的BodyShapeEntered信号委托如何正确定义?

解决Godot 4.2 C#中Area3D.BodyShapeEntered信号委托不匹配问题

编译错误的核心原因是你定义的OnCollision方法参数类型与Area3D.BodyShapeEnteredEventHandler委托的要求不匹配,具体是第二个参数的类型错误。

在Godot 4.2的C#绑定中,BodyShapeEntered信号对应的委托参数格式为:

  • Rid:碰撞体的RID标识
  • CollisionObject3D:进入区域的碰撞体节点(所有具备碰撞属性节点的基类,涵盖PhysicsBody3D、Area3D等)
  • int:碰撞体上触发碰撞的形状索引
  • int:区域自身参与碰撞的形状索引

修正后的完整代码如下:

using Godot;
using System;

public partial class LocationExit : Area3D
{
    public static readonly PackedScene DefaultExitScene = ResourceLoader.Load<PackedScene>("res://Overworld/Overworld.tscn");
    
    public PackedScene ExitScene;//alternate scene to exit to
    string StoryVariableOnExit;//increments this variable in the story on collision
    
    public override void _Ready()
    {
        this.BodyShapeEntered += OnCollision;
    }

    public override void _Process(double delta)
    {
    }
    
    // 修正参数类型后的处理方法
    private void OnCollision(Rid body_rid, CollisionObject3D body, int body_shape_index, int local_shape_index)
    {
        // 这里可以编写碰撞逻辑,比如判断是否是玩家角色
        if (body is CharacterBody3D player)
        {
            // 执行场景切换或剧情变量更新逻辑
            if (ExitScene != null)
            {
                GetTree().ChangeSceneToPacked(ExitScene);
            }
            else
            {
                GetTree().ChangeSceneToPacked(DefaultExitScene);
            }
        }
    }
}

如果需要针对特定类型的碰撞体做逻辑处理,只需在方法内通过is关键字进行类型判断和转换即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 12:24:53