Skip to content
Steffenvy edited this page Apr 17, 2020 · 50 revisions

You should look at the examples, especially about the CollisionSounds script. Because a lot of the default script values are arbitrary, but the proper values are assigned to the prefabs, depending on what sounds good, but that means that the values aren't magic numbers either, they're just the numbers that they were left at after solving the audio problems that came up.

There is one major thing you should keep in mind, the asset is designed to have the most control possible, which means there can be:

Many variables, including many multipliers. You can and should ignore them unless you want to control them.

Quite a complicated sampling system code-wise (But I've given some powerful scripts that do it for you). For example, this is the simplest way to play a sound:

var outputs = soundSet.data.GetRaycastSurfaceTypes(pos, dir);
outputs.Downshift(); //albeit this function doesn't affect the 1st output, so it's not necessary for this
soundSet.PlayOneShot(outputs[0], audioSource);

(For "pos" you can use transform.position. For dir you can use -transform.up, Vector3.down, or Physics.gravity)

And the reason it's so lengthy is because:

  • There are many different things you could do with the information, so it is modular.
  • I give the ability to get more than 1 output.
  • DownShift is used to smoothly cull the list to the maxCount (default is 1) and using minimumWeight (default is 0). Until you do this, it is not guaranteed to be maxCount or fewer.

If you need more control than the (following) scripts, you should start getting comfortable knowing the parameters you have control over though. This is the equivalent of the last line (soundSet.PlayOneShot(outputs[0], audioSource);):

var output = outputs[0];
float volume = output.volume * output.weight;
soundSet.surfaceTypeSounds[output.surfaceTypeID].PlayOneShot(audioSource, volume, output.pitch);

The aforementioned scripts/components are:

  • SurfaceEffectsBase - This is a basic class you can inherit from, and then you'd call the function Play(audioSources, pos, dir, impulse, speed) and it would take care of Sounds and Particles using a Raycast.

  • GunShooter is a good demonstration of implementing from SurfaceEffectsBase. The Play function returns the SurfaceOutputs it finds, and GunShooter uses that to relocate the AudioSources to the impact position. If you need your gunfire to be more rapid, you could cycle through a couple AudioSource sets, or perhaps you could use an AudioSource pool.

  • RaycastFeet / SpherecastFeet - For footstep sounds. You can pair it with a customized version of the script %Examples - Trash%/Example Scripts/SimpleFootsteps.cs for even easier use. The reason I say to customize the script SimpleFootsteps.cs, is because it's currently designed around a single, specific, setup, which might not be how your setup is. Alternatively you could use the script alone, and use animation events to trigger the PlayFootSound(footID) function, however that wouldn't modulate the volume/pitch depending on the speed: walking/running/crouching, so instead you could use a setup like the script: AnimatorEventFootsteps.cs

  • CollisionSounds - For impact and friction sounds/particles, as well as vibration sound.

And for testing/debugging purposes:

  • CastSoundTester - For Ray/SphereCast testing. You should just use the "Cast Sound Tester" prefab.

cast-sound-tester

  • ArraycastTester - For testing of weight blending using a line of raycasts. You should just use the "Arraycast Tester" prefab. This script is not performant, so you shouldn't leave Arraycast Testers just laying around. They do disable themselves on start though.

arraycast-tester

  • CastSoundPerformanceTester - Tests how long it takes to sample x number of times. Adding a marker to the object being tested should improve the speeds. You should use the "Performance Tester" prefab.