How to make a node delete itself once collided with a KinematicBody2D (Player)

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

I’m trying to make some code that once the player has collided with another KinematicBody2D, which has a Sprite and a CollisionShape2D as its child, It will make itself hidden or delete itself. Am I on the right track with this code?

extends KinematicBody2D

func _ready():
	set_fixed_process(true)
	
func _fixed_process(delta):
	if is_colliding():
		self.hide()
:bust_in_silhouette: Reply From: eons

First, only a moving kinematic body (with move) will collide, the non moving will never detect because kinematics normally never detect collisions.

So, in the player code you may have:

func _fixed_process(delta):
  some_code
  more_code

  move(motion)

  if is_colliding():   #only collides after move
     var collider = get_collider()

     if collider.is_in_group("bodies_that_die_when_player_touch_them"):
        collider.queue_free() 

Later you may want some processing before deleting instead of removing it from the player code (is better to let an instance to take care of its own death), like remove collisions, play sounds, change sprites, animations, fire particles and make that node queue_free itself after finishing all that fancy dying process.

Every body in the group that can be killed by the player should have a common function to call and do all that (like die, hurt, etc.).