Having problems creating 'game over' on player collision.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ben2420

Hi, I’ve just started with Godot this week without much experience at all with coding.

I’m trying to create a simple Flappy Bird style game without using a tutorial to challenge myself with problem solving.

My problem:

I’m trying to get my player (KinematicBody2D with AnimatedSprite and CollisionShape2D) to die when colliding with my pipes (Sprite with StaticBody2D and CollisionPolygon2D) but I can’t seem to get it working.

This is my code I have for my user to try and implement this so far but it’s not working:

func _on_body_entered(body):
	if body.get_name() == "obstacle":
		get_tree().reload_current_scene()

Any help would be greatly appreciated, thanks!

:bust_in_silhouette: Reply From: djmick

Just add an area 2d to your bird with a collision shape slightly bigger than the actual collision shape so that it can collide, and then connect the body entered signal. Also, it seems that your obstacles are a sprite with a static body child? I would always use a static body with a sprite child. I may be wrong but it could be since your sprite seems to be the root, that you named it an obstacle, but the body that the player is colliding with is actually the static body, which could have a different name and wouldn’t trigger anything. I hope these help! If not, hopefully, I can help you better.

After a bit of experimenting it finally worked, thank you for the help and advice!

ben2420 | 2021-02-03 20:46

Of course! I didn’t know if I had worded the answer well at all so I’m glad it helped!

djmick | 2021-02-03 21:08