Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Any difference between FixedUpdate and WaitForFixedUpdate?

Discussion in 'Scripting' started by Silly_Rollo, Mar 2, 2014.

  1. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    501
    I have some code that is behaving far differently in a standalone build than in the editor. Normally you would expect this is because of frame rate differences but I'm yielding on a WaitForFixedUpdate
    Code (csharp):
    1.  
    2. private WaitForFixedUpdate _waitForFixedUpdate = new WaitForFixedUpdate();
    3.  
    4. private IEnumerator ApplyImpact(float endTime) {
    5.         while (Time.time < endTime) {
    6.             _char.LastDamage.ImpactLocation.AddForce(_char.LastDamage.Impact * Time.timeScale, ForceMode.Impulse);
    7.             yield return _waitForFixedUpdate;
    8.         }
    9. }
    10.  
    This is to apply an impact to a ragdoll. In the editor it works perfectly but in a standalone build the character flies away as if struck by a much larger impact. I inserted debug statements and I can confirm the standalone build is receiving the same number of add force inputs of the same velocity but the end result is very different. Any ideas why?
     
  2. JaffaCake

    JaffaCake

    Joined:
    Jan 19, 2014
    Posts:
    16
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  4. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    501
    Yep I saw the doc page. Those indicate that in my usage there should be no appreciable difference. Any ideas why the standalone and editor have different behavior for WaitForFixedUpdate? Time.time is sometimes affected by vsync (though you'd think it shouldn't be) but if anything that should cause less force to be added.
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  6. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    501
    I'm doing more testing and it looks like it actually doesn't have anything to do with trying to apply the impact over several frames or how fixed update is running. I'm not certain why but fairly small AddForce values even with a single call occasionally produce absurd velocities. I ran it through an automated test and similar tiny impact values like (-0.8, 0.1, 1.5) hitting objects with a mass of 5-8 can sometimes cause the object to rocket away at a Y velocity in the thousands. I've simplified the scene and I'm not sure where it is going awry. My physics timestep and gravity are set to default.