Increase speed of footsteps when sprinting?

Im trying to have this script play the footstep sounds at a normal speed when the player is just walking but when sprinting the sounds are sped up, but this script doesn’t work and for some reason crashes Unity. Can someone help me out?

var footsteps : AudioClip;

var waitTime = 0.5;

var sprintwaitTime = 0.2;



function Start () {

  // create an infinite loop that runs every frame:

  while (true){

    if(Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d")){

      yield WaitForSeconds(waitTime);

      audio.PlayOneShot(footsteps);

}

    

    if(Input.GetKeyDown("left shift")){

    yield WaitForSeconds(sprintwaitTime);

    audio.PlayOneShot(footsteps);

}

}

}

I myself am not a big fan of while(true), but in this case, if you must, note that your check for Input from ‘left shift’ is outside that while(true) loop, so it never gets there. Put it inside your loop.

hope it works i forgot where i got this from


#pragma strict
var audioStepLength = 0.3;

var walkSounds:AudioClip;

function Start ()

{

var controller : CharacterController = GetComponent(CharacterController);



while (true)

{

    if (controller.isGrounded && controller.velocity.magnitude > 0.3)//magic here

    {

        audio.clip = walkSounds[Random.Range(0, walkSounds.length)];

        audio.Play();

        yield WaitForSeconds(audioStepLength);

    }

    else

    {

        yield;

    }

}

}