Check collisions in a group with a raycast2D

Godot Version

` 4.4.stable

Question

` Thank you in advance for your attention to my problem.
I was following Brackey’s video “How to make a Video Game - Godot Beginner Tutorial”, and it made me want to continue learning about Godot.

I tried to ensure that my enemies (different instances of the Slime scene) wouldn’t collide with each other and turn around when they crossed paths with one of their peers by adding my “Slime” scene (Node2D) to a global “slime” group and using the “is_in_group” method.
However, if collisions with the TileMap are always detected by the various Raycast2Ds (right and left), it would seem that the “is_in_group” method simply doesn’t work, because when I use the print method to display the collisions detected, it only shows me objects relating to the TileMap.

extends Node2D

const SPEED = 60

var direction = 1
@onready var raycast_right: RayCast2D = $RaycastRight
@onready var raycast_left: RayCast2D = $RaycastLeft
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D



	
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float):
	if raycast_right.is_colliding():
		var collider_right = raycast_right.get_collider()
		if is_instance_valid(collider_right):
			if collider_right is TileMap or collider_right.is_in_group("slimes"):
				direction = -1
				animated_sprite.flip_h = true
				print(collider_right)
	elif raycast_left.is_colliding():
		var collider_left = raycast_left.get_collider()
		if is_instance_valid(collider_left):
			if collider_left is TileMap or collider_left.is_in_group("slimes"):
				direction = 1
				animated_sprite.flip_h = false
				print(collider_left)
	position.x += direction * SPEED * delta

I’m aware that my code won’t be perfect and may even be highly debatable, which is why I’m relying on you to understand my mistakes and learn how to use Godot better. I apologize in advance if my message is not clear, as English is not my native language.

`

The collider object that the raycast will return in get_collider() will be the physics object of your slime (like a CharacterBody2D for example), not the Node2D where you added the group. You’ll need to add the group to that node.

Also, make sure to use the same value for the group and when you check it in is_in_group() in your example you added the group slime but you are checking for slimes.

1 Like

Thank you for your prompt reply!

I followed your advice and added an AnimatableBody2D node (“slimes” group in my Slime scene) and attached a CollisionShape2D, since until then the Slime scene was only an AnimatedSprite2D and therefore could not be detected by the get_collider() function. The Godot console does indeed display collisions between Raycast2Ds and AnimatableBody2Ds.

However, the slimes don’t turn back. I also noticed that the is_in_group() function has no autocompletion, and doesn’t display documentation like other methods such as get_collider() when the cursor is hovered over it.