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;
}
}