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.
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.
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();
Pay special note to the example used, as it requires an AudioListener component on the gameObject
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
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.
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??
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.
i just downloaded the first person controller off unity.
?? 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
thank you again deadlycrow for the script again, it well be very helpful to me.
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