在Godot 3D中使用Body_entered和Body_ex的示例(版本3.4.4)

发布于 2025-02-04 06:05:29 字数 306 浏览 4 评论 0 原文

我正在尝试做一个传送的部分,因此我需要检测身体何时与区域发生碰撞。我尝试使用 body_enter / body_entered body_exit / body_exited ,但我不知道 他们工作,我需要在哪里插入它们。我可以使用它的示例代码吗? 我的节点路径:

RigidBody
├ CollisionShape
├ MeshInstance
├ Area
├ ├ CollisionShape

I am trying to make a teleport part, so I need to detect when Body collides with area. I tried to use body_enter/body_entered and body_exit/body_exited, but I do not know how exactly they work and where do I need to insert them. Can I have example codes of using it?
My nodes path:

RigidBody
├ CollisionShape
├ MeshInstance
├ Area
├ ├ CollisionShape

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

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

发布评论

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

评论(1

从来不烧饼 2025-02-11 06:05:29

场景树结构

首先,如果您希望两件事碰撞,则可能希望它们成为兄弟姐妹不是一个节点另一个孩子的孩子。这是因为空间 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” 信号,例如:

      func _ready() - >空白:
        连接(“ body_entered”,self,“ _on_area_body_entered”)
    
    func _on_area_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 because Spatials are placed relative to their parent. Said another way: when an Spatial 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 a RigidBody and an Area, the Area will detect the RigidBody, and for that it must have monitoring to be true 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 the Area to have bits in common with the collision_layer of the RigidBody. 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 the Area. 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 any Node on the same scene that has a script attached beforehand. I suggest to connect it to the Area 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:

    func _ready() -> void:
        connect("body_entered", self, "_on_Area_body_entered")
    
    func _on_Area_body_entered(body:Node) -> void:
        pass
    

    This code is intended to work in a script attached to the Area itself. It is also possible to call connect from a Node to another. Please note that we are passing the name of the method we want to connect as parameter to the connect 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 the Area.

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