Blocks change color and a sound is played when hit.
When I throw my RigidBody2D to the right without jumping, it registers one collision too many (3). If I do the same thing on the left, it registers 2 collisions, and this is what I want.
On the other hand, when I throw after a jump, I have 2 collisions on the right, and 3 on the left, so one too many !
It looks like your RigidBody2D might be registering multiple collisions due to overlapping shapes or rapid movement. Try implementing collision debouncing by adding a “has_collided” flag to prevent multiple detections.
for example
var has_collided = false
func _on_body_entered(body):
if not has_collided:
has_collided = true
if body.modulate == Color(1,1,1):
body.set_modulate(Color(1,0,0))
else:
body.set_modulate(Color(1,1,1))
$FlagCollisionSound.play()
func _on_body_exited(body):
has_collided = false
It works, thanks ! But it feels kind of “hacky”, I mean, it’s a simple collision between two rectangles at a relatively normal speed, I find it kind of weird that I have to resort to timers for such a simple thing… And I still don’t understand why the collision is “normal” on one side and not the other !
I could also store the velocity at each frame and check in _body_entered if the velocity is different than 0, and in that case play the sound. But again, and maybe it’s my inexperience with gamedev in general, I find it weird that I can’t just say something as simple as “play the sound when something hits the object”.
Yeah it does feels like using a timer is a bit “hacky” for a simple collision. The different behavior on each side could be due to how the physics engine processes collisions or slight differences in object positioning.
You could try checking the velocity before playing the sound, which might feel more intuitive:
if not has_collided and linear_velocity.length() > 0:
has_collided = true
$FlagCollisionSound.play()
Also, consider fine-tuning collision layers, masks, and the physics settings like continuous collision detection (“CCD”). Game development sometimes requires these workarounds due to the complexities of physics engines, but with experience, you’ll find approaches that feel more natural.