- Home /
Audio issue - Trying Waitforseconds
Hello I'm trying to stop a sound from repeatedly playing on my GetKey command, while the button is hold the sound will play over and over although I only want it to play once every few seconds otherwise the sound gets really loud and irritating, I've tried to create a cooldown using a coroutine and it's not working, also if I put audio.Stop() in the IEnumerator then the sound doesn't play at all. Here's my code.
using UnityEngine;
using System.Collections;
public class JumpScript : MonoBehaviour {
Animator anim;
public AudioClip JumpSound;
public AudioClip SlidingSound;
public float jumpSpeed = 1000f;
float jumpRate = 1.2f;
bool canJump = true;
public float slideTime = 1.0f;
public float timeHeld;
void Start()
{
this.gameObject.AddComponent<AudioSource>();
this.GetComponent<AudioSource>().clip = JumpSound;
this.gameObject.AddComponent<AudioSource>();
this.GetComponent<AudioSource>().clip = SlidingSound;
anim = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKey(KeyCode.Space)) timeHeld += Time.deltaTime;
if (Input.GetKeyUp(KeyCode.Space) && timeHeld <= slideTime) TryJump();
if (Input.GetKey(KeyCode.Space) && timeHeld > slideTime) Slide();
timeHeld = 0f;
}
void TryJump()
{
if (!canJump) return;
Jump();
StartCoroutine(JumpCooldown());
}
IEnumerator JumpCooldown()
{
canJump = false;
yield return new WaitForSeconds(jumpRate);
canJump = true;
yield break;
}
IEnumerator SoundCooldown()
{
audio.Play();
yield return new WaitForSeconds(2);
audio.Stop();
}
void Jump()
{
audio.PlayOneShot(JumpSound);
rigidbody2D.AddForce(new Vector2(0, jumpSpeed));
anim.Play("Jumping");
}
void Slide()
{
audio.PlayOneShot (SlidingSound);
anim.Play("Sliding");
StartCoroutine(SoundCooldown());
}
}
You know you can do a check to see if the audio is playing :
http://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html
This might not sort out your whole issue but it is a very useful tool.
I have tried messing around with this but it doesn't seem to have the desired effect, because the clip loops continuously and i'm not sure how to stop it after just one play through
does your jump cooldown work? If so then you only need to do your sound like the jump (TrySound)
I think the real issue here though is the fact that in your Update function timeHeld will only ever equal Time.deltaTime. You increment it then immediately set it to 0 every frame. if deltaTime is bigger than slideTime then you are calling Slide() every frame.
Also if you know how long the audio clip is you can simply check AudioSource.time to see if it has finished.
Create a Bool(isRunning) check for a sound play coroutine. Set to true when you first play the sound, don't allow restart of the coroutine if it is true. Play the sound in the coroutine, while isPlaying yield the coroutine, add another yield in the coroutine after this for however long you wish to delay the repeat playback further, once this yield expires set your Bool(isRunning) to false at the end of the coroutine.
Or if you never want it to play again unless it has both finished playing the sound(with or without an extra delay) and the key has been pressed again then use Get$$anonymous$$eyDown in conjunction with your bool (isRunning ) coroutine check.
Your answer
Follow this Question
Related Questions
Some audio does not play on Windows build but works perfectly in editor and on Mac 1 Answer
[NoBraves] Sound makes lag 1 Answer
Audio not playing! 0 Answers
Best way to play audio sound effects 1 Answer
Can't play any sounds. What's wrong? 1 Answer