当我的玩家在 Godot 4 或更高版本中击中地面时,如何使 TileMap/TileSet 发出碰撞信号

问题描述 投票:0回答:1

在 Godot 4 中,使用 C#,我很难获得碰撞信号。我使用 TileMap/TileSet 来创建地板,并添加了物理层组件。我的玩家不会从地板上掉下来,我的玩家会撞到地板并停止。很明显碰撞已经打开并且工作正常。

但是,在检查器中,当您单击“节点”选项卡时,您可以看到所选树中每个节点的信号。我选择了 Player (RigidBody2D) 节点,并且尝试在我的玩家与地板碰撞时调用函数 _onBodyEntered(Node body)。但无论如何,TileMap/TileSet 都不会发出这个信号。

我只想要一个解决方案来保证我的球员在场上就是我想要的。如果有更简单的方法来验证您的球员是否在场上,我不需要解决这个特定问题。但如果能够知道我的玩家何时与游戏其他部分的 TileMap 发生碰撞,那就太好了。

请不要告诉我阅读文档。我有。多次。还欠缺。

我已确保播放器位于第 1 层,而我的 TileMap/TileSet 位于第 2 层。我已将其两个 MASKS 设置为 1 和 2。

我多次仔细检查并重新制作了我的 TileSet,以确保我确实使用碰撞油漆物理层 0 正确创建了图块。但不知道为什么它被称为第 0 层。但我的瓷砖绝对开启了碰撞。

signals collision-detection godot4 sktilemapnode tilesets
1个回答
0
投票

玩家没有从地板上掉下来这一事实表明碰撞工作正常。

如果您想在 CharacterBody2D

检测碰撞
,则需要在
_physics_process
中执行此操作。具体来说,您需要在移动角色的代码中执行此操作(例如
move_and_slide
move_and_collide
)。 或者,如果您只想知道玩家是否在地面上,请使用
is_on_floor()
函数


这是

CharacterBody2D
的模板代码,假设您将
Motion Mode
设置为
Grounded
(默认)。

extends CharacterBody2D


const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
    # Add the gravity.
    if not is_on_floor():
        velocity.y += gravity * delta

    # Handle Jump.
    if Input.is_action_just_pressed("ui_accept") and is_on_floor():
        velocity.y = JUMP_VELOCITY

    # Get the input direction and handle the movement/deceleration.
    # As good practice, you should replace UI actions with custom gameplay actions.
    var direction = Input.get_axis("ui_left", "ui_right")
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)

    move_and_slide()

您可以通过多种方式修改最后一行 (

move_and_slide()
):

  1. 使用布尔结果(
    true
    ,如果发生碰撞)来确定是否发生碰撞。
  2. 切换到
    move_and_collide()
    并使用
    KinematicCollision2D
    捕获结果。用它来处理碰撞。
© www.soinside.com 2019 - 2024. All rights reserved.