- Home /
Sprinting Audio Problem
Hey, I would like to ask how to add a sprinting sound to my game. I already have a system that allows me to play footsteps but I want to know how to make these sounds play at a faster rate while running?
This is my footstep Script:
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.4) {
audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
audio.Play();
yield WaitForSeconds(audioStepLength);
} else {
yield;
}
}
}
EDIT
Ok, I have been suggested the following script - how do I combine them? Im spatially 'slow' when it comes to scripting
SUGGESTED SCRIPT
var walk:AudioClip;
var run:AudioClip;
if(walk){
audio.clip=walk;
audio.Play();
}else if(run){
audio.clip=run;
audio.Play();
}
yes, but It must have both footsteps and running footsteps... How Do?
run is running steps, walk is footsteps. audio.clip = clip; is switching the clip before using them. Your script shows two variable slots (run/walk) in the Inspector. Simply drag the audio files in there.
it doesnt work :( how about editing the script so that it will play walking sounds when velocity is above 0.4 but below 10 and when the velocity is above 10 play running footsteps:
so ins$$anonymous$$d of being just 0.4 its > 0.4 < 10???
Answer by fafase · Aug 18, 2012 at 11:39 AM
Modifying the pitch of the audio source result in a faster tempo but also a higher pitch.
In your case, I would recommend using Audacity (free), you can easily (really easily) fast up the speed of a file and lower its pitch to make it sound ok. It tkes about a couple of trials and 5min.
Then import your new sound as usual and:
var walk:AudioClip;
var run:AudioClip;
if(Input.GetKey(KeyCode.R) && Input.GetKey(KeyCode.Akey)){
Run();
audio.clip=run;
audio.Play();
}elseif(Input.GetKey(KeyCode.Akey)){
Walk();
audio.clip=walk;
audio.Play();{
}else
audio.Pause();
You could use this simple principle. Make sure the playing is looped. The runing is based on how your guy moved plus pressing r (r is checked first to save cycle). If r is not pressed, then maybe the movement is on but you are walking. Then is nothing then you are not walking the stepping stops. The complicated part compared to way 2 is that you need to manually tweak the animation speed to make the sound and steps match. Immersion would not like if you hear a step while the foot is up.
EDIT:Way2
You can use animation event easily.
The curve of your right and left foot should look like a sin wave (going up and down). Create a single step sound (just "pac";, no "pac-pac-pac").Add an event when the green curve of the feet animations are at the lowest and add this:
var step:AudioClip;
function StepSound(){
AudioSource.PlayClipAtPoint(step,transform.position);
}
Attach this to the right and left feet. Then follow the tutorial in the link http://docs.unity3d.com/Documentation/Components/animeditor-AnimationEvents.html
Do the same process for the running. The curves have a higher frequency, your running sound happens more often. You are running.
how do I add this:
var walk:AudioClip; var run:AudioClip;
if(walk){ audio.clip=walk; audio.Play(); }else if(run){ audio.clip=run; audio.Play(); }
Do I have to add this to my script or what? how do i do this! I'm a noob when it comes to javascripting
Yes you add this to the movement script. I updated the answer.
hold on? which script do i add it to?: $$anonymous$$y Sprinting and crouching or the Footstep Script?