Colliding enemy and character

Godot Version

4.4.1

Question

Can’t make my collision to work. More specifically, my player does not receive damage.

My character has a collision shape 2d which is set to the 2nd layer.
On my enemy I have added another collision shape and a small script to check if the colliding object has the proper collision layer.

extends CharacterBody2D
@onready var player: CharacterBody2D = $"../Player"

func _on_body_entered(body):
	if body.get_collision_layer() == 2:
		player.take_damage()

However, when I start the build my character passes through the enemy, which is fine, but the take_damage script is not working properly.

Either the collision is working not as I have planned or the custom damage fucntion is a total crap.

func take_damage():
	if health > 0:
		health -=1
		#damage animation can be placed here
		update_heart_display()

func update_heart_display():
	for i in range(hearts_list.size()):
		hearts_list[i].visible = i < health

Please let me know if I can share any other specific information which might be related to my problem.
Thank you in advance.

The code seems fine (as long as health is greater than 0, else it would explain why the function is not triggered as you check if health > 0, but I suppose it’s not the issue here).

You’re checking if body.get_collision_layer() == 2, however, physics layer are made to avoid collisions between layers, so that you don’t have to check it manually in code later on; you just let the physics engine do its magic, and in your code, you then know that if _on_body_entered is called, that body must be in a valid layer.

Add some prints into your functions to know what’s happening: if a print in _on_body_entered is called, that means the collision is working but the code inside the function is not. If not, that means your physics layers are not working as intended.
I guessed you know that already, but here’s a link to the documentation about physics :smile:

1 Like

Thank you! I have completely forgotten about this debugging tip with printing something.

Indeed the problem is that my enemy and character are not colliding properly. They are both on the second layer. Alas _on_body_entered is not working.
My enemy collision has a mask applied for the second layer. The funny part is that I have a pick-up coin, which works fine. Lol

Enemy:

If you have a working pickup coin, you should compare your coin scene with the enemy scene. Any difference you spot may be an explanation of what’s going on.

Also, don’t forget to make sure everything is set up properly: an Area2D node, with a CollisionShape2D node, with a proper Shape, etc.

1 Like

I figured out hat was the problem. Lol
Can’t get used to signals. So I am just copying the code and not properly attaching the signal itself.
I have deleted and created an enemy several times and at the very end, I decided to re-watch the tutorial, where the author used the signal functionlaity.
My bad.

Anyway, thank you so much for taking your time and helping. Have a lovely rest of the day.

1 Like

If you prefer to connect signals from code this is possible if you do it in the ready function.
Connecting Signals Via Code

1 Like

thank you! will give it a go