敌人不受子弹的影响

发布于 2025-02-11 02:35:00 字数 240 浏览 1 评论 0原文

这很愚蠢,但我似乎无法修复它。敌人是场景中的随机实例,因此我将脚本附加到具有此代码的主要敌人场景上:

func _on_Enemy_body_entered(body):
    if is_in_group("bullet"):
        Player.score += 1
        queue_free()

子弹场景,当玩家射击在一个称为“子弹”的小组中时,它也将其本身在主要场景中。

This is very stupid but I can't seem to fix it. The enemy is randomly instance in the scene so I attached a script to the main enemy scene that has this code:

func _on_Enemy_body_entered(body):
    if is_in_group("bullet"):
        Player.score += 1
        queue_free()

The bullet scene which is also instanced to the main scene when the player shoots is in a group called "bullet".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧竹 2025-02-18 02:35:00

首先,通常的调试方法适用:

  • 添加打印语句或断点以查看给定点的变量值(或者如果完全执行)。
  • 从“调试”菜单中启用“可见碰撞形状”,以查看山脉是否在执行过程中的期望。
  • 当游戏运行时,在场景面板的“远程选项卡”上选择节点,以在检查员中实时查看属性值。

鉴于代码,我希望您发现IF语句中的代码根本根本没有执行。

func _on_Enemy_body_entered(body):
    if is_in_group("bullet"):
        Player.score += 1
        queue_free()

您说代码在敌人中。敌人是一个叫做“子弹”的群体?我猜不是。因此,is_in_group(“ Bullet”)评估false,因此执行流不输入IF语句。

需要清楚,如果IS_IN_GROUP(“ Bullet”):If self.is_in_group(“ Bullet”):相同。它在附加脚本的对象上调用is_in_group


由于您提到子弹位于一个名为“子弹”的小组中,因此我还会猜测您想检查敌人相撞的尸体。也就是说:

func _on_Enemy_body_entered(body):
    if body.is_in_group("bullet"):
        Player.score += 1
        queue_free()

应该更好。

我看不到您删除子弹,但我无法分辨是否是故意的。

通常考虑检测碰撞。因此,它可能仍然不起作用。您可以看到其他地方以进行完整的说明。对于简短版本:检查是否启用了监视(并且报告的联系人在刚体上足够大),并检查碰撞层和口罩重叠,并且碰撞形状未被禁用,当然,请检查信号是否连接。

First of all, the usual debugging approaches apply:

  • Add print statements or breakpoints to see the value of variables at a given point (or if that executes at all).
  • Enable "Visible Collision Shapes" from the debug menu to see if the colliders are how you expect during execution.
  • Select the nodes on the remote tab of the scene panel when the game is running to see the values of properties in real time in the inspector.

Given the code, I expect you to find that the code inside the if statement is not executing at all.

func _on_Enemy_body_entered(body):
    if is_in_group("bullet"):
        Player.score += 1
        queue_free()

You say the code is in the enemy. Is the enemy is a group called "bullet"? I'm going to guess it isn't. So is_in_group("bullet") evaluates to false, and thus the execution flow does not enter the if statement.

To be clear, if is_in_group("bullet"): is the same as if self.is_in_group("bullet"):. It is calling is_in_group on the object on which the script is attached.


Since you mention the bullet is in a group called "bullet", I'm also going to guess you want to check the body that collided the enemy. That is:

func _on_Enemy_body_entered(body):
    if body.is_in_group("bullet"):
        Player.score += 1
        queue_free()

That should be better.

I don't see you delete bullet, but I can't tell if that is intended or not.

There are the usual consideration to detect collisions. So it might still not work. You can see elsewhere for a full explanation. For the short version: check if monitoring is enabled (and contacts reported is large enough on rigid bodies), and check the collision layers and masks overlap, and that the collision shapes are not disabled, and of course check that the signals are connected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文