- Home /
Foot step audio- randomize pitch
I'm trying to play audio of footsteps whenever my character is moving. The Lerpz example will not work because we are in first person mode, so the player does not have two feet with colliders, just one capsule collider that glides along. Therefore, I just need a script that randomizes the pitch of our footstep audio clip, then plays it when the player is moving forward, backward, or side to side. I am not an experienced progammer, so please be kind!! Any help would be greatly appreciated.
Answer by aldonaletto · Aug 12, 2011 at 08:17 PM
The FPS Tutorial has a good example. If you've downloaded it already, look for FpsPlayer.js script - the tutorial Assets folder has the footstep sounds too. If you don't have this tutorial, use the script below. You should attach it to the player, then at the Inspector set the walkSounds array size to 5 (or the number of sounds you want) and define each element to a different footstep sound.
var walkSounds : AudioClip[]; // fill this array with the sounds at the Inspector var audioStepLength = 0.3;
function Start(){ var controller : CharacterController = GetComponent(CharacterController); while (true) { if (controller.isGrounded && controller.velocity.magnitude > 0.3) { audio.clip = walkSounds[Random.Range(0, walkSounds.length)]; audio.Play(); yield WaitForSeconds(audioStepLength); } else { yield; } } } But if you prefer to vary the pitch as you've suggested, you can use this:
var stepLenght = 0.28;
function Start(){ var controller : CharacterController = GetComponent(CharacterController); while (true) { var vel = controller.velocity.magnitude; if (controller.isGrounded && vel > 0.3) { audio.pitch = 0.9 + 0.2*Random.value; // randomize pitch in the range 1 +/- 0.1 audio.Play(); yield WaitForSeconds(stepLength*vel/0.3); // shorten the step length with speed } else { yield; } } }
This script works great... with one exception. For some reason, the audio does not stop looping when the player stops moving. From what I can tell in the script, it should (but I'm not a programmer, so what do I know!) How might I go about preventing the audio clips from playing when the player is not moving?
It should stop whenever the character speed fell below 0.3.
Which script have you used, the first (array of sounds) or the second (varying pitch)?
I used the first script utilizing 4 differnt audio clips. As for the character, I'm using the basic first person controller from the standard assets.
I tested it in a First Person Controller and it worked fine. The if right above the audio.clip... line only executes the audio instructions if the character is grounded AND its velocity is higher than 0.3, exactly to ensure that the sound will stop when the character slows down too much. Did you copy and paste exactly the same script? Or have you done some adaptation to your case?
Interesting... I'm using the first script at the top. I tried applying the same script to the same first person controller in a test scene, and it seemed to work fine. However, in our actual game scene, the footsteps continually loop after the player has stopped moving. Could this be caused by other audio sources attached to our first person controller that are set to loop? If not, I'm not quite sure where to look or what I need to do here... Thank you very much for your help by the way Aldonaletto!!
Answer by lewchico · Aug 12, 2011 at 08:49 PM
Hi there..I have a similar issue to this....would like to vary a sound's pitch each time it is triggered....tried to modify your above script but does not compile...
function Start(){
audio.clip = squeakysample[Random.Range(0, squeakysample.length)];
audio.Play();
}
Also...do I attach this to the sample itself?
Apologies for newbiness...just beginning unity recently...cheers!
@lewchico, please post this question as a new question - I can't answer it here because it would mess things up in Unity Answers - people would think this question has 3 answers, nobody will be able to find your question, etc.
Your answer
Follow this Question
Related Questions
Random footsteps 5 Answers
Random Audioclip. No Repeat ?? 1 Answer
Random Footsteps within CollisionSoundEffects Script 1 Answer
Footstep Script Not Working 2 Answers
Set audio.pitch limits 1 Answer