Collision only detected in a certain part of CollisionShape2D

Godot Version

v4.4.1 stable

Why is the collision detected everywhere in the projectile script but not on the Enemy?

I’m trying to detect a collision by group, and delete the enemy from the screen.
I have a CharacterBody2D with a CollisionShape2D, which represents the enemy and the projectile is also a CharacterBody2D with a ColorRect.
The problem is that the collision is only detected when the projectile hits very specific spots on the far right or far left side of the enemy.



IT DOES HOWEVER, remove the projectile after colliding with any part of it (projectile.gd):

extends CharacterBody2D

var PROJECTILE_SPEED = 600

func _physics_process(delta):
	var motion = Vector2(0, -PROJECTILE_SPEED) * delta
	var collision = move_and_collide(motion)
	if collision:
		queue_free()

THE PROBLEM

extends CharacterBody2D
@onready var raycast_right: RayCast2D = $RaycastRight
@onready var raycast_left: RayCast2D = $RaycastLeft

const SPEED = 200.0
var direction = 1

func _ready():
	var screen_size = get_viewport_rect().size
	
func _physics_process(delta):
	var motion = Vector2(direction * SPEED, 0) * delta
	var collision = move_and_collide(motion)
	
	if collision and collision.get_collider().is_in_group("Projectiles"):
		print("Collision")
		queue_free()
	
	if raycast_right.is_colliding():
		direction = -1  
	if raycast_left.is_colliding():
		direction = +1

I’m a noob, if something else is needed I’ll update it.

Your projectile deletes itself on collision and your enemy deletes itself on collision too, but which order does this happen? Since you cannot guarantee the enemy will move first in a frame then you cannot be sure the projectile will work.

Maybe it would be better to use an Area2D for the projectile, which can use it’s body_entered signal to detect the enemy and act appropriately deleting both, rather than having the two scripts in charge of deleting themselves and hoping it happens in the right order.

2 Likes