Hello!
I am presently trying to create a destructible box in a 2D platformer.
Up to this point, I have a projectile object which, when it hits a piece of terrain with is_on_wall (or is_on_floor, or is_on_ceiling) it destroys itself.
Now I want to have the "destructible box" object detect the projectile, and the recommended method for doing this seems to be to create an Area2D and use on_Area2D_body_entered.
However, what seems to be happening is that the projectile destroys itself before the body enters the Area2D (which makes sense). What surprises me is that I can't just extend the Area2D by a single pixel and have the detection register, and instead have to stretch it out by a whole 4 pixels, which means the projectile is pretty far away from the box when damage/destruction is registered.
I'm assuming the answer to this is that "is_on_wall isn't great" and I should be using something else to detect terrain blocks for my projectile. But since the built-in function seems very convenient so far, I was hoping there might be another solution that wouldn't require me to code custom detection. In particular, I was hoping I could detect the collision (rather than the "entered area") to trigger the damage/destruction of the box, but I haven't found anything like that.
Relevant code:
# PROJECTILE
func _ready():
projectileVelocity.x = PROJECTILE_VELOCITY * projectileDirection
$Sprite.play("default")
func _physics_process(delta):
if(stuckCheck == position.x):
queue_free()
stuckCheck = position.x
if(is_on_wall() || is_on_floor() || is_on_ceiling()):
queue_free()
projectileVelocity = move_and_slide(projectileVelocity, Vector2.UP)
# DESTRUCTIBLE BOX
func _on_Area2D_body_entered(body):
print("colliding") # Obvious placeholder is obvious.
(Apologies for the lack of tabbing, I don't quite understand why the code is formatting this way.)