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.