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 Kokujou · Sep 11, 2014 at 06:53 PM · c#audioaudiosourcedelay

Unity - Play AudioSource without delay

The Problem is: I'm playing some sort of animation... in general its the rotation of a text over a period of time using the Thread.Sleep() Function (C#) I want to Play a Sound while this Animation loads but when i play the audio source the sound starts playing when the animation already ended !! it's not a very long animation and it's not in the audio file. i checked it there is no delay in the file so it has to be in the code somewhere. what can i do to get rid of this delay?

Comment
Add comment · Show 5
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 akruckus · Sep 11, 2014 at 07:21 PM 0
Share

Would you $$anonymous$$d posting the code?

avatar image khos85 · Sep 11, 2014 at 07:42 PM 0
Share

Why are you using Thread.Sleep() ? Can't you create a Unity Animation to rotate the text.. then the audio should play ok?

avatar image alexpizzini · Sep 11, 2014 at 07:49 PM 0
Share

I'm not quite sure how you're rotating with Thread.Sleep, but I'd guess putting the thread to sleep is delaying the audio source.

avatar image ExtremePowers · Sep 11, 2014 at 08:02 PM 0
Share

Try placing the Audio.play() right before the Thread.Sleep

avatar image Kokujou · Sep 12, 2014 at 06:23 PM 0
Share

of course thats what i ded !! i added the audio.play right before the Sleep function and i use the Sleep function for a delay because my "animation" is just ... set new rotation -> wait 20msec -> set another new rotation etc... thats basically it because i'm not used to unity i don't know any ways how to make it better ^-^ i don't know how to create a real animation

1 Reply

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

Answer by rutter · Sep 11, 2014 at 11:47 PM

You should almost never perform blocking operations on Unity's main thread. Doing so will freeze the entire engine while you wait.

It's better to use timers that update once per frame, such as coroutines.

For example, this coroutine:

 IEnumerator Spin(Vector3 spin, float duration) {
     Vector3 spinPerSecond = spin / duration;
     for (float time = 0f; time <= duration; time += Time.deltaTime) {
         transform.Rotate(spinPerSecond * Time.deltaTime); //spin a little bit
         yield return null; //wait for next frame
     }
 }


 //call this elsewhere:
 Vector3 spinAxis = Vector3.up;
 float spinDegrees = 360f;
 float spinSeconds = 2f;
 StartCoroutine(Spin(spinAxis * spinDegrees, spinSeconds));
Comment
Add comment · Show 2 · 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 Kokujou · Sep 12, 2014 at 06:32 PM 0
Share

lets see if i get this correct: float duration is the complete duration of the whole animation right? so it rotates as long as this duration is not fit? and Vector3 spin is the complete rotation ... the number of degrees it will rotate in the given duration... i'll try if this works better thank you !!

avatar image Kokujou · Sep 12, 2014 at 07:01 PM 0
Share

It worked perfectly thank you !! i can use this very good ... well the only Problem with your Rotate version was that it didn't stop in 360 degrees... the resulting text was a bit escrow. but the animation is very fast so it doesn't matter when i just but it from 0 to 100 in the original direction ^-^ thank you !! that helped me a lot

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't get sound clip from array to play properly 1 Answer

Triggering multiple audio clips to play in sequence 1 Answer

How to play "audiosource" component in a prefab from script? 0 Answers

Help with sound clips [C#] 0 Answers

A node in a childnode? 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