Area2D 不触发 Godot 中的对象

发布于 2025-01-16 14:32:23 字数 501 浏览 1 评论 0原文

这是我的代码:(只是为了让你知道我是一个初学者,我这周才刚刚开始,尽管我确实了解其他语言和游戏引擎)

func _on_Area2D_area_entered(area):
    get_parent().get_node("Level 1/Area2D/Flag").rotation_degrees += 1

我试图完成的是玩家游戏对象将查看它是否在旗帜的区域,如果是,旗帜就会旋转。

我不确定问题出在哪里。我认为它可能在第二行。如果我的设置错误,我在下面提供了屏幕截图。我查看了有关同一主题的其他密切问题,但他们没有回答我的问题。

输入图片这里的描述

“玩家”游戏对象是一个包含包含检测(如果它位于二维区域)的脚本的游戏对象。

Here is my code: (just so you know I am a beginner and I just started this week, though I do have knowledge with other languages and game engines)

func _on_Area2D_area_entered(area):
    get_parent().get_node("Level 1/Area2D/Flag").rotation_degrees += 1

What I was trying to accomplish was that the Player GameObject would see if its in the area of the Flag, and if it is, the flag would rotate.

I am not sure where the issue is. I think it is probably in the second line. I provided a screenshot below if I did the setup wrong. I have looked at the other close questions asked on the same topic, but they did not answer my question.

enter image description here

The "Player" GameObject is the one with the script that contains the detection if its in the area2D.

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

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

发布评论

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

评论(1

极度宠爱 2025-01-23 14:32:23

如果您想检查 Area2D 在运行时是否定位正确,请启用“调试”->;可见的碰撞形状。

如果您想检查 _on_Area2D_area_entered 是否正在运行,请添加断点(或使用 print)。


您收到错误了吗?

如果那里没有 Node ,这个表达式将在运行时导致错误:

get_parent().get_node("Level 1/Area2D/Flag")

如果你想能够检查,你可以使用 get_node_or_nullis_instance_valid

由于您没有提到任何错误,我猜测该方法没有运行。


如果该方法没有运行,最可能的罪魁祸首是 - 我猜给定的名称该方法 - 您连接了“area_entered”信号,但打算连接“body_entered”信号。

当另一个 Area2D 进入 Area2D 时,"area_entered" 信号将被触发。但我在场景树中只看到一个 Area2D。另一方面,当PhysicsBody2D(例如StaticBody2DKinematicBody2DRigidBody2D)进入Area2D。无论哪种情况,您都会获得作为方法参数输入的内容。

Area2D 可能无法检测到您想要的内容的其他原因包括 collision_layercollision_mask 没有交集以及 monitoring禁用。


并消除一些可能的误解:

  • Area2DPhysicsBody2D 时,"area_entered""body_entered" 会触发分别进入 Area2D,而不是它们所在的每个帧。 所以 rotation_ Degrees += 1 不是旋转动画。
  • 您将收到任何触发信号的通知,而不仅仅是您连接到的对象。您可能需要进一步过滤,例如if body == self:

对于通过搜索到达这里的人,我想链接一个类似的案例:敌人不受子弹影响。还有我的如何设置物理节点的完整说明

If you want to check if the Area2D is positioned correctly during runtime enable Debug -> Visible Collision Shapes.

If you want to check if _on_Area2D_area_entered is running, add breakpoints (or use print).


Did you get an error?

If there isn't a Node there, this expression will cause an error in runtime:

get_parent().get_node("Level 1/Area2D/Flag")

If you want to be able to check, you can use get_node_or_null and is_instance_valid.

Since you didn't mention any error, I'm going to guess the method is not running.


If the method is not running, the most likely culprit is that - I'm guessing given then name of the method - you connected the "area_entered" signal but intended to connect the "body_entered" signal.

The "area_entered" signal will trigger when another Area2D enters the Area2D. But I only see one Area2D in your scene tree. On the other hand the "body_entered" will trigger when a PhysicsBody2D (e.g StaticBody2D, KinematicBody2D, RigidBody2D) enters the Area2D. In either case you get what entered as a parameter of the method.

Other reasons why the Area2D might not be detecting what you want include no intersection of collision_layer and collision_mask and monitoring being disabled.


And to dismiss a couple possible misconceptions:

  • The "area_entered" and "body_entered" trigger when the Area2D or PhysicsBody2D respectively enter the Area2D, not every frame they are inside. So rotation_degrees += 1 is not a rotation animation.
  • You will get notifications of anything that trigger the signals, not just the object to which you connected it. You may have to further filter, e.g. if body == self:.

For people arriving here from search, I want to link a similar case: Enemy is not affected by bullets. And also my full explanation of how to set up physic nodes.

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