- Home /
Sync player bone/skin renderer with unet?
So I have a player model, and I would like to sync the head movement with the camera on the network, which is already working locally. For the head movement I just set the head bone's rotation.y to the camera's rotation.y (using euler angles and quaternions). So my question is, would it be possible to sync that rotation over the network without separating the mesh into two meshes for the head and body? Any help is appreciated and thanks for your time!
public GameObject headBone;
[SerializeField]
private Camera cam;
private void LateUpdate() {
RotateHead();
}
void RotateHead() {
Vector3 _camRot = cam.transform.localRotation.eulerAngles;
Quaternion _newRot = Quaternion.Euler(0, _camRot.x, 0);
headBone.transform.localRotation = _newRot;
}
Answer by UnityNoob123 · Apr 22, 2018 at 06:44 PM
So basically if anyone stumbles upon this in the future, to sync the bone rotation I used a command and an rpcclient call and passed in the calculated rotation, and set the head bone gameobject inside those two calls. However this worked, it made the characters head glitch over the network because if the Rpc call was late or never happened due to network lag, the players head would try to snap back to its original rotation because of the animation that was playing. To fix this I saw an old post from 2013 which basically said to make a gloabal variable for rotation, and set that variable in the command and rpc calls, but in the regular function, set the head rotation to that global variable so that way the clients and network calls "see" the same rotation. Also note, you cannot disable this script for other clients, since the connected clients need to run the head rotation function every frame to avoid the head from glitching. I hope this helps someone!
public GameObject headBone;
private Quaternion headRot;
[SerializeField]
private Camera cam;
// Update is called once per frame
void FixedUpdate () {
PerformMovement ();
PerformRotation ();
}
private void LateUpdate() {
RotateHead();
}
//This script cannot be disabled because the clients need to be updated every frame
void RotateHead() {
float _currentCamRotX = cam.transform.localRotation.x;
Vector3 _camRot = cam.transform.localRotation.eulerAngles;
prevCamRot = _currentCamRotX;
Quaternion _rot = Quaternion.Euler(0, _camRot.x, 0);
headBone.transform.localRotation = headRot;
if (isLocalPlayer) {
CmdProvideRotationsToServer(_rot);
}
}
[Command]
void CmdProvideRotationsToServer(Quaternion _rot) {
headRot = _rot;
headBone.transform.localRotation = _rot;
RpcProvideRotationsToClient(_rot);
}
[ClientRpc]
void RpcProvideRotationsToClient(Quaternion _rot) {
headRot = _rot;
headBone.transform.localRotation = _rot;
}
Absolute legend. Been stuck on the problem looking everywhere for days but this works perfectly. Thank you so much!