Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by Inv · Feb 09, 2016 at 01:18 PM · audiotimeaudiosourcerhythm

How to Sync GameObject Creation to The Beat of a Song?

I am currently working on a simple VR rhythm game, and my main goal right now is to create a GameObject in time with the rhythm of a song currently playing. The game already knows the bpm of the song. I am not worrying about spawning the object right on the beat, but in time with the rhythm.

This is the current script I have running. The script gets close but still creates objects at a slightly quicker interval than the song's bpm. Any Advice?

     public GameObject note;
     public GameObject[] laneStart;
     public float bpm;
     private float lastTime, deltaTime, timer;
 
 
     void Start () {
         lastTime = 0f;
         deltaTime = 0f;
         timer = 0f;
     }
 
     // Update is called once per frame
     void Update () {
         //Chooses a random lane to create the note in
         int rand = Random.Range (0, 4);

         deltaTime = GetComponent<AudioSource> ().time - lastTime;
         timer += deltaTime;
 
         if (timer >= (60f/bpm)) {
             //Create the note
             ((GameObject)Instantiate (note, laneStart [rand].transform.position, laneStart [rand].transform.rotation)).GetComponent<Transform> ().parent = GetComponent<Transform> ();
             timer = 0;
         }
 
         lastTime = GetComponent<AudioSource> ().time;
     }
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

2 Replies

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

Answer by Inv · Jun 17, 2016 at 01:46 PM

Sorry for coming back to this topic so late for those who were looking for a similar answer!

The reason I was getting a slightly quicker interval than normal was because of line 24:

timer = 0;

Because I was working with timing that needed very high accuracy, the extra ~.01 seconds that was on timer when it hit that line of code brought it out of sync by a small amount. After 30 seconds of many slightly out of sync beats, I would find the notes coming in ~.25 seconds late (not good for a rhythm game). To account for the delay, I changes line 24 to something like this:

timer -= (60f/bpm);

This way when we go .01 seconds over, the timer would start at .01, accounting for the delay. Hope this helped anyone with the same problem!

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

Answer by Dashz · Mar 10, 2018 at 01:50 PM

I know you were asking, but this helped me a lot, do you know how it would be possible to make the action at each 2nd note, you know most of the songs have 2 bass and 2 claps, bass - clap - bass - clap.

How could you make so that the action happens each clap?

EDIT: After many tries (because I didn't understand the logarithm you used as I'm not very talented in logic), just by starting the "timer" at 60f/bpm would solve this, just a "quarter of compass", since we are adding a beat as a starting point. Note that I used 120f/bpm and a delay of 60f because I wanted a slower action, so the stuff happens every 2 beats.

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

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

How to mute multi-able audioclips within a single audiosource? 0 Answers

Basic setup for spatial audio 0 Answers

How to play ambisonic sound files on android 1 Answer

Playing Through a Queue/List of Audio Sources, One at a Time 1 Answer

Change Audio Mixer through script for every scene 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