- Edited
I'm learning Godot right now and decided to create simple 2d game to get understanding of the engine. I'm currently designing damage system (e.g. when bullet or any other projectile hit enemy/player, there should be hit event which would trigger taking damage, reducing health bla-bla-bla) and encountered one issue. I thought that the most logical way to design such system would be to check for collision on enemy/player side. I put the bullet collision check in _physics_process movement function of enemy (which is KinematicBody2D class):
var collision = move_and_collide(-v*speed*delta);
if collision != null:
if collision.collider.is_in_group('Projectile'):
hit();
I was surprised when found out that move_and_collide function doesn't return any object when bullet hits enemy! Though, when enemy collides with player, the same code (with a slight change to check for Player group) works fine.
I read some answer that KinematicBody2D may not detect collision if some other object hits it. This would suggest to put such collision check to the bullet code instead. However, from my point of view that would be a bad design since bullet should care only about events which has impact on it (like bullet hit any object -> play hit sound -> disappear).
As another workaround I could add additional Area2D with another collision to the Enemy node that would specifically check for collisions with projectiles. However, this seems to me as even worse idea since in this case I have to make Area2D collision bigger that Enemy's shape collision in order to catch it. And this may also have impact on performance since engine will calculate 2 collisions separately.
Is there any other way to make the trick and call bullet collision check in enemy's script? Or engine doesn't have any out of the box solutions?
Thank you in advance!