I know this question has been asked a number of times, but the solution is not as easy as it seems. I am pretty sure I have done almost everything, but it STILL dosen’t work. The solution in the other posts have been replace transform.position/rotation to realPosition/rotation. But that did not work for me. Any ideas?
Here is my code:
using UnityEngine;
using System.Collections;
public class NetworkCharacter : Photon.MonoBehaviour {
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
if(anim == null) {
Debug.LogError ("ZOMG, you forgot to put an Animator component on this character prefab!");
}
}
// Update is called once per frame
void Update () {
if( photonView.isMine ) {
// Do nothing -- the character motor/input/etc... is moving us
}
else {
transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 0.1f);
}
}
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);
stream.SendNext(anim.GetFloat("Speed"));
stream.SendNext(anim.GetBool("Jumping"));
}
else {
// This is someone else's player. We need to receive their position (as of a few
// millisecond ago, and update our version of that player.
realPosition = (Vector3)stream.ReceiveNext();
realRotation = (Quaternion)stream.ReceiveNext();
anim.SetFloat("Speed", (float)stream.ReceiveNext());
anim.SetBool("Jumping", (bool)stream.ReceiveNext());
}
}
}