Two KinematicBody2D collision problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hajmon7

hello
Im developing my first 2D game.
I have a player which is KinematicBody2D with CollisionPolygon2D surrounding the sprite
and i have bullet fired by the bad guy. the bullt is also KinematicBody2D surrounded by CapsuleShape2D.
The problem is when bullet hits player from left or right it is ok - move_and_collide method inside bullet script returns collision with player,
but when player jumps and fall off on the bullet this method don’t return collision and player is “standing” on moving bullet.
Cause I’m new in godot I don’t know if

  • Should I somehow detect this situation in player script ?
  • Should I make bullets to be other type of nodes like RigidBody2D ?

i see that method is_on_floor returns true when player is standing on flying bullet object
but what to do with it?

thanks in advance for any suggestions

:bust_in_silhouette: Reply From: kidscancode

KinematicBody2D only detects collision when moving with move_and_slide() or move_and_collide(). This means that if the player is standing still, it won’t “know” that the bullet hit it, and vice versa.

To deal with two kinematic bodies colliding, you have to do the collision checks on both of them. When the player moves, check if it hit a bullet, and when the bullet moves, check if it hit the player.

Another solution is to use Area2D for bullets. If you only need to check for contact, this simplifies things because you can use the area’s body_entered signal to tell you that the bullet has contacted something.