Search Unity

Rotation of child objects is not synced if rotation occurs before client connects?

Discussion in 'Multiplayer' started by noise256, Aug 19, 2015.

  1. noise256

    noise256

    Joined:
    Apr 7, 2013
    Posts:
    22
    Greetings comrades,

    I'd like some advice on how to fix a long standing bug.

    I've setup my player, as follows, with a Rigidbody2D and a NetworkTransform using Sync Rigidbody 2D however, the child objects of the Player do not have a Rigidbody2D or a NetworkTransform.

    Player -> Rigidbody2D, NetworkTransform
    Child
    Child
    Child​

    After a client connects, this works fine and the rotation of the Player is synced and the children pivot around the Player position as expected. However, any rotation that occurs on a Player prior to new client connecting is not synced on the child objects. This results in a jumbled mess as follows:

    http://imgur.com/2fGFcTR

    I would really like to avoid adding NetworkTransform components to the children as their is likely to be a significant number of them. I thought perhaps that the children could have a NetworkTransform with a send rate of 0 and a sync event be executed manually but I don't really understand how to use:

    Code (CSharp):
    1. InvokeSyncEvent(int cmdHash, Networking.NetworkReader reader);
    Is my best option to create a sync var for the rotation on each child object? This doesn't seem like a very good solution as the rotation only needs to be synced once on client connect.

    Apologies if I've missed something obvious. Thank you.
     
  2. noise256

    noise256

    Joined:
    Apr 7, 2013
    Posts:
    22
    Hi, OK, my earlier problem didn't really explain the situation sufficiently but for anyone else having the similar problems:

    To the best of my understanding:

    The position and rotation of the children is not being synced because the children are NetworkBehaviour objects that are dynamically added/removed from the player object and therefore, I don't have a fixed prefab with all the child objects attached. To deal with this I am spawning the child objects separately and using a SyncVar to set the parent of the child objects on the client during OnClientConnect(). Because this was happening after the Player object is created on the client the position of the child objects was not synced correctly. As such I added two lines to adjust the position and rotation of the child objects to the OnClientConnect() event as follows:

    Code (CSharp):
    1.     [SyncVar]
    2.     public GameObject parentObject;
    3.  
    4.     [SyncVar]
    5.     public Vector3 defaultOffset;
    6.  
    7.     [SyncVar]
    8.     public Quaternion serverRotation;
    9.  
    10.     public override void OnStartClient () {
    11.         if (parentObject) {
    12.             transform.parent = parentObject.transform;
    13.             transform.position = parentObject.transform.position + defaultOffset;
    14.             transform.rotation = parentObject.transform.rotation * serverRotation;
    15.         }
    16.     }