- Home /
Network - Sync Issue
Hello,
I'm playing around with networking, and I've set up a little demo scene which has a wall that rotates via:
function Update(){
transform.Rotate(Vector3.up, Time.deltaTime * 5);
Now, this wall rotation needs to be synced across all players, right now who ever connects will start rotating the wall, thus being out of sync.
So, I decided to let the Server player control the rotation of the wall, and then send the position to every client. I could just stick a NetworkView on it, but I want to learn about syncing.
So, I've done this:
function Update () {
if(Network.isClient){
SyncedMovement();
}
else if(Network.isServer){
transform.Rotate(Vector3.up, Time.deltaTime * 5);
}
}
function SyncedMovement()
{
syncTime += Time.deltaTime;
transform.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
}
var lastSynchronizationTime : float = 0f;
var syncDelay : float = 0f;
var syncTime : float = 0f;
var syncStartPosition : Vector3 = Vector3.zero;
var syncEndPosition : Vector3 = Vector3.zero;
function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)
{
var syncPosition = Vector3.zero;
if (stream.isWriting)
{
syncPosition = transform.position;
stream.Serialize(syncPosition);
}
else
{
stream.Serialize(syncPosition);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncStartPosition = transform.position;
syncEndPosition = syncPosition;
}
}
I've then attached a NetworkView on it, and dragged the attached script into the observer.
The wall rotates just fine on the server player, but nothing happens at all with the clients. What am I doing wrong here? I followed this tutorial:
http://www.paladinstudios.com/2013/07/10/how-to-create-an-online-multiplayer-game-with-unity/
And just replaced the Rigidbody syncs with the transforms
Well ... that was silly of me, I didn't replace the transform.position, to transform.rotation
It works now, but there is still a bit of lag - so I'm wondering, because the animation is linear, maybe I just need to get the rotation from the server when starting up - and then using that Quaternion to play the animation locally? Would this be a better approach?
Your answer
![](https://koobas.hobune.stream/wayback/20220613121524im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sync animaor of child prefab 0 Answers
Network animations 2 Answers
Network Animation Problem 1 Answer
Network : sync animations of characters 1 Answer
Sync Network Animation State 2 Answers