Unity3d 光子2d游戏多人滞后

Unity3d 光子2d游戏多人滞后,unity3d,2d,multiplayer,photon,Unity3d,2d,Multiplayer,Photon,我有一个使用光子的2D多人游戏,所有的动作都是由刚体2D完成的。当两名球员都连接在一起时,我可以看到对手的动作,但它们并不流畅上次更新时间变量设置为0.25 using UnityEngine; using System.Collections; public class NetworkCharacter : Photon.MonoBehaviour { Vector3 realPosition = Vector3.zero; Quaternion realRotation

我有一个使用光子的2D多人游戏,所有的动作都是由刚体2D完成的。当两名球员都连接在一起时,我可以看到对手的动作,但它们并不流畅上次更新时间变量设置为0.25

using UnityEngine;
using System.Collections;

public class NetworkCharacter : Photon.MonoBehaviour {

    Vector3 realPosition = Vector3.zero;
    Quaternion realRotation = Quaternion.identity;
    public float lastUpdateTime = 0.1f;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void FixedUpdate () {
         if(photonView.isMine){
            //Do nothing -- we are moving
        }
        else {
            transform.position = Vector3.Lerp(transform.position, realPosition, lastUpdateTime);
            transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, lastUpdateTime);
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
        if(stream.isWriting){
            //This is OUR player.We need to send our actual position to the network
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation); 
    }else{
            //This is someone else's player.We need to receive their positin (as of a few millisecond ago, and update our version of that player.
            realPosition = (Vector3)stream.ReceiveNext();
            realRotation = (Quaternion)stream.ReceiveNext();
        }

    }
}

[Lerp]通过分数t在from和to之间插值。这最常用于沿两个端点之间的直线找到一个点(例如,在这些点之间逐渐移动对象)。此分数被钳制到范围[0…1]。当t=0从返回时。当t=1时,返回到。当t=0.5时,返回从和到之间的中间点。

因此基本上
Vector3.Lerp
返回以下内容:

from + ((to - from) * t)
如您所见,通过使用相同的值调用
Lerp
,它总是返回相同的值,您应该使用的是和


我不熟悉光子,但我认为增加
lastUpdateTime
应该会使它看起来更平滑。尽管可能最好使用的代码来自的插值部分,因为它可以更好地处理慢速网络。
transform.position = Vector3.MoveTowards(transform.position, realPosition, lastUpdateTime * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, realRotation, lastUpdateTime * Time.deltaTime);