How to make a sound play when RigidBody2D hits something

Godot Version

4.5.1

Question

hey guys! so i have a RigidBody2D

i want it so that when it touches something, it plays a sound

now, it works, but it just keeps repeating the sound until its not touching that thing anymore

here is my code

func _physics_process(delta):
	if not get_colliding_bodies().is_empty():
		var body_velocity: Vector2
		
		var velocity_difference = linear_velocity - body_velocity
		
		if velocity_difference.length() > UNALIVE_IMPACT:
			$AnimatedSprite2D.play("corpse")
			$HurtSFX.play()
		

also, UNALIVE_IMPACT is 0 if that helps

Use body_entered signal.

Body velocity is undefined so if the linear velocity is greater then zero it will be playing the sfx

thank you so much!!!

1 Like