我有Node.js服务器和Unity客户端。服务器端每40-50毫秒发送一个新的玩家坐标(取决于负载),但是对于没有滞后的移动来说它太多了。
我的错误版本伪代码:
GameObject player;
serverListener += TakeCoordFromServer;
Vector3 oldCoord = Vector.zero;
Vector3 newCoord = Vector.zero;
float deltaServerTime = 0.0f;
float estimatedSpeedMove = 0.0f;
float lastTime = 0.0f;
Vector3 lastPosition = Vector.zero;
void TakeCoordFromServer(Vector3 _newCoord) //every 40-50 ms
{
this.oldCoord = this.newCoord;
this.newCoord = _newCoord;
this.lastPosition = this.player.transform.position;
this.deltaServerTime = Time.time - this.lastTime;
this.lastTime = Time.time;
this.estimatedSpeedMove = Vector3.Distance(this.lastPosition, this.NewPosition.position) / deltaServerTime;
}
Update () {
this.player.transform.position = Vector3.MoveTowards(this.lastPosition, this.newCoord, Time.deltaTime * estimatedSpeedMove);
}
请帮助我移动没有颤抖的玩家