Enemy detecting player

Godot Version

4.3

Question

I am extremely new to godot, so be nice please…
I have set up a scene called kill zone, Here is the script:

extends Area2D
@onready var timer = $Timer
@onready var player = $“…/Player”

func _on_body_entered(body):
if body == player:
get_tree().reload_current_scene()

This works for objects within the same scene, but I have created an enemy that is its own scene and it cant detect the player node…
Any advice?

this body_entered signal give the body that entered, so for this encounter the kill zone can use body to deduce if it is a player through other means than explicitly setting the player’s path.

extends Area2D

func _on_body_entered(body):
    if body.name == "Player":
        get_tree().reload_current_scene()

Using this maybe you can get your enemy to work just as well? You can also use @export and groups to detect certain nodes like your player.

I only know this because I just followed the tutorial a few days ago, but this reads like you are doing Brackey’s Godot Beginner Tutorial.

If that is the case, he solves this problem by having the player node set to collision layer 2, then having the killzone mask set to 2, meaning it only checks for collisions on layer 2.

This allows your killzone to find your player when it enters, without triggering on something else entering such a slime or platform, since they are default on layer 1.

Whether or not you are following said tutorial, you may be able to solve your problem this way.

Otherwise, I assume your issue here is a referencing issue caused by your reference pathing to $Player, though I’m not versed in GDScript enough yet to say that definitively or explain why.

I would follow @gertkeno approach. Just make sure that enemy extends Area2D also, and compare maybe the group of the body rather than the body itself. This should work. If not, look at collision layers or give us some more info xD

1 Like