Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity Multiplayer Rigidbody sync

Discussion in 'Multiplayer Networking' started by Daniiii, Oct 7, 2015.

  1. Daniiii

    Daniiii

    Joined:
    Nov 13, 2013
    Posts:
    11
    Hi, I got a question about how to sync a rigidbody using Unet.
    My setup: Player-characters (syncing fine) and a ball, all with rigidbodies. The players should be able to push the ball around.
    When using a networktransform with syncrigidbody on the ball and a remote clients tries to push the ball, it doesn't work because the position gets set by the server.


    I found the NetworkRigidbody script using the old Unity networking system
    and I tried editing it to get it to work with Unet, but to no avail.

    What is the general way to sync rigidbodies using unet/which general things would one need to change in the NetworkRigidbody script?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NetworkRigidbody : MonoBehaviour
    5. {
    6.  
    7.     public double m_InterpolationBackTime = 0.1;
    8.     public double m_ExtrapolationLimit = 0.5;
    9.  
    10.     internal struct State
    11.     {
    12.         internal double timestamp;
    13.         internal Vector3 pos;
    14.         internal Vector3 velocity;
    15.         internal Quaternion rot;
    16.         internal Vector3 angularVelocity;
    17.     }
    18.  
    19.     // We store twenty states with "playback" information
    20.     State[] m_BufferedState = new State[20];
    21.     // Keep track of what slots are used
    22.     int m_TimestampCount;
    23.  
    24.     void Start ()
    25.     {
    26.         foreach (NetworkView n in GetComponents<NetworkView> ()) {
    27.             n.observed = this;
    28.         }
    29.     }
    30.  
    31.     {
    32.         // Send data to server
    33.         if (stream.isWriting) {
    34.             Vector3 pos = rigidbody.position;
    35.             Quaternion rot = rigidbody.rotation;
    36.             Vector3 velocity = rigidbody.velocity;
    37.             Vector3 angularVelocity = rigidbody.angularVelocity;
    38.            
    39.             stream.Serialize (ref pos);
    40.             stream.Serialize (ref velocity);
    41.             stream.Serialize (ref rot);
    42.             stream.Serialize (ref angularVelocity);
    43.             // Read data from remote client
    44.         } else {
    45.             Vector3 pos = Vector3.zero;
    46.             Vector3 velocity = Vector3.zero;
    47.             Quaternion rot = Quaternion.identity;
    48.             Vector3 angularVelocity = Vector3.zero;
    49.             stream.Serialize (ref pos);
    50.             stream.Serialize (ref velocity);
    51.             stream.Serialize (ref rot);
    52.             stream.Serialize (ref angularVelocity);
    53.            
    54.             // Shift the buffer sideways, deleting state 20
    55.             for (int i = m_BufferedState.Length - 1; i >= 1; i--) {
    56.                 m_BufferedState[i] = m_BufferedState[i - 1];
    57.             }
    58.            
    59.             // Record current state in slot 0
    60.             State state;
    61.             state.timestamp = info.timestamp;
    62.             state.pos = pos;
    63.             state.velocity = velocity;
    64.             state.rot = rot;
    65.             m_BufferedState[0] = state;
    66.            
    67.             // Update used slot count, however never exceed the buffer size
    68.             // Slots aren't actually freed so this just makes sure the buffer is
    69.             // filled up and that uninitalized slots aren't used.
    70.             m_TimestampCount = Mathf.Min (m_TimestampCount + 1, m_BufferedState.Length);
    71.            
    72.             // Check if states are in order, if it is inconsistent you could reshuffel or
    73.             // drop the out-of-order state. Nothing is done here
    74.             for (int i = 0; i < m_TimestampCount - 1; i++) {
    75.                 if (m_BufferedState[i].timestamp < m_BufferedState[i + 1].timestamp)
    76.                     Debug.Log ("State inconsistent");
    77.             }
    78.         }
    79.     }
    80.  
    81.     // We have a window of interpolationBackTime where we basically play
    82.     // By having interpolationBackTime the average ping, you will usually use interpolation.
    83.     // And only if no more data arrives we will use extra polation
    84.     void Update ()
    85.     {
    86.         // This is the target playback time of the rigid body
    87.         double interpolationTime = Network.time - m_InterpolationBackTime;
    88.        
    89.         // Use interpolation if the target playback time is present in the buffer
    90.         if (m_BufferedState[0].timestamp > interpolationTime) {
    91.             // Go through buffer and find correct state to play back
    92.             for (int i = 0; i < m_TimestampCount; i++) {
    93.                 if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount - 1) {
    94.                     // The state one slot newer (<100ms) than the best playback state
    95.                     State rhs = m_BufferedState[Mathf.Max (i - 1, 0)];
    96.                     // The best playback state (closest to 100 ms old (default time))
    97.                     State lhs = m_BufferedState[i];
    98.                    
    99.                     // Use the time between the two slots to determine if interpolation is necessary
    100.                     double length = rhs.timestamp - lhs.timestamp;
    101.                     float t = 0.0f;
    102.                     // As the time difference gets closer to 100 ms t gets closer to 1 in
    103.                     // which case rhs is only used
    104.                     // Example:
    105.                     // Time is 10.000, so sampleTime is 9.900
    106.                     // lhs.time is 9.910 rhs.time is 9.980 length is 0.070
    107.                     // t is 9.900 - 9.910 / 0.070 = 0.14. So it uses 14% of rhs, 86% of lhs
    108.                     if (length > 0.0001f)
    109.                         t = (float)((interpolationTime - lhs.timestamp) / length);
    110.                    
    111.                     // if t=0 => lhs is used directly
    112.                     transform.localPosition = Vector3.Lerp (lhs.pos, rhs.pos, t);
    113.                     transform.localRotation = Quaternion.Slerp (lhs.rot, rhs.rot, t);
    114.                     return;
    115.                 }
    116.             }
    117.             // Use extrapolation
    118.         } else {
    119.             State latest = m_BufferedState[0];
    120.            
    121.             float extrapolationLength = (float)(interpolationTime - latest.timestamp);
    122.             // Don't extrapolation for more than 500 ms, you would need to do that carefully
    123.             if (extrapolationLength < m_ExtrapolationLimit) {
    124.                 float axisLength = extrapolationLength * latest.angularVelocity.magnitude * Mathf.Rad2Deg;
    125.                 Quaternion angularRotation = Quaternion.AngleAxis (axisLength, latest.angularVelocity);
    126.                
    127.                 rigidbody.position = latest.pos + latest.velocity * extrapolationLength;
    128.                 rigidbody.rotation = angularRotation * latest.rot;
    129.                 rigidbody.velocity = latest.velocity;
    130.                 rigidbody.angularVelocity = latest.angularVelocity;
    131.             }
    132.         }
    133.     }
    134.    
    135. }
    136.  
    137.  
     
  2. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    352
    You can just add NetworkTransform component to your object with Rigidbody and it will be interpolated automatically. If client want to push the object, he should send a [Command] to server.
     
    HakJak likes this.
  3. hottabych

    hottabych

    Joined:
    Apr 18, 2015
    Posts:
    43
    The same issue...
    I do VR multiplayer. When player on the host grabs the ball (by joystick), all the players see it. But if remote client grabs ball, only he can see it, while other players see like a ball still lies on the ground.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    2,243
    Does the server or the client have authority over the ball object? If the server has authority, are you sending Commands to the server to move the ball or just moving the ball locally on the client?