Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Jittering problem while moving objects

Discussion in 'Physics' started by red_dash, May 12, 2021.

  1. red_dash

    red_dash

    Joined:
    May 12, 2021
    Posts:
    11
    I'm trying to build a simple multiplayer game with an authoritative server.

    I calculate the player positions in each frame (which runs in a 20 ms loop) and put the results into ObjectController class. Then in the update method, move the object to the target position (which is calculated in the loop) with a certain speed.

    The problem is when the main object (current player) is moving, other moving object jitters. I locked the camera to the other object (in the scene with shift+f shortcut), then the main object jitters.

    I tried all unity moving methods but the problem exists, I think the problem is with using them.

    The objects have acceleration, but in the loop period (20ms) they move in constant speed.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ObjectController : MonoBehaviour
    4. {
    5.     private const float DeltaTime = 0.02f;
    6.     private float Position = 0f;
    7.     private float TargetPosition = 0f;
    8.     private float MovingSpeed = 0f;
    9.  
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         Position = transform.position.x;
    18.         if (MovingSpeed > 0)
    19.         {
    20.             Move();
    21.         }
    22.     }
    23.  
    24.     void Move()
    25.     {
    26.         transform.position = Vector3.MoveTowards(transform.position, new Vector3(TargetPosition, transform.position.y, transform.position.z), MovingSpeed * Time.deltaTime);
    27.     }
    28.  
    29.     public void SetTargetPosition(float targetPosition)
    30.     {
    31.         MovingSpeed = (targetPosition - Position) / DeltaTime;
    32.         TargetPosition = targetPosition;
    33.     }
    34. }
    35.  
     
  2. red_dash

    red_dash

    Joined:
    May 12, 2021
    Posts:
    11
    Any comment?
     
  3. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    783
    Hi,
    You should probably begin trying to isolate what causing the problem. Is it for example the physics making the player, or the other object, jumping around, or is it a multiplayer sync problem, or is it something wrong with how the camera is following the player?
     
  4. red_dash

    red_dash

    Joined:
    May 12, 2021
    Posts:
    11
    Thanks

    I checked the position of the object, it's always increasing. I'm setting the object position each 20ms in a Timer. The timer is not accurate enough, but I lerp the object in delay time. so I changed the ObjectController method to this:

    Code (CSharp):
    1.  
    2. private float lastSynchronizationTime = 0f;
    3. private float syncDelay = 0f;
    4. private float syncTime = 0f;
    5. private float syncStartPosition = 0f;
    6. private float syncEndPosition = 0f;
    7.  
    8. void Update() {
    9.     Move();
    10. }
    11.  
    12. void Move()
    13. {
    14.     syncTime += Time.deltaTime;
    15.     transform.position = Vector3.Lerp(new Vector3(syncStartPosition, transform.position.y, transform.position.z), new Vector3(syncEndPosition, transform.position.y, transform.position.z), syncTime / syncDelay);
    16. }
    17.  
    18. public void SetTargetPosition(float position)
    19. {
    20.     syncTime = 0f;
    21.     syncDelay = Time.time - lastSynchronizationTime;
    22.     lastSynchronizationTime = Time.time;
    23.  
    24.     syncStartPosition = transform.position.x;
    25.     syncEndPosition = pos;
    26. }
    And the camera follow script:

    Code (CSharp):
    1. private GameObject Target;
    2. private float SmoothSpeed = 0.125f;
    3.  
    4. private void LateUpdate()
    5. {
    6.     if (Target)
    7.     {
    8.         transform.position = Vector3.Lerp(transform.position, new Vector3(Target.transform.position.x - 10f, 1.6f, 0f), SmoothSpeed);
    9.     }
    10. }
    Problem exists: