I have simple multiplayer code set up in which players can join a network server. This is authored so the server controls their location. The rotation of each is not controlled by the server.
It works fine for the server. The servers rotation updates on the clients as well. However the client rotation doesnt. The clients only rotate on their local computer. The other client objects do not rotate.
Full player code below:
var moveSpeed = 6.0;
var jumpSpeed = 12.0;
var verticalSpeed = 0.0;
var Grounded = 0;
var rotateSpeed = 3.0;
var grav : float = 1;
var rotRead : Quaternion = Quaternion.Euler(0,0,0);
var rot: Quaternion = Quaternion.Euler(0,0,0);
var test1 : int = 0;
public var owner : NetworkPlayer;
//Last input value, we're saving this to save network messages/bandwidth.
private var lastCl_InH : float=0;
private var lastCl_InV : float=0;
private var lastCl_InRotH : float=0;
private var lastCl_InJump : boolean;
//The input values the server will execute on this object
private var Current_InH : float = 0;
private var Current_InV : float = 0;
private var Current_InRotH : float = 0;
private var Current_InJump : boolean ;
function Awake()
{
// We are probably not the owner of this object: disable this script.
// RPC's and OnSerializeNetworkView will STILL get trough!
// The server ALWAYS run this script though
if(Network.isClient)
{
enabled=false; // disable this script (this enables Update());
}
}
@RPC
function SetPlayer(player : NetworkPlayer) //called from NetworkServer_Spawner2, newPlayer (the one joining) is passed into here
{
owner = player; //< why? convience and below functions
if(player==Network.player) // if the newPlayer = this client
{
//Hey thats us! We can control this player: enable this script (this enables Update());
enabled=true;
}
}
function ApplyJump(controller : CharacterController)
{
if (controller.isGrounded)
{
// We are grounded
if (Current_InJump == true)
{
verticalSpeed = jumpSpeed;
}
}
}
function FixedUpdate()
{
if(owner!=null Network.player==owner)
{
transform.Rotate(0, Input.GetAxis ("Mouse X") * rotateSpeed, 0);
rot = transform.rotation;
rotRead = rot;
test1 += 1;
//Only the client that owns this object executes this code
var HInput : float = Input.GetAxis("Horizontal");
var VInput : float = Input.GetAxis("Vertical");
var JumpInput : boolean = Input.GetButton("Jump");
//Is our input different? Do we need to update the server?
if(lastCl_InH!=HInput || lastCl_InV!=VInput || lastCl_InJump!=JumpInput ){
lastCl_InH= HInput;
lastCl_InV = VInput;
lastCl_InJump = JumpInput;
if(Network.isServer){
//Too bad a server can't send an rpc to itself using "RPCMode.Server"!...bugged :[
SendMovementInput(HInput, VInput, JumpInput);
}else if(Network.isClient){
SendMovementInput(HInput, VInput, JumpInput); //Use this (and line 64) for simple "prediction"
networkView.RPC("SendMovementInput", RPCMode.Server, HInput, VInput, JumpInput);
}
}
}
//Only run this on the server
if(Network.isServer || Network.player==owner)
{
//Only the I can move the cube!
/*
var moveDirection : Vector3 = new Vector3(-1*Input.GetAxis("Vertical"), 0,Input.GetAxis("Horizontal"));
transform.Translate(speed * moveDirection * Time.deltaTime);//now really move!
*/
var controller : CharacterController = GetComponent(CharacterController);
//Grounded = 1 * ( controller.isGrounded ? 1 : 0);
if (controller.isGrounded)
{
Grounded = 1;
}
else
{
Grounded = 0;
}
// Apply gravity
//verticalSpeed -= grav;
if(Grounded == 0) {verticalSpeed -= grav;}
else {verticalSpeed = 0;}
//ApplyAnimation(controller);
ApplyJump(controller);
//moveDirection *= moveSpeed;
// Rotate the controller
//transform.Rotate(0, InRotH * rotateSpeed, 0);
// Move the controller
moveDirection = Vector3(Current_InH / 2 , 0, Current_InV);
moveDirection = transform.TransformDirection(moveDirection);
var movement = (moveDirection * moveSpeed) + Vector3(0, verticalSpeed, 0) ;
controller.Move(movement * Time.deltaTime);
}
}
@RPC
function SendMovementInput(HInput : float, VInput : float, JumpInput : boolean)
{
//Called on the server
Current_InH = HInput;
Current_InV = VInput;
Current_InJump = JumpInput;
}
function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)
{
if (stream.isWriting){
//Executed on the owner of the networkview; in this case the Server
//The server sends it's position over the network
var pos : Vector3 = transform.position;
stream.Serialize(pos);//"Encode" it, and send it
stream.Serialize(rot);//"Encode" it, and send it
stream.Serialize(test1);//"Encode" it, and send it
}
else
{
//Executed on the others; in this case the Clients
//The clients receive a position and set the object to it
var posReceive : Vector3 = Vector3.zero;
var rotReceive : Quaternion = Quaternion.Euler(0,0,0);
stream.Serialize(posReceive); //"Decode" it and receive it
stream.Serialize(rotReceive); //"Decode" it and receive it
transform.position = Vector3.Lerp(transform.position, posReceive, 1); //"lerp" to the posReceive by 90%
transform.rotation = rotReceive;
}
}
Ive tried alot of different methods… but still can’t make it work.