_on_body_entered not working?

Godot Version

4.3

Question

I’m following along with a tutorial (https://youtu.be/FNEAJsry5sA?si=PKszaXqG-kT2oUK9&t=4964) because I’m new to Godot and have come to a roadblock. Mine and his code look almost alike (i skipped some parts for they are less important for the time being) and in his code, when the character touches the end, he prints win, while mine prints once as the game loads and then not after any ideas why?
My code:
imageMy scene tree:
image
P.S. his Godot version was 4.0.2

If your tilemap has collision it is also a “body”. on load, the tilemap enters the area. The tutorial assumes only the player can enter the area, you could do a number of this to fix this.

I would recommend adding an if statement to ensure the game only moves to the next scene if the player enters the area. This could be done by comparing the colliding body’s name.

func _on_end_body_entered(body: Node2D) -> void:
    if body.name == "Player":
        print("win")
1 Like

After some testing, now when my game is ran, it knows that it has touched something but can’t tell if it’s a player. When moved off the ground, it prints nothing, but when touching the ground, it prints: “I touched something”.

The new code:
image

How are you testing if it touched the player? Did you change the node’s name of the player?

You could also print(body) to get information about anything that enters the area.


Make sure to paste code instead of screenshots

2 Likes

My code now looks like this (thanks for the tip btw)

func _on_end_body_entered(body) -> void:
	if body.name == "Player":
		print("win ", body)
	else:
		print("i touched something ", body)

I’ve been testing two things, one scenario where the end’s hitbox is touching the ground, and another where it is.
In scenario one, it prints this out.
image

Whereas in scenario 2 it prints nothing.
I have tested multiple movement methods to verify that I have touched the two hitboxes together, such as jumping into the flag.

I have also tried changing the if statement to check for “collision”, and changing the argument to “body: Node2D”

Here are the scene structures:
Main scene:
image

Player scene:
image

Tough to say, did you edit the collision mask/layer of your Player or End?

3 Likes

Yeah that worked, thanks for all the help!