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

Physics.Simulate problems

Discussion in 'Scripting' started by allencook200, Jan 15, 2021.

  1. allencook200

    allencook200

    Joined:
    Oct 2, 2020
    Posts:
    178
    Code (CSharp):
    1.             var CurrentPosition = RigidBody.transform.position;
    2.  
    3.             Physics.Simulate(0.02f);
    4.  
    5.             RigidBody.transform.position = CurrentPosition;
    This piece of code runs every few seconds in Update(). It's just a small portion of a bigger project I'm working on, but it causes the player to jitter, whenever it runs. Why exactly does this happen and how would I fix it?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This is running one extra frame of the physics engine every time you call it.

    That seems ... odd.
     
  3. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    What are you trying to achieve, obviously you are adding some strange additional simulation step.
    Physics is running by itself anyway no need to simulate it inside Update. (depends on what you are doing).
     
  4. allencook200

    allencook200

    Joined:
    Oct 2, 2020
    Posts:
    178
    As I said, smaller piece of a much larger project. That was a dumb example for me to give.

    When doing clientside reconciliation inside unity, would I just be better off doing the whole re-simulation in another scene? I've tried like 20 different ways to rewind and re-simulate to correct the physics from the server value, but it's extremely jittery with whatever I try.

    I feel like the only way to do this is just inside a different scene running it's own physics?
     
  5. allencook200

    allencook200

    Joined:
    Oct 2, 2020
    Posts:
    178
    Yeah that was a dumb example for me to give. Check out the reply I gave to Kurt and let me know what you think about doing that in a different scene. More input the better
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    The reason it's jittery is because Update (and your game) does not run at a consistent framerate. Therefore, you are now simulating Unity's fixed-timestep physics at a non-fixed time interval.
     
  7. allencook200

    allencook200

    Joined:
    Oct 2, 2020
    Posts:
    178
    I tried it in fixedupdate and lateupdate, it did not help.

    I even tried doing it without a network, and just taking the last 5 predicted movements from my array, teleporting my character back to the first of the 5 values, and re-simulating all of the 5 movements. The rigidbody jittered whenever I did this.
     
  8. allencook200

    allencook200

    Joined:
    Oct 2, 2020
    Posts:
    178
    Code (CSharp):
    1.         if (PerformSimulation == true)
    2.         {
    3.  
    4.             RigidBody.position = (PastNetworkingStuffClass.NetworkingPlayerPosition[PastNetworkingStuffClass.NetworkingPlayerPosition.Count - 5]);
    5.  
    6.             for (int i = PastNetworkingStuffClass.NetworkingPlayerPosition.Count - 5; i < PastNetworkingStuffClass.NetworkingPlayerPosition.Count; i++)
    7.             {
    8.  
    9.                 RigidBody.velocity = PastNetworkingStuffClass.NetworkingPlayerVelocity[i] * Speed;
    10.                 Physics.Simulate(0.02f);
    11.  
    12.             }
    13.  
    14.             PerformSimulation = false;
    15.  
    16.         }
    Here is an experiment I made. It does NOT use networking, and just grabs the past predicted inputs, and re-simulates them, so we don't have to worry about any networking variables going wrong for this experiment. Whenever this is called, there is a very clear jitter. This is in update() by the way.
     
  9. allencook200

    allencook200

    Joined:
    Oct 2, 2020
    Posts:
    178
    I then had another idea, to only simulate an invisible rigidbody with no camera attached to it, and then have the real player lerp to the invisible rigidbody's position, and I did something like this:

    Code (CSharp):
    1.         if (PerformSimulation == true)
    2.         {
    3.  
    4.             RigidBody.isKinematic = true;
    5.  
    6.             for (int i = PastNetworkingStuffClass.NetworkingPlayerPosition.Count - 5; i < PastNetworkingStuffClass.NetworkingPlayerPosition.Count; i++)
    7.             {
    8.  
    9.                 Physics.Simulate(0.02f);
    10.  
    11.             }
    12.  
    13.             RigidBody.isKinematic = false;
    14.  
    15.             PerformSimulation = false;
    16.  
    17.         }
    (also in update), and this produces the same jitter. All I'm trying to do is reconciliate/"re-simulate" my player's movements all during a single frame. If I remove physics.simulate, it does not jitter. So we can conclude that physics.simulate is causing the jitter, I just don't know why. Any help is appreciated.