Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

[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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges