![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Voxybuns |
Hello!
EDIT: I found the answer, I need to get the difference between the velocity pre-collision and post-collision, as explained here
I’m looking to play a sound effect when a RigidBody3D is hit by/hits another object. I’d also like the volume of the sound to be controlled by the collision’s force (i.e., a weak collision plays the sound quietly, but a strong one plays it louder)
I’ve figured that I could try to calculate the collision force based on the linear velocity of the two objects on collision, and use that to control the audio player’s volume property, like so:
func _on_body_entered(body):
# Get the forces based on the colliding objects' velocities
var forces
if body == RigidBody3D:
forces = self.linear_velocity.length() + body.linear_velocity.length()
else:
forces = self.linear_velocity.length()
# Remap the forces variable to the appropriate volume range (this is a handy function I found online)
var audio_volume = remap_range(forces, 1, 6, -50, 5)
# Set the AudioStreamPlayer's volume accordingly
$stream.volume_db = clamp(audio_volume, -50, 5)
$stream.play()
However, I’m pretty sure there’s a much, much better and more reliable way of doing what I want to achieve… How could I improve this?