0 votes

Hi,
My player character (kinematic body 2D) has a weapon as a child with its own hitbox. When a enemy (also kinematic body 2D) enters the weapon hitbox it passes a signal to the player script to trigger functions in the enemy to take damage like this:

func onWeaponHitboxbodyentered(body):
enemy = body
enemy.takedamage(weapon_damage)
enemy = null

I would like to play some hit effect animation at the exact point of contact between the weapon and the enemy when they collide. How do I get these coordinates?

Godot version 3.2.3
in Engine by (22 points)

1 Answer

+1 vote
Best answer

Hi! Because the enemy is a KinematicBody2D, it can be made to return a KinematicCollision2D object upon being hit.

An easy way to achieve the effect you are looking for is to say something likeVector2 position = get_slide_collision(0).position in the script of the enemy object. The object we called position will then hold the coordinates of collision.

get_slide_collision(int slide_idx) is a method in the KinematicBody2D class that returns a KinematicCollision2D object. KinematicCollision2D objects have a position property which contains the exact coordinates of the collision.

This code depends on you using move and slide for your movement. Please let me know if this helps!

More information can be found in the Godot API (we love the Godot API) for the KinematicBody2D class and for the KinematicCollision2D class

by (155 points)
selected by

Hi, thanks for answering,
does this require the enemy object to be looking for collision with the weapon? I have set things up to have as few collision checks as possible. Thus, I have the activated weapon looking for collision with enemies, which is 1 object checking when attacking. The enemy does not know that it collided with anything. If I did it the other way around it would be many enemy objects checking constantly for collision with a weapon.

Happy to help. This code would require the enemy to be checking for collisions from the weapon. If the weapon is also a KinematicBody2D, what I described before can be done with the weapon, rather than the enemy, which would not require the enemy to check for weapon collisions.

I sadly cannot think of a way to receive the exact coordinates of collisions without using the previously mentioned methods and hence a KinematicBody2D, so I believe your code will have to work around that condition. All this being said, enemies checking for weapon collisions is fairly typical and should have no significant performance effects, though I understand if you have hordes of enemies and are trying to keep things as clean as possible.

yes its probably not that big of a deal for enemies to check, collision checks with body entered and signals in Godot seems quite efficient. This does explain why some other projects I looked at seem to prioritize convenience over minimizing collision checks, so that is probably the way to go for me as well. Thank you very much for your answers! :)

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.