Area2D 不触发 Godot 中的对象
这是我的代码:(只是为了让你知道我是一个初学者,我这周才刚刚开始,尽管我确实了解其他语言和游戏引擎)
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.
The "Player" GameObject is the one with the script that contains the detection if its in the area2D.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想检查
Area2D
在运行时是否定位正确,请启用“调试”->;可见的碰撞形状。如果您想检查
_on_Area2D_area_entered
是否正在运行,请添加断点(或使用 print)。您收到错误了吗?
如果那里没有
Node
,这个表达式将在运行时导致错误:如果你想能够检查,你可以使用
get_node_or_null
和is_instance_valid
。由于您没有提到任何错误,我猜测该方法没有运行。
如果该方法没有运行,最可能的罪魁祸首是 - 我猜给定的名称该方法 - 您连接了
“area_entered”
信号,但打算连接“body_entered”
信号。当另一个
Area2D
进入Area2D
时,"area_entered"
信号将被触发。但我在场景树中只看到一个Area2D
。另一方面,当PhysicsBody2D
(例如StaticBody2D
、KinematicBody2D
、RigidBody2D
)进入Area2D
。无论哪种情况,您都会获得作为方法参数输入的内容。Area2D
可能无法检测到您想要的内容的其他原因包括collision_layer
和collision_mask
没有交集以及monitoring
禁用。并消除一些可能的误解:
Area2D
或PhysicsBody2D
时,"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:If you want to be able to check, you can use
get_node_or_null
andis_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 anotherArea2D
enters theArea2D
. But I only see oneArea2D
in your scene tree. On the other hand the"body_entered"
will trigger when aPhysicsBody2D
(e.gStaticBody2D
,KinematicBody2D
,RigidBody2D
) enters theArea2D
. 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 ofcollision_layer
andcollision_mask
andmonitoring
being disabled.And to dismiss a couple possible misconceptions:
"area_entered"
and"body_entered"
trigger when theArea2D
orPhysicsBody2D
respectively enter theArea2D
, not every frame they are inside. Sorotation_degrees += 1
is not a rotation animation.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.