• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by proxyjan · Apr 13, 2014 at 01:08 PM · networkingmultiplayernetworkmasterserveronserializenetworkview

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
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image proxyjan · Apr 13, 2014 at 05:38 PM 1
Share

Well, I just did this, it worked!! But I'm still looking for a standard solution!

 private void SyncedMovement()
     {
         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);
         }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

are you sure the server can be connected to? 1 Answer

Unity networking tutorial? 6 Answers

Multiplayer UNet doesn't use UnityEngine.Network anymore right? 2 Answers

RPC custom server best practice 1 Answer

Networking, players can't see each others movements 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges