Reputation: 21
I attached a sound clip to my object walk function, but when my object stops the sound keeps running. I'm using the PlayOneShot
function but the sound plays 3 or 4 secound more than the animation. The sound is mixed with itself also.
if (JSMove.x != 0.0f) {
transform.GetComponent<Animation>().Play("maleWalk");
audio.PlayOneShot(WalkSound, 0.5f);
}
Upvotes: 1
Views: 1325
Reputation: 16623
@Mark's answer fixes partially the problem: while the animation is running the sound would be played over and over again. So you have to check if the sound is still running before playing it again.
if (JSMove.x != 0.0f) {
transform.GetComponent().Play("maleWalk");
if (!audio.isPlaying)
audio.PlayOneShot(WalkSound, 0.5f);
} else if (audio.isPlaying)
audio.Stop();
Upvotes: 1
Reputation:
PlayOneShot is to play an audio clip, it won't stop playback before the 'walksound' is over. Without the code you resist to submit, I can give you a generic answer which is, your code should be something like:
if (JSMove.x != 0.0f) {
transform.GetComponent().Play("maleWalk");
audio.PlayOneShot(WalkSound,0.5f);
} else if (audio.isPlaying) { //no movement yet audio did not finish the clip
audio.Stop();
}
The "mixover" you mention is because the clip is still playing when you try to start over and play it again. You either should stop playback before you start again, or, depending on your game's needs, don't start playing the clip again (i.e. let the 'previously started' audioclip finish playback).
Upvotes: 0