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 kjuuz · Jan 28, 2014 at 01:32 PM · soundaudioclipsecondsinterval

Update and PlayOneShot (audio interval)

Heeey developers :)

I have one question - how can i set time between the same audio file? The thing what i want to do is - While holding key play sound in loop. this is what i have:

 function Update () {
 if(Input.GetKey(KeyCode.X)) {
 audio.PlayOneShot(sound);
 } 
 }

Everything would be just fine if sound wouldnt be looped (and started over) every single frame. So the sound isnt anymore sound, but noise (like - trtrtrtrtrtr..) But the thing is i dont want to play whole audio clip i just want to play 0.5 secs not 0.1 or less like how is it now.. So solution wouldn't be to check if sound is played till end.. I also tried like this:

 var timer : float;
 if(Input.GetKey(KeyCode.X)) {
 
 timer = Time.time * 0.1; //also tried deltaTime..
 if(timer > 0.5) {
 timer = 0;
 audio.PlayOneShot(sound);

 }
 }
 

But no success Any ideas?

PS. Audio source is on Player GObj and player have more then one sound, so if i will edit AudioSource component, then also will be edited other sounds.. right?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ArkaneX · Jan 28, 2014 at 02:06 PM

You need to increment the timer, by the amount of time elapsed since last frame, while in your code you set it always to one tenth of time elapsed since scene start. Use:

 timer += Time.deltaTime;

instead of

 timer = Time.time * 0.1; //also tried deltaTime..

Additionally, you need to:

1) Declare timer variable outside of Update function

2) Call audio.Stop() before calling audio.PlayOneShot (otherwise your sounds will stack)

Comment
Add comment · Show 3 · Share
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 ArkaneX · Jan 28, 2014 at 02:34 PM 0
Share

Expanded my answer a bit.

avatar image kjuuz · Jan 28, 2014 at 03:58 PM 0
Share

Thaaaank you. Then timer (without +)= time.deltatime * 0,1... Timer will always be 0,1?

Worked like a charm. Thank you for your time. :)

avatar image ArkaneX · Jan 28, 2014 at 04:08 PM 0
Share

If you use timer = Time.deltaTime * 0.1, then it will always be equal to one tenth of Time.deltaTime for the current frame. So if your game runs at 60FPS, timer value would be around 0,0017.

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayOneShot returns false for isPlaying 5 Answers

Android sound distorted 2 Answers

Audio: -3db automatic attenuation on any audio playing? 0 Answers

Only able to play a single sound through AudioSource. 1 Answer

Draw in a Texture a Waveform From Audioclip Before Play 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