在Godot 3D中使用Body_entered和Body_ex的示例(版本3.4.4)
我正在尝试做一个传送的部分,因此我需要检测身体何时与区域发生碰撞。我尝试使用 body_enter
/ body_entered
和 body_exit
/ body_exited
,但我不知道 他们工作,我需要在哪里插入它们。我可以使用它的示例代码吗?
我的节点路径:
RigidBody
├ CollisionShape
├ MeshInstance
├ Area
├ ├ CollisionShape
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
场景树结构
首先,如果您希望两件事碰撞,则可能希望它们成为兄弟姐妹。 不是一个
节点
另一个孩子的孩子。这是因为空间
s是相对于父母的。换句话说:当空间
移动时,其孩子随它移动。启用监视
第二,您要确保将检测到碰撞的
节点
能够做到这一点。对于刚性机体
和区域
,区域
将检测arigidbody
,为此必须拥有监视
为true
这是默认值。因此,除非您搞砸了它,否则您不需要做任何事情。层和蒙版
第三,您希望层和掩码重叠。特别是,您希望
collision_mask
区域
具有与arigd> arigid> arigidbody
的collision_layer
的共同点。 默认情况下,它们带有第一个位,因此默认情况下也可以满足它。信号
第四,您需要 connect
“ body_entered”
区域的信号
。您有两种方法:使用编辑器:
在场景面板中选择
区域
(默认情况下为左侧),然后转到节点面板(默认情况下为右侧),然后转到“信号”选项卡。在那里您应该找到“ body_entered(body:node)”。双击它。 或选择它,然后单击底部的“连接…”按钮,或按Enter。
godot会要求您选择
节点
您将其连接到。您可以将其连接到具有事先附加脚本的同一场景上的任何节点
。 我建议将其连接到区域
本身。godot还将允许您指定可以处理它的方法的名称(默认情况下,名称为
_ON_AREA_BODY_ENTERED
)。在“高级”下,您还可以将额外的参数添加到该方法中,无论信号是否可以/将等待下一帧(“递延”),以及一旦触发,它就会断开自己(“ Oneshot”)。
通过按“连接”按钮,Godot将相应地连接信号,如果目标节点不存在,则用“目标节点”脚本中的名称创建一种方法。
来自代码:
要连接来自代码的信号,您使用连接方法。 另请参见 is_connected >
因此,您可以使用
连接
方法连接“ body_entered”
信号,例如:此代码旨在在附加到
区域
本身的脚本中工作。 也可以调用连接
从node
到另一个。请注意,我们正在传递我们要连接的方法的名称连接
方法的参数,因此可以是您想要的。connect
方法具有可选参数以指定额外的参数,或者如果要延期连接或onsothot。或需要将信号连接到它。并且该方法没有特殊名称,它可以选择您选择的任何名称。
完成此操作后,该方法应在
argidbody
重叠afear
时执行。Scene tree structure
First of all, if you want two things to collide you probably want them to be siblings. Not one
Node
child of the other. This is becauseSpatial
s are placed relative to their parent. Said another way: when anSpatial
moves, its children move with it.Enable monitoring
Second, you want to make sure that the
Node
that will detect the collision is able to do it. In the case of aRigidBody
and anArea
, theArea
will detect theRigidBody
, and for that it must havemonitoring
to betrue
This is the default. Thus, unless you have been messing with it, you need to do nothing.Layers and masks
Third, you want the layers and masks to overlap. In particular, you want the
collision_mask
of theArea
to have bits in common with thecollision_layer
of theRigidBody
. By default, they come with the first bit set, so this is also satisfied by default unless you have been messing with it.SIGNALS
Fourth, you need to connect the
"body_entered"
signal of theArea
. You have two ways to do that:Using the editor:
Have the
Area
selected in the Scene panel (on the left by default), and then go to the Node panel (on the right by default) and then to the Signals tab.There you should find "body_entered(body:Node)". Double click it. Or select it and then click the "Connect…" button at the bottom, or press enter.
Godot will ask you to select the
Node
you will connect it to. You can connect it to anyNode
on the same scene that has a script attached beforehand. I suggest to connect it to theArea
itself.Godot will also allow you to specify the name of the method that will handle it (by default the name will be
_on_Area_body_entered
).Under "advanced" you can also add extra parameters to be passed to the method, whether or not the signal can/will wait for the next frame ("deferred"), and if it will disconnect itself once triggered ("oneshot").
By pressing the Connect button, Godot will connect the signal accordingly, creating a method with the provided name in the script of the target node if it does not exist.
From code:
To connect a signal from code you use the connect method. See also disconnect and is_connected.
So you can, use the
connect
method to connect the"body_entered"
signal, like this:This code is intended to work in a script attached to the
Area
itself. It is also possible to callconnect
from aNode
to another. Please note that we are passing the name of the method we want to connect as parameter to theconnect
method, and thus it can be whatever you want.The
connect
method has optional parameters to specify extra parameters, or if you want the connection to be deferred or oneshot.To reiterate: simply adding a method won't work, you need to connect the signal to it. And there is no special name for the method, it can have any name you choose.
With that done, the method should execute when the
RigidBody
overlaps theArea
.