Hello Guys! I am making a multiplayer third person game. I have a character prefab and it consists of an empty game object with the actual character and third person camera as children. I am able to instantiate it for all players but it won't synch correctly. I am using the code below for synchronizing the character but I see only my character in the environment and not the other and when I inspect it in the inspector, its actually there but not at the spawn position. Once i got it to work but it was not synchronizing correctly, animations were looking all right but no position synchronization. Any help would be much appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Network_Character : Photon.MonoBehaviour {
Vector3 realPosition;
Quaternion realRotation = Quaternion.identity;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponentInChildren<Animator> ();
}
// Update is called once per frame
void Update () {
if (photonView.isMine) {
// do nothing
}
else {
transform.position = Vector3.Lerp (transform.position, realPosition, Time.deltaTime * 5f);
transform.rotation = Quaternion.Lerp (transform.rotation, realRotation, Time.deltaTime * 5f);
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
Debug.Log ("On Photon");
if (stream.isWriting) {
// This is our player sending data so we need to send data to network
stream.SendNext (transform.position);
stream.SendNext (transform.rotation);
stream.SendNext (anim.GetFloat("Forward"));
stream.SendNext (anim.GetFloat ("Turn"));
stream.SendNext (anim.GetFloat ("Jump"));
stream.SendNext (anim.GetFloat ("JumpLeg"));
stream.SendNext (anim.GetBool("Crouch"));
stream.SendNext (anim.GetBool("OnGround"));
}
else {
// This is someone else's so we need to update their postion and rotation to our player
realPosition = (Vector3)stream.ReceiveNext();
realRotation = (Quaternion)stream.ReceiveNext ();
anim.SetFloat ("Forward", (float)stream.ReceiveNext());
anim.SetFloat ("Turn", (float)stream.ReceiveNext());
anim.SetFloat ("Jump", (float)stream.ReceiveNext());
anim.SetFloat ("JumpLeg", (float)stream.ReceiveNext());
anim.SetBool ("Crouch", (bool)stream.ReceiveNext());
anim.SetBool ("OnGround", (bool)stream.ReceiveNext());
}
}
}