- Home /
Question by
NovaSurfer · May 18, 2014 at 03:22 PM ·
2dmultiplayerphotonunity4.3
Photon 2d game multiplayer lags
I have a 2D multiplayer game using Photon where all the movements are made by RigidBody 2d. When both players are connected I can see my opponent movements but they are not smooth. Last Update Time variable is set to 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();
}
}
}
Comment
Ah man I'm struggling just like you. Would love to have an answer to solve this.
At runtime, play with lastUpdateTime. It affects how the Lerp is done, which in turn affects if things are smoothed (and lag a bit) or if you snap to the latest position update (not smooth but less lagging).
hey there i have the same problem. if you solved that problem can you help me please ?