0 votes

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.

in Engine by (30 points)

2 Answers

+1 vote
Best answer

moveandcollide returns the colliding object. To retrieve the name, just use .collider.name.

by (30 points)
+1 vote

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
by (629 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.