CharacterBody2D 和 StaticBody2D 的碰撞未触发

huangapple go评论53阅读模式
英文:

Collision of CharacterBody2D and StaticBody2D not triggering

问题

I'm trying to trigger a print when CharacterBody2D collides with StaticBody2D. I'm very new to Godot please bear with me

我想在CharacterBody2D与StaticBody2D发生碰撞时触发打印。我对Godot非常陌生,请谅解。

I got 3 scenes game_level, CharacterBody2D (player), StaticBody2D (item). In game_level, I instantiated child Player and Item

我有3个场景 game_levelCharacterBody2D (player)StaticBody2D (item)。在game_level中,我实例化了子级Player和Item。

I currently attached this script to the StaticBody2D (item):

我当前将这个脚本附加到了StaticBody2D(item)上:

extends StaticBody2D

func _ready():
    print("ready")
	

func _on_StaticBody2D_body_entered(body):
    print("Static body collided with: ", body.name)

I tried changing Collision Layer & Mask for both the CharacterBody2D and StaticBody2D as follows:

我尝试根据以下方式更改CharacterBody2D和StaticBody2D的碰撞层和掩码:

Just bumped but no prints
只是碰撞但没有打印

CharacterBody2D || StaticBody2D
Collision>Layer = 1 || Collision>Layer = 1
Collision>Mask = 1 || Collision>Mask = 1

Just passed through but no prints
只是穿过但没有打印

CharacterBody2D || StaticBody2D
Collision>Layer = 1 || Collision>Layer = 2
Collision>Mask = 1 || Collision>Mask = 2

Just bumped but no prints
只是碰撞但没有打印

CharacterBody2D || StaticBody2D
Collision>Layer = 1 || Collision>Layer = 2
Collision>Mask = 1 & 2 || Collision>Mask = 1 & 2

Godot Version: Godot版本:Godot_v4.0.1-stable_win64

英文:

I'm trying to trigger a print when CharacterBody2D collides with StaticBody2D. I'm very new to Godot please bear with me

I got 3 scenes game_level, CharacterBody2D (player), StaticBody2D (item). In game_level, I instantiated child Player and Item

I currently attached this script to the StaticBody2D (item):

extends StaticBody2D

func _ready():
	print("ready")
	

func _on_StaticBody2D_body_entered(body):
	print("Static body collided with: ", body.name)

I tried changing Collision Layer & Mask for both the CharacterBody2D and StaticBody2D as follows:

Just bumped but no prints

CharacterBody2D            || StaticBody2D
Collision>Layer = 1        || Collision>Layer = 1
Collision>Mask = 1         || Collision>Mask = 1

Just passed through but no prints

CharacterBody2D            || StaticBody2D
Collision>Layer = 1        || Collision>Layer = 2
Collision>Mask = 1         || Collision>Mask = 2

Just bumped but no prints

CharacterBody2D            || StaticBody2D
Collision>Layer = 1        || Collision>Layer = 2
Collision>Mask = 1 & 2     || Collision>Mask = 1 & 2

Godot Version: Godot_v4.0.1-stable_win64

答案1

得分: 1

在Godot中,Node具有信号,您可以从编辑器或代码中连接这些信号。您需要连接信号才能使它们起作用。

我知道您没有连接StaticBody2Dbody_entered信号。

创建一个与Godot生成的名称匹配的方法 - 在这种情况下是_on_StaticBody2D_body_entered - 不起作用。

您可以查看Godot是否识别到方法已连接信号,因为它会在代码编辑器中的方法声明左侧显示一个绿色图标。

请参考使用信号


我知道您没有连接StaticBody2Dbody_entered信号...因为StaticBody2D没有body_entered信号。

可以绕过这个问题。我在其他地方提到了一些方法,这里不再赘述。

话虽如此,我建议不使用StaticBody2D,而是使用Area2D

如果您需要它是StaticBody2D,那么我建议向StaticBody2D添加一个Area2D子节点,并使用它来检测碰撞(这是我在其他地方提到的选项之一)。


为了完整起见,我还要提到,您可以检测CharacterBody2D碰到了什么。就像这样:

for index in get_slide_collision_count():
    var collision := get_slide_collision(index)
    print(collision.get_collider())

这对于CharacterBody2D和不移动的StaticBody2D的情况应该足够了,因为如果StaticBody2D不移动,它不会碰到CharacterBody2D(换句话说,碰撞是由于CharacterBody2D的运动引起的)。

※:在Godot 4中执行移动平台的推荐方式是使用AnimatableBody2D,这是StaticBody2D

英文:

In Godot Nodes have signals, which you can connect form the editor or from code. You need to connect the signal for them to work.

I know you didn't connect the body_entered signal of your StaticBody2D.

Making a method with the name that matches the that Godot would generate - in this case _on_StaticBody2D_body_entered - does not work.

You can see if Godot recognized that a method has a signal connected because it shows a green icon on the left of the method declaration in the code editor.

Please refer to Using Signals


I know you didn't connect the body_entered signal of your StaticBody2D... Because StaticBody2D does not have a body_entered signal.

It is possible to work around that. I have outlined some approaches elsewhere. And I won't repeat them here.

With that said, my recommendation is to not use an StaticBody2D but an Area2D instead.

If you need it to be an StaticBody2D, then I suggest adding a child Area2D to the StaticBody2D and use that to detect the collisions (which is one of the options I mention elsewhere).


For completeness I'll also mention that you can detect what the CharacterBody2D ran into. Like this:

for index in get_slide_collision_count():
    var collision := get_slide_collision(index)
    prints(collision.get_collider())

Which should be sufficient for the case of a CharacterBody2D and a StaticBody2D that does not move※, because if the StaticBody2D does not move, it won't run into the CharacterBody2D (in other words the collision would be result of the motion of the CharacterBody2D).

※: The recommended way to do moving platforms in Godot 4 is with an AnimatableBody2D which are StaticBody2D.

huangapple
  • 本文由 发表于 2023年3月31日 18:46:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定