- Home /
Setting rotation of another player
Hello. I am making a multiplayer game and am trying to synchronize rotation of child gameobjects of each player but am having a couple issues. For example, if player one looks up, his head tilts back locally, but not over the network. I have a network Identity and network transform so when the player looks left and right, the whole player rotates locally and over network. However, network transform child doesnt work for the head, and i'm assuming that's because i have a rigidbody on it (so it can ragdoll on death). Therefore I made this script.
using UnityEngine;
using UnityEngine.Networking;
public class NetworkSync : NetworkBehaviour {
[SerializeField]
private Transform neck;
private Quaternion neckRotation;
private Quaternion neckLastRotation;
[SerializeField]
private float lerpSpeed = 10f;
[SerializeField]
private float sendRate = 10;
void Start(){
neckLastRotation = neck.localRotation;
InvokeRepeating ("UpdatePos", 0f, 1f / sendRate);
}
[Client]
void UpdatePos(){
if (!isLocalPlayer) {
return;
}
neckRotation = neck.localRotation;
if (!Network.isServer) {
CmdUpdateServerRotation (neckRotation, neckLastRotation);
} else {
RpcSetRotation (neckRotation, neckLastRotation);
}
neckLastRotation = neckRotation;
}
[Command]
void CmdUpdateServerRotation(Quaternion last, Quaternion newR){
RpcSetRotation(last, newR);
}
[ClientRpc]
void RpcSetRotation(Quaternion last, Quaternion newR){
neck.localRotation = Quaternion.Lerp(last, newR, Time.time/lerpSpeed);
}
}
The gist of that is that it sends the heads rotation from last time the method was called and its current rotation to a command that calls an rpc that will lerp the head from the old rotation to the new one for all clients.
This works but after setting the rotation of the player's neck, it immediately sets it back. I think this is because players have local authority with the network identity script, but I have absolutely no clue how to fix this. Also, after a few minutes the server shuts down and sends these errors
Failed to send internal buffer channel:0 bytesToSend:42 UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
Send Error: WrongConnection channel:0 bytesToSend:42 UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
Any ideas what I can do? I've been on this for a few days and it's driving me insane.
Also I always have trouble making bodies of code, so if anybody could help me out with how to do spacing there, that would be nice too :)