Hello,
Please focus on the commented lines in the code.
The code below is used on the client and on the authoritative server.
It works - player send RPC, server moves the transform and sends data back to client (it should be optimized though).
I want to serialize player input instead of sending it via RPC, but when I put the data in the stream I get data only on the client end. Server is not receiving any!!
This part is not executed on the client:
print (“===========================”+F);
If you take a look at the commented code - it is not working - //Debug.Log (failF); - on server it is always 0.
I have tried many many variants, including disabling the RPC at all.
One other question - should I use FixedUpdate or Update - I have read the difference, but I am not sure which one is better.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
private float speed = 5f;
private float turnspeed = 100f;
private bool mine = false;
private float vertical = 0f;
private float horizontal = 0f;
//private float failF = 0f;
void FixedUpdate(){
if(Network.isClient mine == false){
if(Connect.PlayerId==name) mine = true;
}
//Debug.Log (failF);
if(Input.anyKey mine == true){
SendThisInput();
}
}
void SendThisInput(){
vertical = Input.GetAxis("Vertical");
horizontal = Input.GetAxis("Horizontal");
if((vertical!=0)||(horizontal!=0)) {
networkView.RPC("handlePlayerInput",RPCMode.Server,Network.player,vertical, horizontal);
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
if (stream.isWriting)
{
Vector3 pos = transform.position;
stream.Serialize(ref pos);
Quaternion rot = transform.rotation;
stream.Serialize(ref rot);
//float F = 123f;
//print ("==========================="+F);
//stream.Serialize(ref F);
}
else
{
Vector3 rpos = Vector3.zero;
stream.Serialize(ref rpos);
transform.position = rpos;
Quaternion rrot = Quaternion.identity;
stream.Serialize(ref rrot);
transform.rotation = rrot;
//float receivedF = 0f;
//stream.Serialize(ref receivedF);
//failF = receivedF;
}
}
[RPC]
void handlePlayerInput(NetworkPlayer player, float vertical, float horizontal) {
if(Network.isServer){
transform.Translate(Vector3.forward * vertical * speed * Time.deltaTime);
transform.Rotate(Vector3.up, horizontal*turnspeed*Time.deltaTime);
}
}
}