Footsteps Sounds in Unity3D C# code example

This was my attempt to implement footsteps sounds in Unity 3D which I did use to test my sounds (it was used, inter alia, here /footsteps-in-unity3d/) some time ago. If you need some easy example, then it may help you. Or else. It is written in C# in Unity3D 4.x but it is still works with Unity 5.x. It uses Resources.Load which maybe isn’t the most efficient way of loading sounds to the memory but it is an easy one. It works with default First Person Controller. The script supports switching footsteps type based on splatmap detection and object collision.

To contains two scripts footstepsSound.cs and footstepsSoundClass.cs.  In order to work you need to creat it into your project and attach footstepsSound.cs script to First Person Controller.

Between lines 34 and 47 in footstepsSound.cs script you have to enter paths to your own directories where you have your sounds. Single file is for single step so you need to cut it appropriately.

The tag parameter is used to associate particular footsteps type with objects tag. It means you need to add the same tag here and to the object if you want to detect it’s collision. Another parameter tells the script whether the sound corresponds with objects or textures on splatmap. “-1” is for objects, and the numbers (starting with 0) are for splatmap texture index. If you are on that texture this sound will be played then.

You can decide if you use AudioSource or PlayOneshot to play sounds. The latter one allows the sound to overlap. Not the only way to achieve that but the easiest one (for me).

Variables for you:

  • foostepsAAOn – if you are on splatmap and go from one texture to another it will play two sounds at once: current one and the last texture one with half of the volume. It is not applied to the objects, just splatmap textures.
  • footstepsDelay – an integer value of delay between succeding footsteps. It is not time value so you need to experiment which suits you best. The script detects the speed of movement thanks to controller.velocity.magnitude so if you run footsteps play faster.


and footstepsSoundClass.cs

or get it from GitHub:

Leave a Reply