我有一个使用 Photon 的 2D 多人游戏,其中所有动作都是由 RigidBody 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();
}
}
}