Hi, I’m working in a project which the player have to control remotely, an object from network, so I created a class that have some Input values, that the server need to see to change the object position like that.
This is the class of the network Object, in first time i wondered that the values would be the same in Server and Client, but the variables had not the same values:
using UnityEngine;
using System.Collections;
public class NetInput : MonoBehaviour {
public bool LUp{ get; set; }
public bool LDown{ get; set;}
public bool LLeft{ get; set;}
public bool LRight{ get; set;}
public bool Ahead{ get; set;}
public bool Back{ get; set;}
public bool Close{ get; set;}
public bool Max{ get; set;}
public bool Min{ get; set;}
public bool AutoMode{ get; set;}
// Use this for initialization
void Start () {
//print("Network Initiazed.");
print(networkView.viewID.ToString());
gameObject.name = "InputConnection";
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W)){
LUp = true;
} else {
LUp = false;
}
if(Input.GetKey(KeyCode.A)){
LLeft = true;
} else {
LLeft = false;
}
if(Input.GetKey(KeyCode.D)){
LRight = true;
} else {
LRight= false;
}
if(Input.GetKey(KeyCode.S)){
LDown = true;
} else {
LDown = false;
}
if(Input.GetKey(KeyCode.UpArrow)){
print(true);
Ahead = true;
} else {
//print(false);
Ahead = false;
}
if(Input.GetKey(KeyCode.DownArrow)){
Back = true;
} else {
Back = false;
}
if(Input.GetKey(KeyCode.C)){
Close = true;
} else {
Close = false;
}
if(Input.GetKey(KeyCode.N)){
Min = true;
} else {
Min = false;
}
if(Input.GetKey(KeyCode.M)){
Max = true;
} else {
Max = false;
}
if(Input.GetKey(KeyCode.P)){
AutoMode = true;
} else {
AutoMode = false;
}
if(Ahead){
print(Ahead);
}
if(Network.isClient){
bool []comands ={LLeft, LRight, LDown, LUp, Ahead, Back};
networkView.RPC("UpdateValues", RPCMode.Server, comands);
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
bool CLUp = false;
bool CLDown = false;
bool CLLeft = false;
bool CLRight = false;
bool CAhead = false;
bool CBack = false;
bool CClose = false;
bool CMax = false;
bool CMin = false;
bool CAutoMode = false;
if (stream.isWriting) {
CLUp = LUp;
CLDown = LDown;
CLLeft = LLeft;
CLRight = LRight;
CAhead = Ahead;
CBack = Back;
CClose = Close;
CMax = Max;
CMin = Min;
CAutoMode = AutoMode;
stream.Serialize(ref CLUp);
stream.Serialize(ref CLDown);
stream.Serialize(ref CLLeft);
stream.Serialize(ref CLRight);
stream.Serialize(ref CAhead);
stream.Serialize(ref CBack);
stream.Serialize(ref CMax);
stream.Serialize(ref CMin);
stream.Serialize(ref CClose);
stream.Serialize(ref CAutoMode);
} else {
stream.Serialize(ref CLUp);
stream.Serialize(ref CLDown);
stream.Serialize(ref CLLeft);
stream.Serialize(ref CLRight);
stream.Serialize(ref CAhead);
stream.Serialize(ref CBack);
stream.Serialize(ref CMax);
stream.Serialize(ref CMin);
stream.Serialize(ref CClose);
stream.Serialize(ref CAutoMode);
LUp = CLUp;
LDown = CLDown;
LLeft = CLLeft;
LRight = CLRight;
Ahead = CAhead;
Back = CBack;
Close = CClose;
Max = CMax;
Min = CMin;
AutoMode = CAutoMode;
}
}
[RPC]
void UpdateValues(bool []comandos){
LLeft = comandos[0];
LRight = comandos[1];
LDown = comandos[2];
LUp = comandos[3];
Ahead = comandos[4];
Back = comandos[5];
}
}