Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Extrapolation of Rotation, Bug Help

Discussion in 'Multiplayer' started by unity_maxxsurabaya, Mar 10, 2023.

  1. unity_maxxsurabaya

    unity_maxxsurabaya

    Joined:
    Aug 15, 2022
    Posts:
    2
    Hi, I'm trying to create an Extrapolation for Rotation, but seems to hit a wall. I'm trying to create a Multiplayer game which is using heavy construction vehicles such as dump truck, backhoe, wheel loader, etc. And cannot seem to get pass my rotation bug for the mechanical parts.

    Here's my script:


    Code (CSharp):
    1.  
    2.             Vector3 difEuler = state0.AngleRotation - state1.AngleRotation;
    3.             Quaternion difRot = state0.OriginRotation * Quaternion.Inverse(state1.OriginRotation);
    4.             // float distance = Vector3.Distance(state0.AngleRotation, state1.AngleRotation);
    5.             float angle = Vector3.Angle(state0.AngleRotation, state1.AngleRotation);
    6.  
    7.             if (Mathf.Approximately(angle, 0) || Mathf.Approximately(timeDif, 0))
    8.             {
    9.                 mechanicalPart.rotation = state0.OriginRotation;
    10.                 return;
    11.             }
    12.  
    13.             // float speedEul = distance / timeDif;
    14.             float speedRot = angle / timeDif;
    15.  
    16.             if(namaAlat == "loaderFrame")
    17.             {
    18.                 Debug.Log("Euler State: " + state0.AngleRotation + ", Rotation: " + state1.OriginRotation);
    19.                 Debug.Log("Angle: " + angle + ", SpeedRot: " + speedRot);
    20.             }
    21.  
    22.             difEuler = difEuler.normalized;
    23.             difRot = difRot.normalized;
    24.  
    25.             Vector3 expectedEuler = state0.AngleRotation + (difEuler * extrapolationLength * speedRot);
    26.             expectedEuler = new Vector3(expectedEuler.x > 180 ? expectedEuler.x - 360 : expectedEuler.x,
    27.                                         expectedEuler.y > 180 ? expectedEuler.y - 360 : expectedEuler.y,
    28.                                         expectedEuler.z > 180 ? expectedEuler.z - 360 : expectedEuler.z);
    29.             // Quaternion expectedRot = Quaternion.Euler(expectedEuler);
    30.             mechanicalPart.eulerAngles = Vector3.Lerp(mechanicalPart.eulerAngles, expectedEuler, Time.deltaTime * speedRot);
    I tried to use Vector3 or Quaternion but it's still getting a bug, either it's too fast or the movement is really laggy. And I tried to debug it and found out it maybe cause by its speed, either it became too fast and got 40 or higher float, or being too slow which I got .6 to .3 float. The thing is I use the same extrapolation for the walking / riding movement of my vehicles and it worked just fine, but it's not working for the mechanical part.

    For the time difference between each stat I use this code
    Code (CSharp):
    1. float timeDif = Convert.ToSingle(bufferedStates[0].TimeStamp - bufferedStates[1].TimeStamp) / 1000.0f;
    I also tried using this one but it still remained a bit buggy
    Code (CSharp):
    1. dt = (state2.Timestamp - state0.timeStamp) / (state1.timeStamp - state0.timeStamp);