如何修复 Godot 中的所有 area2D 函数,使其不在启动时运行?
好吧,我已经使用 Godot 一段时间了,并没有遇到任何问题。但是,在我添加了区域 2D 来检测玩家并传送它们(如下所示)后,我遇到了问题。每次我启动游戏时,控制台都会显示这两个函数已经运行,即使起始位置离area2Ds很远。另外,因为它们运行在输入->退出命令,它会在隧道的出口处生成我,而不是在地图的开头。
func _on_Tunnel_body_entered(_body):
print("entered_tunnel")
global_position.x = 952.5
global_position.y = 487
func _on_TunnelBack_body_entered(_body):
print("exited_tunnel")
global_position.x = 920
global_position.y = 635
任何帮助将不胜感激!
Ok, I've been using Godot for a while now, and haven't really had any issues. But, after I added an area2D to detect the player and teleport them as shown below that I run into issues. Every time I start the game, the console shows that both of the functions have already been run, even though the starting location is nowhere near the area2Ds. In addition, because they run in enter -> exit order, it spawns me at the exit of the tunnel, instead of the start of the map.
func _on_Tunnel_body_entered(_body):
print("entered_tunnel")
global_position.x = 952.5
global_position.y = 487
func _on_TunnelBack_body_entered(_body):
print("exited_tunnel")
global_position.x = 920
global_position.y = 635
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否偶然实例化了玩家角色,将其添加到场景中,然后设置其位置(按顺序)?
这是此问题的常见原因。发生的情况是,一旦您将其添加到场景中但在设置其位置之前,它就会与区域发生碰撞。
要解决此问题,请在将其添加到场景之前设置全局位置。
您可以通过以下任何方式暂时禁用该行为:
set_collision_layer_bit
和set_collision_mask_bit
来实现)。disabled
设置为true
。但是,使用set_deferred
以避免在 Godot 仍在进行物理计算时禁用它add_collision_exception_with
和remove_collision_exception_with
)。Are you, by chance, instancing the player character, adding it to the scene, and then setting its position (in that order)?
That is a common reason for this problem. What happens is that it collides with the areas once you add it to the scene but before you set its position.
To solve it set the global position before adding it to the scene.
You could temporarily disable the behavior by any of these means:
set_collision_layer_bit
andset_collision_mask_bit
).disabled
totrue
. However, useset_deferred
to avoid disabling it while Godot is still doing physics computations)add_collision_exception_with
andremove_collision_exception_with
).