• 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 ionside · Nov 04, 2015 at 11:46 PM · timing-issue

Cannot figure out why my BPM is slightly off

Timing my script with a stop watch, I see it is slightly out of sync. And can't figure why. Is there anything I should be doing to keep it correct? I've attached the whole (unfinished) script in here, to put it all on context.

 public class YPop : MonoBehaviour {
     public float startTime = 1.0f;
     public float moveAmount = 0.25f;
     public float startHeight = -0.5f;
     // public Collider Trigger;
     public float beatTime = 60.0f;
     float timer;
 
     float rnd;
     float oTime;
     bool UpDown = false; //up true, down false
 
     void Start(){
         transform.position = new Vector3(transform.position.x, startHeight + transform.parent.position.y, transform.position.z);
 
     }
     void Update () {
         if(timer >= 0.0f)
             timer -= Time.deltaTime;
 
         else{
             StartCoroutine(WaitToPop(startTime));
             timer = (60f / beatTime)*0.5f;
         }
 
         if(transform.position.y > startHeight + transform.parent.position.y)
             transform.Translate(Vector3.down * Time.deltaTime);
 
     }
 
     IEnumerator WaitToPop(float startTime){
         rnd = Random.Range(0.0f, moveAmount);
         yield return new WaitForSeconds(startTime);
         if(UpDown){
             transform.Translate(Vector3.up * rnd);
             UpDown = false;
         }
         else{
             UpDown = true;
         }
     }
 
     void OnCollisionStay(Collision col){
 
     }
 }
Comment
Add comment · Show 2
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 Scribe · Nov 05, 2015 at 11:25 AM 1
Share

HenryStratton's answer is likely correct, though it might be worth noting that InvokeRepeating is very useful for this sort of regular execution!

avatar image ionside Scribe · Nov 05, 2015 at 11:43 AM 0
Share

Very good to know about, thank you, Scribe!

1 Reply

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

Answer by HenryStrattonFW · Nov 05, 2015 at 11:01 AM

Well the first thing that springs to my mind is that your script doesn't consider how far below 0 your timer variable is when it then resets it. It's highly unlikely that subtracting delta time will nicely land you on 0 each time, you're likely to be tiny amounts below 0 each time. Every time you then reset your timer as if it was from exactly 0, you begin to accumulate tiny sync errors with each loop of the timer.

Granted I'm not sure if this sort of error accumulation is enough to be significantly out of sync unless you're experiencing lag spikes or poor frame rate, but its something to consider. Getting around it is as simple as changing your = into a += when resetting your timer. That way the overlap below 0 is taken into account as your next loop of the timer starts with that offset.

 timer = (60f / beatTime)*0.5f; // Old
 timer += (60f / beatTime)*0.5f; // New

Like i said, might be only a minor contribution to your sync error. but worth noting, and maybe it will have a bigger effect that I'm imagining.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Issue with switch statement 1 Answer

Player 1 and 2 both have same oncollisionenter, but unity always execute oncollisionenter on player 1 first 1 Answer

How to change the position of the Gameobject in 10 second? 1 Answer

OnTriggerEnter2D is called in two different objects in the same frame. 4 Answers

Advanced C# problem: Timings incorrect in script, assigning a variable that is not updated yet in an array (If someone wants a challenge, hop on in!) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges