鱼C论坛

 找回密码
 立即注册
查看: 19|回复: 1

godot又双求助

[复制链接]
发表于 昨天 20:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
c# godot求助,碰撞函数没有启用(没有报错)
  1. using Godot;
  2. using System;
  3. using System.Threading.Tasks;

  4. public partial class Enemy : CharacterBody2D
  5. {
  6.     // 敌人变量
  7.     [Export] public int health;
  8.     [Export] public int attack;
  9.     [Export] public float speed;
  10.     [Export] public CharacterBody2D player;


  11.     private AnimationPlayer animation;
  12.     private Label label;
  13.     private bool is_destruction = false;
  14.     private bool is_attacking = false;

  15.     public override void _Ready()
  16.     {
  17.         base._Ready();
  18.         GetNode<Area2D>("Destruction").BodyEntered += OnDestructionBodyEntered;
  19.     }


  20.     public override void _EnterTree()
  21.     {
  22.         base._EnterTree();
  23.         animation = GetNode<AnimationPlayer>("AnimationPlayer");
  24.         player = GetNode<CharacterBody2D>("/root/Main/Player");
  25.         label = GetNode<Label>("/root/Main/Hud/Label2");
  26.     }


  27.     public override void _Process(double delta)
  28.     {
  29.         base._Process(delta);

  30.         

  31.         Move((float)delta);
  32.         if (!is_attacking)
  33.         {
  34.             MoveAndSlide();
  35.         }

  36.         for (int i = 0; i < GetSlideCollisionCount(); i++)
  37.         {
  38.             KinematicCollision2D collision = GetSlideCollision(i);
  39.             GodotObject collider = collision.GetCollider();
  40.         }

  41.         // 自爆
  42.         if (Position.DistanceTo(player.Position) <= 100.0f && is_destruction == false)
  43.         {
  44.             Destruction();
  45.         }

  46.         if (health <= 0)
  47.         {
  48.             QueueFree();
  49.         }
  50.     }

  51.     // 移动
  52.     private void Move(float delta)
  53.     {
  54.         LookAt(player.Position);

  55.         Vector2 direction = Vector2.Right.Rotated(Rotation).Normalized();
  56.         Vector2 movement = direction * speed * (float)delta;
  57.         Velocity += movement;
  58.     }

  59.     private void OnDestructionBodyEntered(Node2D body)
  60.     {
  61.         GD.Print("a");
  62.         if (body == player && is_attacking == false)
  63.         {
  64.             GD.Print("b");
  65.             is_attacking = true;
  66.             player.Set("health", (int)player.Get("health") - attack);
  67.             label.Set("score", (int)label.Get("score") + 1);
  68.         }
  69.     }


  70.     private async Task Destruction()
  71.     {
  72.         is_destruction = true;
  73.         animation.Play("destruction");
  74.         Timer time = new Timer();
  75.         time.WaitTime = 0.15f;
  76.         AddChild(time);
  77.         time.Start();

  78.         await ToSignal(time, "timeout");

  79.         QueueFree();
  80.     }
  81. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 20:24 | 显示全部楼层
关于Godot碰撞检测未触发的问题排查

以下是常见原因和解决方案:

1. 碰撞层设置问题
检查Destruction区域节点的Collision Layer/Mask是否与目标物体的Layer匹配:

  1. // 在_Ready()中可动态设置(示例)
  2. GetNode<Area2D>("Destruction").CollisionMask = 1; // 设置检测第1层
复制代码


2. 节点类型问题
确保"Destruction"节点是Area2D类型而不是静态碰撞体

3. 信号连接问题
建议改用Godot编辑器的可视化信号连接:
  • 在场景面板右键Destruction节点
  • 选择"连接到信号"
  • 选择BodyEntered信号
  • 连接到Enemy脚本


4. 监控状态问题
检查Area2D的Monitoring属性是否开启

5. 参数类型问题
将事件处理方法改为:

  1. private void OnDestructionBodyEntered(Node2D body)
  2. {
  3.     GD.Print("碰撞检测到:" + body.Name);
  4.     // 添加你的逻辑...
  5. }
复制代码


6. 调试建议
在方法内添加打印语句测试是否触发:

  1. private void OnDestructionBodyEntered(Node2D body)
  2. {
  3.     GD.Print("[碰撞触发] 对象:", body.Name);
  4.     // 其他逻辑...
  5. }
复制代码


7. 移动逻辑冲突
检查是否在移动时覆盖了碰撞位置,可尝试注释掉MoveAndSlide()测试

其他注意事项:
  • 确保目标物体至少有一个CollisionShape2D
  • 检查场景中所有节点的可见性(Hidden属性是否关闭)
  • 运行时可打开Godot的调试 -> 可见碰撞体辅助调试


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-20 06:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表