Adding footstep sounds to player.

Updated on December 24, 2014 in [A] Audio
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
14 on December 13, 2014

HI I need help on adding footstep sounds to my player when he walks. I already have the sounds of the footsteps I just need to know how to add them to the player and only activate them when the player is walking.

  • Liked by
Reply
0 on December 13, 2014

If you want them to come from the ground up to you (so you’re hearing it from the ground) you attach a 3D sound to the audio component onthe player, if you want it to be a constant sound (so it comes from all around you) you use a 2D Sound instead.
Whether or not the sound is 2D or 3D is decided by the sound properties itself I believe.
Then you would make it play in code when you are walking:


gameObject.audioSource.Play();


For your reference, have a look at this:
http://docs.unity3d.com/ScriptReference/AudioSource.html
http://docs.unity3d.com/ScriptReference/AudioSource.Play.html

Pay special note to the example used, as it requires an AudioListener component on the gameObject

  • Liked by
Reply
Cancel
10 on December 14, 2014

tell me, do you want dynamic sound? (sound changing depending of the surface the player walks) or just the same sounds over and over again?? tell me that and i’ll make the script for you

Helpful
on December 14, 2014

I think that dynamic sound is best because I want my game to feel as real as possible. so thank you for writing a script for me.

on December 14, 2014

assuming that you are using a terrain this script check the splatmap of the tetures and return the different colors as an index value:

using UnityEngine;
using System.Collections;
public class TerrainSurface : MonoBehaviour {
 public static float[] GetTextureMix(Vector3 worldPos) {
 // returns an array containing the relative mix of textures
 // on the main terrain at this world position.
 // The number of values in the array will equal the number
 // of textures added to the terrain.
 Terrain terrain = Terrain.activeTerrain;
 TerrainData terrainData = terrain.terrainData;
 Vector3 terrainPos = terrain.transform.position;
 // calculate which splat map cell the worldPos falls within (ignoring y)
 int mapX = (int)(((worldPos.x - terrainPos.x) / terrainData.size.x) * terrainData.alphamapWidth);
 int mapZ = (int)(((worldPos.z - terrainPos.z) / terrainData.size.z) * terrainData.alphamapHeight);
 // get the splat data for this cell as a 1x1xN 3d array (where N = number of textures)
 float[,,] splatmapData = terrainData.GetAlphamaps(mapX,mapZ,1,1);
 // extract the 3D array data to a 1D array:
 float[] cellMix = new float[splatmapData.GetUpperBound(2)+1];
 for (int n=0; n<cellMix.Length; ++n)
 {
 cellMix[n] = splatmapData[0,0,n]; 
 }
 return cellMix; 
 }
 public static int GetMainTexture(Vector3 worldPos) {
 // returns the zero-based index of the most dominant texture
 // on the main terrain at this world position.
 float[] mix = GetTextureMix(worldPos);
 float maxMix = 0;
 int maxIndex = 0;
 // loop through each mix value and find the maximum
 for (int n=0; n<mix.Length; ++n)
 {
 if (mix[n] > maxMix)
 {
 maxIndex = n;
 maxMix = mix[n];
 }
 }
 return maxIndex;
 }
}

just copy that and create a new c# named TerrainSurface.cs and thats all… now to use it just put a int in your player script as this:

int myInt = TerrainSurface.GetMainTexture(transform.position);

where transform.position should be your player transform position

so this is just to identify the texture below your player, in order to continue tell me if your animations are maded inside unity with the animation tool or in a third party program??

Helpful
on December 15, 2014

thank you very much for the scripts. I appreciate it a lot, and for your question I just downloaded the 1st person controller off unity.

Devoted
on December 15, 2014

I think Deadlycrow was asking if you created your animation inside unity or imported it from another program (such as maya or blender)

Helpful
on December 16, 2014

i just downloaded the first person controller off unity.

Devoted
on December 16, 2014

oh so you mean you dont have any animation as you are in first person?

on December 16, 2014

?? well you just need to make a script with a function(you will add that function to the animation, or make a coroutine to play it in a loop) like this:

public AudioClip[] grassSounds;
public AudioClip[] dirtSounds;
public AudioClip[] woodSounds;
void myStepSoundEffect(){
   int curSplatIndex = TerrainSurface.GetMainTexture(transform.position);
   // assuming that 0 is your grass texture, 1 your dirt texture and 2 the wood texture
   // just for the example.
   if(curSplatIndex == 0){
     audio.playOneShot(grassSounds[Random.Range(0,grassSounds.Length -1)]);
   }
   else if(curSplatIndex == 1){
     audio.playOneShot(dirtSounds[Random.Range(0,dirtSounds.Length -1)]);
   }
   else if(curSplatIndex == 2){
     audio.playOneShot(woodSounds[Random.Range(0,woodSounds.Length -1)]);
   }
//and so on...
}

i use this method in my games as you can see here: https://www.youtube.com/watch?v=BnHlmcS_T10&list=UUKiv2Rm0u7d5HgXIER6VRzg

Devoted
on December 21, 2014

sorry to post back here again; 
Deadlycrow do you mind if i use your footstep terrain script in my game?

Helpful
on December 21, 2014

thank you again deadlycrow for the script again, it well be very helpful to me.

on December 24, 2014

you are welcome :D!  i dont mind it at all is not mine, i found it on the unity answers, but yeah it is very helpful ;D

Show more replies
  • Liked by
Reply
Cancel
1 on December 21, 2014

@Deadlycrow Wouldn’t a switch statement be better instead of all the if else statements:P?

on December 24, 2014

yep i dont use too much switch statement so i dont know it very well but yep it is better i think 😮

Show more replies
  • Liked by
Reply
Cancel