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.
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;
}
}
}
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!