0 votes

I feel like this should be really simple but I'm struggling to find out how to do it;

I have a player (KinematicBody2D with a collision shape 2D) and an enemy (with the exact same child nodes), and in the player script I want to check whether the two collide so I can damage the player. What is the easiest way to do this?

in Engine by (53 points)

1 Answer

+1 vote

I think the piece of the puzzle you might be reaching for is that moveandcollide actually returns a KinematicCollision2D object which contains the object it collided with

for example:

  var kb2d = self.move_and_collide(velocity*delta)
  if (kb2d):
        print("collided with" + str(kb2d.collider_id))

for more detail check out the page at http://docs.godotengine.org/en/3.0/classes/class_kinematiccollision2d.html#class-kinematiccollision2d

by (114 points)

There is also a section in "Tutorials" all about using kinematic bodies:
http://docs.godotengine.org/en/latest/tutorials/physics/using_kinematic_body_2d.html

For whatever reason changing my moveandslide to moveandcollide is really not working for me. Is there any way to check collisions while still using moveandslide?

You can use either. move_and_collide() returns a KinematicCollision2D. If you're using move_and_slide() you need to use get_slide_count() and get_slide_collision(), which also returns a KinematicCollision2D.

Okay I'm using that but it's only returning my player colliding with the ground and not when it collides with the enemy.

Nevermind, working now. But how do I check what object it is the KinematicCollision2D is related to? At the moment it's just returning a number.

Nevermind again hahaha. Got it all working. Cheers for the help

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.