How to check if the collision on an enemy was in the head or body

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By michelveloso
:warning: Old Version Published before Godot 3 was released.

Hi,

I have a enemy that extends KinematicBody2D as a node. So I added two CollisionShape2D, one in the head , other in the body.

Is it possible to detect if the collision was on the body or on the head ?
Is it possible to check the inside _fixed_process function?
Is it possible to check the collsion inside Enemy’s _fixed_proces function, instead player’s one?

#Player's KinematicBody2D 

func _fixed_process(delta):

  if is_colliding():    		
    print(get_collider())
    pass
:bust_in_silhouette: Reply From: eons

Sadly there is no direct method to get the own shape that collided on KinematicBody2D, collider refers to the other body that was hit when using move.

In this case, if the player hits enemy, the player can get_collider_shape of the enemy but the enemy can only get_collision_pos and try to guess where it was (or use Physics2DDirectSpaceState and test the collision point).

@eons,

I get it done by spliting the body and head in separated areas.
Than connect the function in enemy’s script.

It works! =D


enter image description here

michelveloso | 2017-06-12 14:59

if the area is bigger, it should work, also you can use KinematicBodies as sub-bodies but these will be moved as static bodies by the main body (ignoring overlaps), if enemy head works as a platform, it can be used this way without affecting the main body move (the head will ignore contact too).

eons | 2017-06-12 15:59

good to know @eons, thanks!!

michelveloso | 2017-06-12 16:19