Алгоритмы:Интерполяция
Материал из GreatGalaxy
// GGTeam (c) 2017
// Глобальные переменные
int _sendInterval = 10; // Частота отправки позиции на сервер (раз в сек)
float inter_timer_position_start = 0;
float inter_timer_position;
Vector3 newPosition;
Vector3 oldPosition;
float interPosFrameOne = 0;
float interPosFrameAll = 0;
// Инициализация
void Start()
{
oldPosition = transform.position;
newPosition = transform.position;
inter_timer_position_start = Time.time;
inter_timer_position = Time.deltaTime;
interPosFrameAll = 999;
}
// Пришла новая позиция
public void receivedPosition(Vector3 position)
{
inter_timer_position = Time.time - inter_timer_position_start; // считаем временную разницу между скоростью отправки координат
float t = 1 / (float)_sendInterval;
if (inter_timer_position > t) inter_timer_position = t; // Задержка перед долгой остановкой, не может быть дольше _sendInterval сек.
interPosFrameOne = 1 / inter_timer_position;
interPosFrameAll = 0;
inter_timer_position_start = Time.time;
oldPosition = transform.position;
newPosition = position;
}
// Каждый кадр
void Update()
{
if (interPosFrameAll <= 1)
{
interPosFrameAll += (interPosFrameOne * Time.deltaTime);
transform.position = Vector3.Lerp(oldPosition, newPosition, interPosFrameAll);
}
}