Search Unity

Audio Variable Footstep Sounds (That depends on surface)

Discussion in 'Audio & Video' started by Consumedgrub2, Apr 27, 2021.

  1. Consumedgrub2


    Mar 6, 2021
    Does anyone know a good tutorial or script that shows me how I would go about implementing footstep sounds that are played from certain footstep sound groups in example if you walked on metal, you’d hear footstep sounds from the metal sound group??? (I would like to try and play these accordingly by detecting the material that the player is walking on.) Also I need to be able to do this without animation events since I am currently using a FP controller that isn’t animated.

    (BTW I am using unity 2020.3 LTS Just for a reference for people in the future who are looking at this forum post trying to figure this out.)
  2. DriesVienne


    Jun 22, 2013
    I don't have a tutorial.
    I was hesitant to reply, because this problem encompasses quite a lot of things.
    • Defining surfaces
    • Physics Raycasting
    • Terrain Splat maps
    • Playing Audio
    I cannot give a clear and concise answer.
    But since on one has replied so far, I'd figure I at least share some insight in how we do it:

    This is a simplified version of how we tackle the problem.
    We support both terrain collision, and gameobjects with a collider that have an added component 'SurfaceMaterialID'.

    The gist of it is as follows:

    - We define each surface as a 'SurfaceMaterial'
    - It contains an enum AND a terrain layer for identification

    - We keep knowledge of all these materials in an asset (scriptable object) called 'SurfaceMaterialData'
    - This asset is referenced by our player object

    - Each step we raycast down (Only to collision layer marked as 'Ground').
    - If we hit terrain, we match the definitions based on the terrainLayer with the greatest opacity
    - If we hit a collider, we check if it has a 'SurfaceMaterialID' component, and match it against that id.
    - Now that we have the SurfaceMaterial, we simply play one of it's files

    Our system has been extended to support multiple terrain layers,
    blending of footsteps based on the alpha of overlapping terrain layers,
    passing through foliage or water, shifting the panning and volume for each step, and all that jazz.
    Once you have the basics down, it's quite easy to extend.

    It's a bit fiddly getting the terrain splat maps, so I'm including some example code.
    I'd share more of the system, but it would take quite a lot of work to extract the complete model and simplifying it down for demonstration purposes.
    Please use this code as a guideline, not as an implementation.
    It is untested.


    Code (CSharp):
    1. [System.Serializable]
    2. public class SurfaceMaterial // Map a bunch of clips to both an identity (enum value) and a terrain layer
    3. {
    4.     public SurfaceIdentity identity;
    5.     public TerrainLayer terrainLayer;
    6.     public AudioClip[] clips;
    7. }
    Code (CSharp):
    1. public enum SurfaceIdentity { Grass, Mud, Sand, Tile, [...] } // Some ID's

    Code (CSharp):
    1. [CreateAssetMenu(fileName = "Audio/SurfaceMaterialData")]
    2. public class SurfaceMaterialData : ScriptableObject // Now we can reference our materials by 'Id' or terrain layer
    3. {
    4.     [SerializeField] private List<SurfaceMaterial> surfaceMaterials = new List<SurfaceMaterial>();
    6.     public SurfaceMaterial FindSurfaceMaterial(TerrainLayer layer) { [...] } //Iterate the list and compare layers
    8.     public SurfaceMaterial FindSurfaceMaterial(SurfaceMaterialId id) => FindSurfaceMaterial(id.identity);
    10.     public SurfaceMaterial FindSurfaceMaterial(SurfaceIdentity identity) { [...] } //Iterate the list and compare identities
    11. }

    Code (CSharp):
    1. [RequireComponent(typeof(SurfaceMaterialIdentifier))]
    2. public class FootstepPlayer : MonoBehaviour // I split up the player from the 'surface-identifying-raycast' thing. We have lots of other code here...
    3. {
    4.     protected void OnFootstepTaken(FootstepArgs args)
    5.     {
    6.         Ray r = new Ray(CurrentPosition + Vector3.up, Vector3.down);
    7.         SurfaceMaterial castResult = Identifier.Cast(r);
    9.         // You can do a lot of fun stuff with panning or volume here.
    11.         if (castResult != null)
    12.             Play(castResult.clips); // pick one at random
    13.     }
    14. }
    16. public class SurfaceMaterialIdentifier : MonoBehaviour // The meat and bone of the system
    17. {
    18.     private static readonly string layerToCastTo = "Ground";
    19.     private static readonly float maxCastDistance = 10f;
    21.     [SerializeField] private SurfaceMaterialData _surfaceData = null;
    23.     public SurfaceMaterial Cast(Ray r)
    24.     {
    25.         if (_surfaceData != null)
    26.         {
    27.             RaycastHit hitInfo;
    28.             if (Physics.Raycast(r, out hitInfo, maxCastDistance, LayerMask.GetMask(layerToCastTo), QueryTriggerInteraction.Ignore))
    29.             {          
    30.                 Terrain terrain = hitInfo.transform.GetComponent<Terrain>();
    31.                 if (terrain != null)
    32.                 {
    33.                     return CastTerrain(terrain, hitInfo.point);
    34.                 }
    36.                 SurfaceMaterialId id = hitInfo.transform.GetComponent<SurfaceMaterialId>();
    37.                 if (id != null)
    38.                 {
    39.                     SurfaceMaterial material = _surfaceData.FindSurfaceMaterial(id);
    40.                     if (material != null)
    41.                     {
    42.                         return material;
    43.                     }
    44.                 }
    45.             }
    46.         }
    47.         else
    48.         {
    49.             Debug.LogError("SurfaceData not assigned", this);
    50.         }
    51.         return null;
    52.     }
    54.     private SurfaceMaterial CastTerrain(Terrain terrain, Vector3 position)
    55.     {
    56.         TerrainData data = terrain.terrainData;
    58.         int terrainInstanceId = terrain.GetInstanceID();
    59.         float[,,] splatMapData = data.GetAlphamaps(0, 0, data.alphamapWidth, data.alphamapHeight); // In our dev code, I cache this result, not having to fetch it every step.
    60.         int splatTextureCount = splatMapData.Length / (data.alphamapWidth * data.alphamapHeight);
    62.         Vector3 localHit = terrain.transform.InverseTransformPoint(position);
    63.         Vector3 splatPosition = new Vector3(
    64.             (localHit.x / data.size.x) * data.alphamapWidth,
    65.             0,
    66.             (localHit.z / data.size.z) * data.alphamapHeight);
    68.         //Get the most opaque splat
    69.         float maxOpaque = 0f;
    70.         int index = -1;
    71.         for (int i = 0; i < splatTextureCount; i++)
    72.         {
    73.             float opacity = splatMapData[(int)splatPosition.z, (int)splatPosition.x, i];
    74.             if (opacity > maxOpaque)
    75.             {
    76.                 maxOpaque = opacity;
    77.                 index = i;
    78.             }
    79.         }
    81.         //Fetch
    82.         TerrainLayer layer = data.terrainLayers[index];
    83.         SurfaceMaterial surfaceMaterial = _surfaceData.FindSurfaceMaterial(layer);
    84.         return surfaceMaterial;
    85.     }
    86. }
    I figured some chopped up code examples are beter than no answer at all.
    Hope this helps you!

    (Some edits for clarity)
    Last edited: Apr 30, 2021
  3. PJEDavy


    Jul 4, 2020
    Amazing reply really. This is very useful. Thank you!
  4. angeldevelopment


    Sep 28, 2022

    Doesn't it make more sense to just check the collider tag of the collider? That way you avoid the overhead GetComponent<>() call? What if you have 100 enemies all running this code?
  5. spiney199


    Feb 11, 2021
    The example code looks to be oriented for a player character. So it's better for it to be flexible, rather than performant. 100+ NPCs is a completely different case to the one the code is trying to solve.

    Tags are super limiting design-wise and architecture wise, in any case. Better to avoid them unless absolutely necessary.

    You can definitely design games without them these days.
    halley likes this.
  6. halley


    Aug 26, 2013
    Instead of tags, I use the collider's material (PhysicsMaterial) reference as a key. Oh, you hit a collider with SandMaterial, let's play the associated sound. You can also get fancy and differentiate sounds with force or incident angle, or choose a sound style based on which of the two materials in a collision is more important, so you can support barefoot/sandal/boot vs metal/water/sand/wood.
    Ryiah and spiney199 like this.
  7. angeldevelopment


    Sep 28, 2022
    Is the physics material cached with the collider? This seems like a good solution, have you used it with many NPCs before?
  8. spiney199


    Feb 11, 2021
    Every collider has a physics material property:

    Only issue I can see is potentially having to null-check it.
  9. halley


    Aug 26, 2013
    Yup, null is just another valid key in the "choose a footstep" manager.

    As for performance testing, and this goes for everything you set up, put together a test scene that pushes the feature 10x, 100x, 1000x more than you ever expect to use it. Focus on the subsystem in some tests, and the whole integrated game in other tests. I don't make games with 1000 NPCs, but I can still set up a test that has a bunch of NPCs or a bunch of footstep calls and profile it to look for improvements.
    Ryiah and spiney199 like this.