• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Iratorum · Nov 12, 2014 at 01:47 PM · audiosoundissueplayonce

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());
                 
         
     }
 }

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrSoad · Nov 12, 2014 at 01:31 PM 0
Share

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.

avatar image Iratorum · Nov 12, 2014 at 02:07 PM 0
Share

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

avatar image smoggach · Nov 12, 2014 at 02:37 PM 0
Share

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.

avatar image MrSoad · Nov 12, 2014 at 02:39 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Some audio does not play on Windows build but works perfectly in editor and on Mac 1 Answer

Audio not playing! 0 Answers

[NoBraves] Sound makes lag 1 Answer

Best way to play audio sound effects 1 Answer

Can't play any sounds. What's wrong? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges