我编写了用于插值和移动的脚本,但它的工作效果不够好,特别是使用对象速度乘以延迟时间(syncVelocity * syncDelay)的预测,导致玩家在停止移动时返回到预测之前的位置。我听说可以使用unity navmesh类进行改进,但我不知道该怎么做。知道如何改进该脚本吗?
链接到文档:http://docs.unity3d.com/Documentation/ScriptReference/NavMesh.html
public class Synchronization : MonoBehaviour {
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
// Update is called once per frame
void Update () {
if (!networkView.isMine){
SyncedMovement();
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 syncPosition = Vector3.zero;
Vector3 syncVelocity = Vector3.zero;
if (stream.isWriting)
{
syncPosition = transform.position;
stream.Serialize(ref syncPosition);
syncVelocity = GetComponent<CharacterController>().velocity;
stream.Serialize(ref syncVelocity);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncVelocity);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncStartPosition = transform.position;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
}
}
void SyncedMovement(){
syncTime += Time.deltaTime;
transform.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
}
}