In a fast-paced multiplayer game I'm working on, there is an issue with the interpolation algorithm. You can see it clearly in the image below.
Cyan: Local position when a packet is received
Red: Position received from packet (goal)
Blue: Line from local position to goal when packet is received
Black: Local position every frame
As you can see, the local position seems to oscillate around the goals instead of moving between them smoothly. Here is the code:
// local transform position when the last packet arrived. Will lerp from here to the goal
private Vector3 positionAtLastPacket;
// location received from last packet
private Vector3 goal;
// time since the last packet arrived
private float currentTime;
// estimated time to reach goal (also the expected time of the next packet)
private float timeToReachGoal;
private void PacketReceived(Vector3 position, float timeBetweenPackets)
{
positionAtLastPacket = transform.position;
goal = position;
timeToReachGoal = timeBetweenPackets;
currentTime = 0;
Debug.DrawRay(transform.position, Vector3.up, Color.cyan, 5); // current local position
Debug.DrawLine(transform.position, goal, Color.blue, 5); // path to goal
Debug.DrawRay(goal, Vector3.up, Color.red, 5); // received goal position
}
private void FrameUpdate()
{
currentTime += Time.deltaTime;
float delta = currentTime/timeToReachGoal;
transform.position = FreeLerp(positionAtLastPacket, goal, currentTime / timeToReachGoal);
// current local position
Debug.DrawRay(transform.position, Vector3.up * 0.5f, Color.black, 5);
}
/// <summary>
/// Lerp without being locked to 0-1
/// </summary>
Vector3 FreeLerp(Vector3 from, Vector3 to, float t)
{
return from + (to - from) * t;
}
Any idea about what's going on?