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
2
Question by sonic220 · Mar 18, 2012 at 01:56 PM · instantiatemusicsynchronization

perfect synch with music

Hi guys, I need a little help here. I want to create a rhythm game for android and I need enemy to appear on screen on a specific time matching with the music. I've wrote this piece of script that makes me go out of sync:

 for(var k = 1; k < time_s.Length; k++){
     if(gameover)
         return;
     var lack : double = Time.time;
     print(Time.time);
     Instantiate(enemy_s[k-1],myTransform.position,myTransform.rotation);
     lack-=Time.time;
     print(Time.time);
     yield new WaitForSeconds(wait[k] -lack);
 }

Problem is that Instantiate does take it's time and it's added to the instantiation of the enemy, making the game out of sync after 20-30 enemys. I've tried to evade this problem by using the lack variable that should store the instantiation time and subtract to the wait, but Time.time only have 4-5 decimal and lack is always 0. Does exist a Time.time more precise or can you guys help me finding another way to do it?

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 asafsitner · Mar 18, 2012 at 02:06 PM 1
Share

Why not have them all instantiated already, but disabled? Then you can just enable them when the time is right.

avatar image Kleptomaniac · Mar 18, 2012 at 02:07 PM 0
Share

Lack is always 0 because you are defining it as being both equal to Time.time and then you are subtracting Time.time off of it. These cancel each other out and you are left with 0. You should ins$$anonymous$$d be += Time.time on to lack.

avatar image sonic220 · Mar 18, 2012 at 02:29 PM 0
Share

@$$anonymous$$leptomaniac I need to find the estimated time for instantiating that object, how can I do that by adding Time.time to lack? @asafsitner I have to change all my script to adapt it, but it's not a bad idea if I can evade the delay

avatar image Kleptomaniac · Mar 18, 2012 at 02:42 PM 0
Share

Sorry, yes I think the system you are currently using is rather unreliable. @asafsitner's solution would probably be the best in terms of the fact that this is a rhythm game.

avatar image sonic220 · Mar 18, 2012 at 03:10 PM 0
Share

unfortunatly @asafsitner's solution create the same effects, still having delay

1 Reply

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

Answer by aldonaletto · Mar 18, 2012 at 04:35 PM

You can sync to audio.timeSamples instead of Time.time - this property shows the current sample number being played, thus it's in perfect sync with the music. In order to use your present array time_s, you can convert the time to the timeSamples equivalent (multiply by audio.clip.frequency):

for (var k = 1; k < time_s.Length; k++){ if (gameover) return; // find the sample number equivalent to time_s[k]: var nSample = time_s[k] * audio.clip.frequency; while (audio.timeSamples < nSample) yield; // wait till the desired sample Instantiate(enemy_s[k-1],myTransform.position,myTransform.rotation); }

NOTE: time_s must contain the times at which the enemies should appear, not the intervals between them.

Comment
Add comment · Show 11 · 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 sonic220 · Mar 19, 2012 at 04:13 PM 0
Share

you... I LOVE YOU $$anonymous$$AN! It's in perfect sync in every device! Thanks! I still have to understand how timeSamples is working, but it's almost perfect now! Thanks again!

avatar image aldonaletto · Mar 19, 2012 at 09:00 PM 0
Share

timeSamples shows the number of the current sound sample: in a CD-like sound, for instance, the sampling rate is 44100Hz, what means that 44100 samples are sent to the output mixer per second. If you want to do something at 1.5s after the beginning of the sound, yield while timeSamples < 44100 * 1.5 - Unity will wait until the desired sample has been played.

avatar image Nerdmigo · Sep 10, 2012 at 02:12 PM 0
Share

hey everbody, iam having a similar problem, but i am using c#, how would you translate that part with yield especially in to a c# code ? actually i think one should use clip.samples ins$$anonymous$$d of frequency, to return the actual sampleamount ..

avatar image aldonaletto · Sep 10, 2012 at 03:58 PM 0
Share

The whole routine must be declared as a coroutine (it must return IEnumerator) and you must call it with StartCoroutine, like StartCoroutine(SincToAudio()). Yield in C# must return something (no idea why), thus it becomes yield return 0;:

IEnumerator SincToAudio(){
  for (int k = 1; k < time_s.Length; k++){
    if (gameover)
       return; // not sure if this should return 0 in CS
    // find the sample number equivalent to time_s[k]:
    int nSample = time_s[k] * audio.clip.frequency;
    while (audio.timeSamples < nSample) yield return 0; // wait till the desired sample
    Instantiate(enemy_s[k-1], myTransform.position, myTransform.rotation);
  }
}
clip.samples is the clip length in samples, but what you really need is to convert a time value in number of samples - that's why the time is multiplied by the audio clip sample frequency (audio.clip.frequency).
avatar image goutham12 aldonaletto · Sep 03, 2019 at 10:27 AM 0
Share

Hi sir, I need to use the same logic for my game. where i need spawn some fruits to sync with the music. how can i use your logic. i didn't understand what is time_s in the above. Could you please take moment and help me out of this

Thank you

avatar image Nerdmigo · Sep 10, 2012 at 04:06 PM 0
Share

thanks alot for the quick answers, looks good, i ll try it out ! and oh boy ! you are right, i actually also calculated the amounts of samples that i need to wait completly wrong.. because you have 44100 per second right ? and if i wanted to wait for 1/4 of a musical beat that would of course not(!) be 44100/4.. because one beat has a different length regarding to temp... ;-)

Show more comments

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

11 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

Related Questions

GameObjects not syncing with music 1 Answer

Spawning based on Music 1 Answer

How to make a loading feedback for big GameObjects loading? 2 Answers

Inconsistent results with music and synchronization 2 Answers

AudioSource and AudioSettings help needed to keep perfect sync between music audio loops. 0 Answers


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