how to detect a collision betwen a kinematickBody2D and a group of a statickBody2D

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

i have a kinematickBody2D node, and a staticBody2D, an the staticBody2D hace a grup call “cajaSorpresa”, so i want to make something when the kinematickBody2D collide whit the group “cajaSorpresa”, for the moment i just want to print something in the console to see that is working correctly. this is the code:

extends KinematicBody2D

export (int) var walk_speed = 200;
export (int) var jump_speed = -420;
export (int) var gravedad = 1200;

var velocidad = Vector2();
var jumping = false;

func get_input():
 #here is the funtion to move the character but idont think that is necesary
#i just use de Input.is_action_pressed and no more

 func _physics_process(delta):
    	#here there is a funtion to jump, no necesary put here to the question i think 
    	velocidad = move_and_slide(velocidad,Vector2(0,-1));

 #this its the funtion that i can't get it, the game doesn't show any 
#problem when is runing, but either do anything 
func _procesar_colision():
	var collision = move_and_collide(Vector2(0,0));
	if collision.is_in_group("cajaSorpresa"):
		var collidedObject = collision.get_collider()
		print("is colliding whit cajaSorpresa");
:bust_in_silhouette: Reply From: kidscancode

When you do this:

var collision = move_and_collide(Vector2(0,0))

The collision variable will be a KinematicCollision2D object, not the body that you collided with. This object contains info about the collision - more about this here: KinematicCollision2D — Godot Engine (3.2) documentation in English

Instead of checking if that is in the group (it isn’t), you need to check if the collider is in the group:

if collision.collider.is_in_group()