1
\$\begingroup\$

I have a game where I am dropping blocks onto a wooden platform. The idea of the game is to stack them whilst keeping them balanced.

The blocks fall from a height. Sometimes they land on the plank, other times on other blocks. Players will opt to stack them close together.

I want to make a noise when the block hits something, so I have the following code:

void OnCollisionEnter(Collision other)
{
    Debug.Log("Cube collided");
    Debug.Log(other.collider.GetComponent<Collider>().tag);
    GetComponent<AudioSource>().Play();
}

This is attached to my block.

The sound plays, however it feels extremely unrealistic. The block makes the same noise no matter what the velocity of the impact. If a block becomes jammed between other blocks then it makes a few sounds, all of the same sound it makes when hitting the plank from a height.

Is there a way of creating more realistic 3D sounds? I guess I'm after a noise and falloff based off of the velocity, but that might not be the case, I'm a little unsure. This seems like a very common usecase but I am struggling to find anything online about it.

\$\endgroup\$
2
\$\begingroup\$
AudioClip audioClip;
AudioSource audioSource;

void Start () {
    audioClip = Resources.Load("click") as AudioClip;
    audioSource = gameObject.AddComponent<AudioSource>();
}

private void OnCollisionEnter(Collision collision)
{
    float audioLevel = collision.relativeVelocity.magnitude / 10.0f;
    audioSource.PlayOneShot(audioClip, audioLevel);
}

The above code is from another project I have worked on, so I know it works. You'll probably have to modify it or your existing code to match your scenario.

\$\endgroup\$
4
\$\begingroup\$

Unity provides you with Collision.relativeVelocity

https://docs.unity3d.com/ScriptReference/Collision-relativeVelocity.html

The basic example inside the documentation already solves one of your problems: Don't play a sound, when the collision occured with a low velocity by using a threshold. This prevents small unwanted turbulence inside the physics engine or insignificant vibrations to cause a storm of sounds to be played.

You can also use the relative velocity to play the sound at different volume levels. A "soft landing" is going to be quiet and a block falling at high speed will be loud. Don't forget to cap the volume to prevent unpleasant loud sounds. You can take it a step further, by playing different sounds for different impact strengths.

Use multiple impact sounds and pitch variations to create variety. If the same sound is being repeated often, it can quickly become annoying.

Use different sounds when two different materials collide, if your objects are made out of different materials.

Edit: Another obvious pitfall, you can easily fall into is described in DMGregorys comment below.

\$\endgroup\$
  • 4
    \$\begingroup\$ One other trick I've used is to have the collision sound script inspect the object it collided with. If the other object also has the collision sound script, I pick just one of them to play the sound, so I don't get muffled overlapping audio when two of the same object hit each other. ;) \$\endgroup\$ – DMGregory Jan 6 '18 at 4:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.