Triggering different sounds for "footsteps" on different surfaces for a worm-like character.

Howdy! I’d like to state first that I’m an audio guy (sound design, music) that has, of this year, gotten really into wanting to learn how to do audio for games and to implement them properly into said games.

I’ve been digging through the forums here and elsewhere online to find different ways of triggering different sounds for when the player is on different surfaces e.g. stone, wood, grass, etc. Though with my situation, I’ve been going through the Tornado-Twins worm-game tutorial (some of you may be familiar with it, and again I’m quite new hence me doing said tutorials) and I’ve finished all the videos in the tutorial and wanting to get very detailed sound use in it.
So my case I have a worm character scooting around the ground, got some water in the level with sound triggers going on and recently created a script that plays a sound when I move my character using WASD or the Arrow Keys and that mutes it when I’m not, which I feel like there is a better way in general for getting sound playing instead of attaching it to the character. Here is the simple script:

var AudioFile : AudioClip;
 
    function Update()
    {
        if ( Input.GetButtonDown( "Horizontal" ) || Input.GetButtonDown( "Vertical" ) )
        audio.Play();
        else if ( !Input.GetButton( "Horizontal" ) && !Input.GetButton( "Vertical" ) && audio.isPlaying )
        audio.Stop(); // or Pause()
    }

Any suggestions would be greatly appreciated! Also, on a similar note, I could definitely learn/know more on scripting in regards to audio (and in general), does anyone know of a good source to check out to learn more about scripting (books, videos, web)? I’m not expecting to be a guru in a week I just wish to be less ignorant about game development and especially on the audio end that I’m interested in.

Thank you very much in advance for your time reading this novel of a post and for any and all help!

There’s not really a “right” way to do this but some ways are more robust than others. The most robust way would be to check what your worm is colliding with (water, stone, gravel, etc…) by doing a collision check, getting the material name of the game object and then using that as an index to retrieve the proper audio for that material.

A far easier way is to set up collision areas and play whatever the appropriate sound for that area is when you move. This would be the next logical step based on what you’re currently doing. Again, its not the “right” way of doing it but you can get a reasonable outcome and its a lot less intensive than building a material to audio system that could cost you 1000 lines of code and a lot of trial and error.

So set up a big box collider that covers your water and if your character collider is touching the box collider play the water sound, otherwise play the dirt sound or whatever is appropriate as a default.

Also you could experiment with audio.PlayOneShot() and just check your movement when audio isn’t playing. this would keep your sound from firing 100 times per second.

I’m not exactly sure if this would work but it might be possible to have different sounds for object with different tags. For example you may be able to tag a dirt floor with a dirt-tag and play a specific sound upon contact with an object with the tag dirt.