- Home /
OnSerializeNetworkView Doesn't work Until Owner moves the Object!
I've looked around the internet for couple of minutes, i couldn't find anything related to my problems, so I'm going to take your time a little bit. I use Interpolation (Smoothing on internet) for moving my characters in the game. When a player spawn, it has it's default location. If he moves And then stops and then somebody joins the game right after that, sees that previous player moves so slowly toward its End Position, where its supposed to be, but so slowly. If that previous player moves a little bit, then it'll be fine and the second player now sees the first one normally. I checked the Delay time between those calls for Syncing second player for first one, and i noticed it was 3 seconds until first player moves a little bit, then after that delay time for Syncing his location fixed to 0.02. I personally think OnSerializeNetworkView function needs a first communication, but don't know how!
Here's the code on the Players Objects:
using UnityEngine;
using System.Collections;
// - - - - - - - - - - Player Network Manager - - - - - - - - - - //
public class NetworkManager_ThePlayer : MonoBehaviour
{
// - - - - - - - - - - Fields - - - - - - - - - - //
#region Fields
// Publics //
public NetworkPlayer theOwner;
// Privates //
float lastClientHInput = 0f;
float lastClientVInput = 0f;
float serverCurrentHInput = 0f;
float serverCurrentVInput = 0f;
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
#endregion
// - - - - - - - - - - Mono Functions - - - - - - - - - - //
#region MonoFunctions
void Start()
{
if (Network.isServer)
{
switch (int.Parse(theOwner.ToString()))
{
case 0:
this.transform.position = GameObject.Find("PlayerRespawnLocation1").transform.position;
break;
case 1:
this.transform.position = GameObject.Find("PlayerRespawnLocation2").transform.position;
break;
case 2:
this.transform.position = GameObject.Find("PlayerRespawnLocation3").transform.position;
break;
case 3:
this.transform.position = GameObject.Find("PlayerRespawnLocation4").transform.position;
break;
}
}
}
void Awake()
{
if(Network.isClient)
this.gameObject.rigidbody.isKinematic = true;
}
void Update()
{
if (theOwner != null && Network.player == theOwner)
{
float HInput = Input.GetAxis("Horizontal");
float VInput = Input.GetAxis("Vertical");
if (lastClientHInput != HInput || lastClientVInput != VInput)
{
lastClientHInput = HInput;
lastClientVInput = VInput;
if (Network.isServer)
SendMovementInput(HInput, VInput);
else if (Network.isClient)
networkView.RPC("SendMovementInput", RPCMode.Server, HInput, VInput);
}
}
if (Network.isServer)
{
Vector3 moveDirection = new Vector3(serverCurrentHInput, 0, serverCurrentVInput);
float speed = 5;
transform.Translate(speed * moveDirection * Time.deltaTime);
}
if (!Network.isServer && !networkView.isMine)
{
SyncedMovement();
}
if (Network.player == theOwner)
if (Input.GetKeyDown(KeyCode.R))
InputColorChange();
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 syncPosition = Vector3.zero;
Vector3 syncVelocity = Vector3.zero;
if (stream.isWriting)
{
syncPosition = rigidbody.position;
stream.Serialize(ref syncPosition);
syncVelocity = rigidbody.velocity;
stream.Serialize(ref syncVelocity);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncVelocity);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncStartPosition = rigidbody.position;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
}
}
#endregion
// - - - - - - - - - - Methods - - - - - - - - - - //
#region Methods
[RPC]
void SetPlayer(NetworkPlayer player)
{
theOwner = player;
if (player == Network.player)
enabled = true;
}
[RPC]
void SendMovementInput(float HInput, float VInput)
{
serverCurrentHInput = HInput;
serverCurrentVInput = VInput;
}
[RPC]
void ChangeColorTo(Vector3 color)
{
if (Network.isClient)
{
renderer.material.color = new Color(color.x, color.y, color.z, 1f);
}
if (Network.isServer)
{
renderer.material.color = new Color(color.x, color.y, color.z, 1f);
networkView.RPC("ChangeColorTo", RPCMode.OthersBuffered, color);
}
}
private void SyncedMovement()
{
syncTime += Time.deltaTime;
rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
}
private void InputColorChange()
{
Vector3 color = new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
if (Network.isServer)
{
renderer.material.color = new Color(color.x, color.y, color.z, 1f);
networkView.RPC("ChangeColorTo", RPCMode.OthersBuffered, color);
}
else if (Network.isClient)
networkView.RPC("ChangeColorTo", RPCMode.Server, color);
}
#endregion
}
Well, I just did this, it worked!! But I'm still looking for a standard solution!
private void Synced$$anonymous$$ovement()
{
if (theOwner != Network.player && FirstTimeSyncing == true)
{
syncStartPosition = syncEndPosition;
rigidbody.position = syncEndPosition;
Debug.Log("It;s happingn" + syncEndPosition.ToString());
FirstTimeSyncing = false;
}
else
{
syncTime += Time.deltaTime;
rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
}
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Multiplayer UNet doesn't use UnityEngine.Network anymore right? 2 Answers
are you sure the server can be connected to? 1 Answer
'NetworkTranformChild' problem 1 Answer