- Home /
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);
}
}
}
Answer by DaveA · Sep 12, 2012 at 10:21 PM
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.
Ive figured out that if I have while true in my script it crashes Unity when the game starts. Is there another way I can script this without using while true, also If I just remove the while true statement my footstep sound does not play. Can you help me out?
Something like this:
var stepLength = .3;
var lastCheck = 0.0;
function Update()
{
if (Time.time - lastCheck > stepLength)
{
lastCheck = Time.time;
audio.Play();
}
}
and you need to change stepLength to be smaller for fast and larger for slow
Answer by cariaga · Sep 15, 2012 at 02:24 PM
hope it works i forgot where i got this from
pragma strict
ar 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;
}
}
}