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 /
avatar image
0
Question by LaowPing · Oct 22, 2016 at 10:36 PM · c#objectmusicsynchronizationrhythm

GameObjects not syncing with music

I have a rhythm game I'm trying to make where obstacles fade into view based on the time of the music. It works at first but after a few times the objects start getting desynced, with each one appearing slightly later than it should and I have no idea why. The scripts look like this.

 using UnityEngine;
 using System.Collections;
 
 public class Obstical_Script : MonoBehaviour {
 
     AudioSource fmusic;
     
     float start, interval;
     int chooseChild;
     void Start ()
     {
         fmusic = GetComponent<AudioSource>();
         interval = 0;
         start = 1.5f;
     }
     
     void Update () 
     {
         if (fmusic.time >= start)
         {
             Debug.Log(fmusic.time);
             transform.GetChild(0).GetComponent<ChildObsticalsScript>().fading = true;
             interval += 1f;
             chooseChild++;
             if (chooseChild == 8)
                 chooseChild = 0;
            
         }
     }
 }
 

 using UnityEngine;
 using System.Collections;
 
 public class ChildObsticalsScript : MonoBehaviour {
 
     public bool fading;
     bool fadeOut;
     Color defaultColor;
     float alpha;
     int wait;
 
     void Start () 
     {
         fading = false;
         fadeOut = false;
         alpha = 0;
         wait = 5;
 
         gameObject.GetComponent<SpriteRenderer>().color = new Color(gameObject.GetComponent<SpriteRenderer>().color.r, 
             gameObject.GetComponent<SpriteRenderer>().color.g, gameObject.GetComponent<SpriteRenderer>().color.b, 0);
 
         defaultColor = gameObject.GetComponent<SpriteRenderer>().color;
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(fading)
         {
             if (alpha < 1f && !fadeOut)
             {
                 alpha += 1f * Time.deltaTime;
                 gameObject.GetComponent<SpriteRenderer>().color = new Color(gameObject.GetComponent<SpriteRenderer>().color.r,
                 gameObject.GetComponent<SpriteRenderer>().color.g, gameObject.GetComponent<SpriteRenderer>().color.b, alpha);
             }
 
             if (alpha >= 1f && !fadeOut)
             {
                 gameObject.GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, 1);
                 gameObject.GetComponent<BoxCollider2D>().enabled = true;
                 alpha = 0;
                 fadeOut = true;
             }
         }
         if (fadeOut)
         {
             --wait;
             if (wait <= 0)
             {
                 gameObject.GetComponent<BoxCollider2D>().enabled = false;
                 gameObject.GetComponent<SpriteRenderer>().color = defaultColor;
                 wait = 5;
                 fading = false;
                 fadeOut = false;
             }
         }
     }
 }
 


The first half is a script I have on a parent object. It chooses which object it wants to fade in. The second part is the object that's supposed to fade in. I've checked both the interval and fmusic.time with Debug.Log and they both work fine yet the objects will still start to slowly desync with the music and I'm not sure why. I've also tried changing fmusic to a variable called time which I increment with Time.DeltaTime and I get the same problem.

Comment
Add comment · Show 1
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 LaowPing · Oct 22, 2016 at 09:50 PM 0
Share

btw the second script begins after line 30

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LaowPing · Oct 24, 2016 at 04:00 PM

Alright, so I more or less fixed the problem. I changed interval from 1f to .924f. It seems I was making the assumption that every down beat was 1 second apart. Silly me.

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

C# Fourier transform rhytm game implementation? 1 Answer

Synchronization object animation motion (NavMeshAgent) 0 Answers

Multiple Cars not working 1 Answer

Rhythm Game - Using Midi Notes to trigger a coloured note on screen. 1 Answer

Distribute terrain in zones 3 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