FootSteps Script

Well, I have this script:

`
var audioStepLength: float = 0.1;
var footAudio: AudioClip;

var controller: CharacterController;

function Awake(){
FootStepsSound();
}

function FootStepsSound(){
while(true){
if (controller.isGrounded && controller.velocity.magnitude > 0.5){
var volume = Mathf.Clamp01(Time.time);
controller.audio.PlayOneShot(footAudio[Random.Range(0, footAudio.Length)], volume);
yield WaitForSeconds(audioStepLength);
} else {
yield;
}
}
}
`

I attach it to a Blank GameObject, and set the Controller to my Player, and attach an AudioSource to my Player, but, when I click Play, the Unity stop working.

If the while is making it crash, add this right after while

yield WaitForSeconds (1);

Oh, sorry, I just got it working, if someone wants it, it needs 2 scripts made by me:

RunSneak (attach to Player):

/***
 * RunSneak
 * Make character run and sneak
 *
 * Author: WilliamPigmeu
*/

// Interacting with BobbingView for a realistic head-bob

static var bobbingSpeed;
static var bobbingAmount;
static var playerStamina = 250;

static var audioStepLength: float = 0.5;
static var stepSound = 0;

var walkSpeed: float = 7; // regular speed
var sneakSpeed: float = 3; // sneaking speed
var runSpeed: float = 20; // running speed

var walkStep: float = 0.5;
var sneakStep: float = 1;
var runStep: float = 0.45;

private var isRunning: boolean = false;

private var chMotor: CharacterMotor;
private var tr: Transform;
private var dist: float; // distance to ground

function Start(){
    chMotor = GetComponent(CharacterMotor);
    tr = transform;
    var ch:CharacterController = GetComponent(CharacterController);
    dist = ch.height/2; // calculate distance to ground
}

function Update(){
    if(chMotor.grounded){
        bobbingSpeed = 0.20;
        bobbingAmount = 0.3;
    } else {
        bobbingSpeed = 0;
        bobbingAmount = 0;
    }
    
    var vScale = 1.0;
    var speed = walkSpeed;
    audioStepLength = walkStep;
    stepSound = 2;
 
 if (!isRunning){
 if (playerStamina < 250){
   playerStamina = playerStamina+2;
 }
 } else if (isRunning){
 if (playerStamina >= 0){
   playerStamina = playerStamina-3;
 }
 }
 
 isRunning = false;
 
    if (Input.GetKey("left shift") && chMotor.grounded){ // press LShift to run
 if (playerStamina >= 3){
   isRunning = true;
   
   audioStepLength = runStep;
   stepSound = 0;
   
   speed = runSpeed;
      bobbingSpeed = 0.30;
      bobbingAmount = 0.3;
     } else {
   isRunning = false;
     }
    }
 
    if (Input.GetKey("c")){ // press C to sneak
        speed = sneakSpeed;
        bobbingSpeed = 0.15;
        bobbingAmount = 0.1;
        
        audioStepLength = sneakStep;
        stepSound = 1;
    }
    
    chMotor.movement.maxForwardSpeed = speed; // set max speed
    var ultScale = tr.localScale.y; // crouch/stand up smoothly 
    tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
    tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position
}

FootSteps (attach to anything and set controller to Player):

var footAudio: AudioClip[];
var controller: CharacterController;

private var RunSneak: RunSneak = null;

function Awake(){
 FootStepsSound();
}

function FootStepsSound(){
 while(true){
  if (controller.isGrounded  && controller.velocity.magnitude > 3){
    var volume = Mathf.Clamp01(Time.time);
    controller.audio.PlayOneShot(footAudio[RunSneak.stepSound], volume);
    yield WaitForSeconds(RunSneak.audioStepLength);
   } else {
    yield;
   }
  }
}

#pragma strict
@script RequireComponent( AudioSource )

var walk : AudioClip;
var run : AudioClip;
 
var walkAudioSpeed : float = 0.4;
var runAudioSpeed : float = 0.2;
 
private var walkAudioTimer : float = 0.0;
private var runAudioTimer : float = 0.0;
 
var isWalking : boolean = false;
var isRunning : boolean = false;
 
var walkSpeed: float = 7; // regular speed
var runSpeed: float = 20; // run speed
 
private var chCtrl: CharacterController;
private var chMotor: CharacterMotor;
 
function Start()
{
    chCtrl = GetComponent(CharacterController);
    chMotor = GetComponent(CharacterMotor);
}
 
function Update()
{
    SetSpeed();
 
    if ( chCtrl.isGrounded )
    {
        PlayFootsteps();
    }
    else
    {
        walkAudioTimer = 0.0;
        runAudioTimer = 0.0;
    }
}
 
function SetSpeed()
{
    var speed = walkSpeed;
 
    if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
    {
        speed = runSpeed;
    }
 
    chMotor.movement.maxForwardSpeed = speed;
}
 
function PlayFootsteps() 
{
    if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
    {
       if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
       {
         // Running
         isWalking = false;
         isRunning = true;
       }
       else
       {
         // Walking
         isWalking = true;
         isRunning = false;
       }
    }
    else
    {
       // Stopped
       isWalking = false;
       isRunning = false;
    }
 
    // Play Audio
    if ( isWalking )
    {
       if ( audio.clip != walk )
       {
         audio.Stop();
         audio.clip = walk;
       }
 
       //if ( !audio.isPlaying )
       if ( walkAudioTimer > walkAudioSpeed )
       {
         audio.Stop();
         audio.Play();
         walkAudioTimer = 0.0;
       }
    }
    else if ( isRunning )
    {
       if ( audio.clip != run )
       {
         audio.Stop();
         audio.clip = run;
       }
 
       //if ( !audio.isPlaying )
       if ( runAudioTimer > runAudioSpeed )
       {
         audio.Stop();
         audio.Play();
         runAudioTimer = 0.0;
       }
    }
    else
    {
       audio.Stop();
    }
 
    // increment timers
    walkAudioTimer += Time.deltaTime;
    runAudioTimer += Time.deltaTime;    
}