0 votes

I want to get the name of the body the player collided with, if the player collided with a body. How can I do this?

in Engine by (50 points)

1 Answer

0 votes
Best answer

Collision info is contained in a KinematicCollision object.

How you access this, depends on how you're moving your KinematicBody.

1) If using move_and_collide(), it's the returned value of the function:

var collision = move_and_collide(velocity * delta)
if collision:
    print(collision.collider.name)

2) If using move_and_slide(), you can get collision data with get_slide_collision(). However, you have to keep in mind that when using this method, it's possible to have multiple collisions in a single frame (when you move into a corner, for example):

velocity = move_and_slide(velocity, Vector3.UP)
for index in get_slide_count():
    var collision = get_slide_collision(index)
    print(collision.collider.name)

Please see the KinematicBody reference for details.

by (21,714 points)
selected by
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 Frequently asked questions and 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 [email protected] with your username.