Alright, I saw a number of scripts for players in authoritative servers. My code however has a little bit different structure than most of the ones I saw.
Now let me explain what problem am I experiencing and than I’ll show my code:
I initialize the server and I can walk and do everything with the server’s player. Than I connect a client to the server. When the client is spawned the server loses control over his player - can’t rotate it, can’t move it and can do nothing. The client can move his player, but except his player, the client moves the server’s player too. The client CAN look around with the mouse, but if I start walking, the rotation gets reset. Now here’s my code, I’ll just show a little just to show the idea of my script’s mecahnism:
First we have the owner setting:
@RPC
function PlayerSetup(player : NetworkPlayer){
owner = player;
if(owner != Network.player) {
mine = false;
playerCamera.camera.enabled = false;
playerCamera.GetComponent(AudioListener).enabled= false;
}
This is called when the player is spawned.
Than we go to Update function and from here I separate the code in two parts - one for the authoritative server, that calculates all the motions, gravity, sliding and everything that is applied to the player no matter what. The other part is for the owner of the player, it takes the inputs from him:
function Update () {
//Things, that are dependant on what the user does
if(owner == Network.player) OwnerControl();
//Things like gravity, sliding off steap surfaces and others, that are applied no matter what
if(Network.isServer) ServerControl();
}
Now I’ll just show the main part of the OwnerControl so that I don’t get anyone confused:
function OwnerControl() {
button_fd = Input.GetButton("Forward");
button_bk = Input.GetButton("Backward");
button_lf = Input.GetButton("Left");
button_rt = Input.GetButton("Right");
button_duck = Input.GetButton("Duck");
//Rotation should be done on the player owner's machine
if(!weaponManager.turretMenu !Menu.state){
transform.Rotate(0, Input.GetAxis ("Mouse X") * mouseSensitivity, 0);
}
// Sending the movement input total
if(Network.isServer){
if(!Menu.state)
CalculateMove(button_fd, button_lf, button_bk, button_rt, button_duck, sprinting);
else CalculateMove(false, false, false, false, false, false);
}
if(Network.isClient){
if(!Menu.state)
networkView.RPC("CalculateMove", RPCMode.Server, button_fd, button_lf, button_bk, button_rt, button_duck, sprinting);
else networkView.RPC("CalculateMove", RPCMode.Server, false, false, false, false, false, false);
}
}
CalculateMove is the function, that calculates the movement variables, that are later used to move the player.
The function, that I won’t show is the ServerControl, which is used to actually move the player, apply gravity and everything. It’s using the CharacterController.Move function.
Can you help me out and tell me where am I wrong. I’m sure I’m missing something, but I don’t have a clue. The problem is that everything looks fine to me.