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

Godot中on_body_entered未触发问题求助及原因咨询

Godot武器攻击蘑菇交互问题:body_entered信号与分组判断失效原因

问题背景

开发Godot游戏时,需实现武器攻击蘑菇触发受击动画的功能,但两者始终无法正常交互。核心问题如下:

  • 武器Hitbox的_on_hitbox_body_entered回调完全未触发,连基础打印都无输出
  • 改用_on_hitbox_area_entered后信号能触发,但通过area.is_in_group("hit")判断分组并调用on_hit()无效
  • 最终改用area.has_method("on_hit")后功能正常,需明确上述两个问题的原因

场景结构

  • Player场景下包含weapon(Node2D)节点
  • weapon节点下包含两个子节点:AnimatedSprite2D、hitbox(Area2D)
  • 蘑菇为独立的Node2D节点,已加入"hit"分组
  • 武器Hitbox与蘑菇的碰撞层、掩码均设置为3

相关代码

武器脚本

extends Node2D

@onready var animated_sprite = $AnimatedSprite2D
@onready var collision_shape = $Hitbox/CollisionShape2D

func _physics_process(delta):
    if Input.is_action_just_pressed("attack"):
        collision_shape.disabled = false
        print(collision_shape.disabled)
        animated_sprite.play("sword_attack")
    else:
        collision_shape.disabled = true
        print(collision_shape.disabled)

func _on_hitbox_body_entered(body):
    print("on body entered")
    if body.is_in_group("hit"):
        body.on_hit()

蘑菇脚本

extends Node2D

@onready var animated_sprite = $AnimatedSprite2D

func on_hit():
    animated_sprite.play("slash_mushroom")
    # queue_free() when animation is over.

原因分析

1. body_entered信号未触发的本质原因

Area2D的body_entered信号仅对PhysicsBody2D类型节点生效,包括CharacterBody2D、RigidBody2D、StaticBody2D等带有物理体属性的节点。你的蘑菇是普通Node2D,没有继承任何物理体类,也未挂载物理体组件,因此武器Hitbox不会向其发送body_entered信号,回调自然不会执行。

2. area.is_in_group("hit")判断失效的原因

当area_entered信号触发时,回调参数中的area是进入Hitbox的Area2D节点(即蘑菇节点下负责碰撞的子节点)。而你将"hit"分组添加给了蘑菇的根Node2D节点,而非其下的Area2D子节点,因此area.is_in_group("hit")检查的是Area2D节点的分组状态,而非蘑菇根节点的分组,结果为false,导致无法调用on_hit()。

改用area.has_method("on_hit")能生效,是因为on_hit()方法定义在蘑菇的根Node2D上,Godot在执行节点方法调用时,若当前节点(Area2D)无对应方法,会自动向上遍历节点树,在其父节点(蘑菇根Node2D)中找到该方法并执行。

优化建议

  • 若想使用body_entered信号:将蘑菇节点改为继承StaticBody2D(适合静态物体),保持碰撞层与掩码设置一致即可。
  • 若继续使用area_entered信号:将"hit"分组添加给蘑菇的Area2D子节点,或在回调中通过area.get_parent().is_in_group("hit")判断根节点的分组状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 04:46:18