Jittering problem while moving objects

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.

using UnityEngine;

public class ObjectController : MonoBehaviour
{
    private const float DeltaTime = 0.02f;
    private float Position = 0f;
    private float TargetPosition = 0f;
    private float MovingSpeed = 0f;

    void Start()
    {

    }

    void Update()
    {
        Position = transform.position.x;
        if (MovingSpeed > 0)
        {
            Move();
        }
    }

    void Move()
    {
        transform.position = Vector3.MoveTowards(transform.position, new Vector3(TargetPosition, transform.position.y, transform.position.z), MovingSpeed * Time.deltaTime);
    }

    public void SetTargetPosition(float targetPosition)
    {
        MovingSpeed = (targetPosition - Position) / DeltaTime;
        TargetPosition = targetPosition;
    }
}

Any comment?

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?

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:

private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private float syncStartPosition = 0f;
private float syncEndPosition = 0f;

void Update() {
    Move();
}

void Move()
{
    syncTime += Time.deltaTime;
    transform.position = Vector3.Lerp(new Vector3(syncStartPosition, transform.position.y, transform.position.z), new Vector3(syncEndPosition, transform.position.y, transform.position.z), syncTime / syncDelay);
}

public void SetTargetPosition(float position)
{
    syncTime = 0f;
    syncDelay = Time.time - lastSynchronizationTime;
    lastSynchronizationTime = Time.time;

    syncStartPosition = transform.position.x;
    syncEndPosition = pos;
}

And the camera follow script:

private GameObject Target;
private float SmoothSpeed = 0.125f;

private void LateUpdate()
{
    if (Target)
    {
        transform.position = Vector3.Lerp(transform.position, new Vector3(Target.transform.position.x - 10f, 1.6f, 0f), SmoothSpeed);
    }
}

Problem exists:
pessimisticimmensebluewhale