- Home /
I need help about audio.PlayOneShot!!
I have to say first...I do not write English well, Because I am a Japanese. My JavaScript is something wrong!! I tried Play the "AOIdeathsound" when the player is dead, Play the "AOIVOICE" when the player is running. but these sounds does not play correctly.
Please help me.
public var AOIvoice : AudioClip;
public var AOIdeathsound : AudioClip;
function Update()
{
if (inputH || inputV)
{
if( Input.GetButton("run") )
{
direction += transform.forward * Input.GetAxis("Vertical") * 3.5f;
timer();
Timer -= Time.deltaTime;
if (Timer <= 0.2)
{
audio.PlayOneShot(footstepsound);
audio.volume = 0.1;
Timer=0.6;
}
animator.SetBool( "run", true );
}
else
{
animator.SetBool( "run" , false );
}
}
if(playerlife == 0)
{
animator.SetBool("Down" , true);
audio.PlayOneShot(AOIdeathsound);
Invoke("Continue", 1);
}
}
function timer()
{
stopTimer -= Time.deltaTime;
if (stopTimer <= 1.0)
{
audio.PlayOneShot(AOIvoice);
audio.volume = 0.3;
stopTimer=2.0;
}
if ( stopTimer <= 2.0)
{
audio.PlayOneShot(AOIvoice);
audio.volume = 0.3;
stopTimer = 2.0;
}
}
This script is so long, I have omitted parts unrelated.
When you say the sound doesn't play correctly. could you tell us what it is actually doing? Anyhow I would recommend you to add this condition:
if(!audio.IsPlaying)
anytime before calling PlayOneShot()
well....The sound is sound like broken. I just want to play sound clearly.
I will try use that. thanks.
Its actually
if(!audio.isPlaying)
The 'is' has small 'i'. :-)
Answer by Lylek · Jun 27, 2014 at 06:03 AM
If the sound seems broken, it may be trying to play multiple sounds at the same time, from the same Audio source. Before playing another sound, try stopping all sound from the audio source with audio.Stop();
When playing multiple sounds from 1 Audio source, I found it best to do:
audio.Stop(); audio.clip = yourAudioClip; audio.pitch = 1; audio.volume = 1; audio.Play();
Instead of PlayOneShot.
If you have any sounds, such as ambient music, have it play from a separate audio source, perhaps on an empty game object.
That's my experience anway, hope you figure it out.