Search Unity

OnSerializeNetworkView and rigidbody2D

Discussion in 'Multiplayer' started by ted23, Sep 21, 2014.

  1. ted23

    ted23

    Joined:
    May 18, 2014
    Posts:
    5
    I'm new to Unity networking. As a learning exercise I started with the following tutorial that many have suggested.

    http://www.paladinstudios.com/2013/07/10/how-to-create-an-online-multiplayer-game-with-unity/

    The tutorial is written for 3D. I'm trying to adjust it for 2D.

    Everything works well until the step where I replace the default synchronization of the transform with my own OnSerializeNetworkView function in PlayerMovementScript.cs. Comments on the blog indicate others are having a similar issue.

    Here is the behavior that I am seeing. I have two instances of the game running, Instance A and Instance B. Instance A is standalone and set to run in background. Instance B is running in the Unity Editor. The two instances successfully connect. I see no error messages in Instance B's console (I have network debugging enabled). As I move the player in Instance A, the movement is not visually updated in Instance B until the player gets close to a wall. Once the player gets close to a wall in Instance A, in Instance B that player will instantly teleport to the correct position in Instance B's display. The reverse is also true; moving the Instance B player is not updating in Instance A's display until the Instance B player gets close to a wall.

    Below is my network view update code. Is this not the proper way to network update players controlled by 2D physics?
    Code (csharp):
    1.  
    2. void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
    3. {
    4.    Vector3 syncPosition = Vector3.zero;
    5.  
    6.    if (stream.isWriting)
    7.    {
    8.       syncPosition = rigidbody2D.position;
    9.       if (syncPosition != lastWritePosition)
    10.       {
    11.          stream.Serialize(ref syncPosition);
    12.          lastWritePosition = syncPosition;
    13.       }
    14.    }
    15.    else
    16.    {
    17.      stream.Serialize(ref syncPosition);
    18.      rigidbody2D.position = syncPosition;
    19.    }
    20. }
    21.  
    Any help is appreciated.
     
  2. ted23

    ted23

    Joined:
    May 18, 2014
    Posts:
    5
    Adding the following after line18 above seems to fix my issue.

    Code (csharp):
    1.  
    2.       rigidbody2D.WakeUp();
    3.  
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,070
    I didn't work with the 2D stuff too much yet. Is it normal you have to WakeUp manually? I guess it's not.
    At the moment, I don't see why this matters :(
     
  4. ted23

    ted23

    Joined:
    May 18, 2014
    Posts:
    5
    The WakeUp/Sleep concept is described as an optimization to remove objects at rest from the physics simulation. I guess the optimization assumes objects will not get updates directly to their rigidBody2D.position. It assumes position changes will happen due to applied forces?

    (As an experiment I moved the update to rigidBody2D.position out of OnSerializeNetworkView, and into FixedUpdate. I thought that maybe Unity expected all physics updates to happen in FixedUpdate. But moving it to FixedUpdate did not help either.)
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,070
    I didn't experiment with the 2D physics so maybe things are a bit tricky here.
    Does FixedUpdate apply to 2D physics, too?
    Is there a setting like in 3D to make the object "Kinematic"? That setting helps if the object's position is driven by non-physics code.
     
  6. ted23

    ted23

    Joined:
    May 18, 2014
    Posts:
    5
    Although I could not find a definitive answer, the 2D examples that I ran across do their rigidBody2D updates in FixedUpdate.

    rigidBody2D does have 'Is Kinematic'. Selecting it did not help (surprisingly).

    Unsurprisingly, if I set the rigidBody2D 'Sleeping Mode' to 'Never Sleep', then I no longer have to WakeUp to see the remote players movement.
     
  7. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,070
    Thanks for letting us know.
    It makes sense that a sleeping physics object has to wake first.