- Home /
How to make a Sound Effect based on the traveled distance ?
I wanna know what is the best way to make the sound of something falling in the ground based in the traveled distance of it.
Answer by lgarczyn · Dec 13, 2019 at 07:13 PM
You can calculate the force of an impact very easily:
float force = collision.impulse.magnitude / Time.fixedDeltaTime.
Just scale your sound intensity by that force, but remember to add a max value to avoid dangerous sound levels. You might want to take the square of the force, to avoid intensities becoming too strong. So:
float intensity = Mathf.Clamp(force * volumeMultiplier, 0f, volumeMax);
This needs to be marked as best answer.
It never occurred to me that Collision had it's own properties, which means Collision must return a dictionary similar to a Raycast. Thanks @ceandros!
Nope! Collision is a class, it's not a dictionary, and neither is a RaycastHit.
It just has "properties", just like a GameObject has a name property, and a Rigidbody has a velocity property.
I'm basing it off of how Godot handles intersect_ray(). It returns a dictionary and the object can be returned by using ray.collider so I figured it would be pretty much the same in Unity.
Answer by Magso · Dec 12, 2019 at 10:57 PM
This can be done using OnCollisionExit to start a timer and OnCollisionEnter to stop the timer and play the sound and/or set the sound volume accordingly.
bool falling;
float timer;
void OnCollisionExit(Collision other)
{
//if(other == ground)...
timer = 0f
falling = true;
}
void OnCollisionEnter(Collision other)
{
//if(other == ground)...
falling = false;
//set sound volume to timer and/or use if statements to pick a specific sound from an array.
}
void Update()
{
if(falling)
{
timer += Time.deltaTime;
}
}
Alternatively the distance could be calculated by a raycast, but it would have to be calculated initially and another raycast would have to be used to check for any changes that could happen between the initial falling and landing.
Your answer