- Home /
[JS]Sounds with animation issue
Hey, i'm new in unity and i got a few problems. I want to change the walk sound activated from key to animation (parent sound to animation once). This is my source code:
#pragma strict
var fwalk : AudioClip;
function Update () {
if (animation["fwalk"].enabled == true){
print("fwalk");
audio.clip = fwalk;
audio.Play();
}
else if (animation["run"].enabled == true){
print("run");
audio.clip = fwalk;
audio.pitch = 2.2;
audio.Play();
}
}
And now the problem. whenever i press the move key on keyboard, there is just no sounds. but after running/walking when i unpress the move key, the walking/running sound playing. Someone know what happend and how to fix this ?
hello,
it depends what is in your audio clip but if the sound is too long, you restart playing it everyframe... try to add the condition: if(!audio.isPlaying)
before playing any sound.
$$anonymous$$aybe check this example, it seems safer to have the states of your player (running/walking), it might be useful if you go further in its programmation.
Now the walk sound is just not playing all the time. weird :< .
But if i just parent to colliders itself, than sound will play always, and not just while i moving the character :) .
using UnityEngine;
using System.Collections;
public class sound2animation : $$anonymous$$onoBehaviour {
AudioClip fwalk;
void OnCollisionStay(Collision mapa){
if (animation.IsPlaying("fwalk")){
print("fwalk");
audio.clip = fwalk;
audio.Play();
}
else if (animation.IsPlaying("run")){
print("run");
audio.clip = fwalk;
audio.pitch = 2.2f;
audio.Play();
}
}
}
This is the c# code, and the problems is now, the walk sound actually play all over without stopping, and already pitch to 2.2f as the last part of the code. Someone can help me fix this ?
Answer by meat5000 · Aug 25, 2014 at 03:33 PM
Stick colliders on the feet and play the sound on detection of the floor, in OnCollision :)
Hacktastic.
Your answer
Follow this Question
Related Questions
Synchronize Audio and Animation 0 Answers
Play from a variety of sounds? 1 Answer
Toggling BGM and SFX 0 Answers