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 /
  • Help Room /
avatar image
0
Question by psznm · Nov 16, 2021 at 08:28 PM · updatetimetestingtest

Handling inaccuracies between update loop and time in playmode tests.

Hello, I have very simple test where I need to move back and forth and assert position. However after couple of iterations, the inaccuracies between time and update loop always make the test fail. I even tried adding offset to each assert, so that the inaccuracies would be accounted for, but without success.

It also happens in both cases when my movement logic is in Update and when it is in FixedUpdate

The issue also comes up in earlier iterations when I change time scale like this, but it does happen even without it (not as much with lower time scales and FixedUpdate).

 [ OneTimeSetUp ] 
 public  void  OneTimeSetUp () { 
     Application.targetFrameRate = 60; 
     Time.timeScale = 10.0f;
 } 

I tried googling but could not find anything related. How can I deal with this issue? Relevant code:

     var maxToleranceDiff = Time.fixedDeltaTime * movement.Speed + 0.001f;
     Debug.Log("Tolerance: " + maxToleranceDiff.ToString());
     var offBy = 0f;
     for (int i = 0; i < 100; i++)
     {
         movement.Direction = new Vector3(-1, 0, 0);
         yield return new WaitForSeconds(1);
         Assert.AreApproximatelyEqual(
             -1 * movement.Speed + offBy, 
             movement.transform.position.x,
             maxToleranceDiff
         );
         offBy = -1 * movement.Speed - movement.transform.position.x;
     
         movement.Direction = new Vector3(1, 0, 0);
         yield return new WaitForSeconds(1);
         Assert.AreApproximatelyEqual(
             0f - offBy,
             movement.transform.position.x,
             maxToleranceDiff
         );
         offBy = movement.transform.position.x;
     }



Edit: I came to a solution where instead of WaitForSeconds I used WaitForFixedUpdate for N times and then counted with fixedDeltaTime * N.


My conclusion is that having the logic inside Update loop is very bad for testing because it may be impossible to handle the inacurracies. And FixedUpdate loop, while it may be fine in normal time scales, it will fail in sped up tests if mixed with yielding for seconds because you may miss some updates due to performance reasons and thus assert "too early".

After some testing I found out that WaitForSeconds(1) can easily take 2 seconds and thus be 1 second late. Takeaway is: do not use WaitForSeconds if you need something to be exact.

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

0 Replies

· Add your reply
  • Sort: 

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

177 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 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 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 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 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 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

Failed to get a line number for unity test method. So please find it in opened file in external editor 1 Answer

Different jump height 1 Answer

Maximum Delta Time 0 Answers

Unity TestRunner doesnt show tests as "Run" 0 Answers

Update Unity AI Text every x minutes 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