How to retrieve name of colliding physics body

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

I have two KinematicBody2D objects (a player and an enemy), and I want the player to detect if the colliding object is the enemy or a wall. I have seen a solution with areas, but they don’t collide with the wall.

:bust_in_silhouette: Reply From: markopolo

Kinematic bodies are probably not the way to go here - as I understand it, their whole deal is being non-collided.

I would change the type of the enemy to RigidBody2D - if you don’t want it to rotate, you can change the mode to “character”

Once you’ve got a rigid body, you can enable contact reporting (set Contacts Reported to something greater than 0 and check the Contact Monitoring box) and then hook up the body_entered signal as pokumofu described. The enemy can check to see if it’s collided with a player and let it know: something like

# in enemy script
func _on_body_entered(body):
  if body.get_name() == "player":

Then in the player:

# in player script
func hit_by_enemy():
  # do whatever
:bust_in_silhouette: Reply From: treetertot

move_and_collide returns the colliding object. To retrieve the name, just use