Search Unity

Opinion request on inter/extrapolation test code

Discussion in 'Multiplayer' started by rob_vld, Aug 2, 2017.

  1. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    so i am trying to create a latency solution for moving objects over the network and made a simple interpolation/extrapolation demo... but in my eyes it is not working properly.... is anyone willing to give his opinion about it ?

    At an interval of 100ms with a latency delay of 200ms the remote is dragging far behind the local


    client -> server; server tries to predict where the client 'was'
    server -> client; client tries to predict where the other client 'was'

    so, am i missing a prediction on the receiving end?



    Code (CSharp):
    1. CLIENT ==================================================
    2.  
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. using System.Collections.Generic;
    7.  
    8. public class Client : MonoBehaviour
    9. {
    10.     [HideInInspector] public NetworkClient networkClient;
    11.     [HideInInspector] public byte channelReliableSequenced;
    12.  
    13.     private string serverIP;
    14.     private int serverPort;
    15.  
    16.     public GameObject localIndicator;
    17.     public GameObject remoteIndicator;
    18.  
    19.     private float networkInterval;
    20.     private float networkIntervalMax;
    21.  
    22.  
    23.     public void Awake()
    24.     {
    25.         print("Client Started");
    26.  
    27.         serverIP = "xxxx";
    28.         serverPort = 22001;
    29.  
    30.         NetworkTransport.Init();
    31.  
    32.         ConnectionConfig connectionConfig = new UnityEngine.Networking.ConnectionConfig();
    33.         channelReliableSequenced = connectionConfig.AddChannel(QosType.ReliableSequenced);
    34.  
    35.         networkClient = new NetworkClient();
    36.         networkClient.Configure(connectionConfig, 1);
    37.         networkClient.RegisterHandler(MsgType.Connect, OnConnect);
    38.         networkClient.RegisterHandler(MsgType.Error, OnError);
    39.         networkClient.RegisterHandler(MsgTypeCustom.OnMsgPong, OnMsgPong);
    40.  
    41.         networkClient.Connect(serverIP, serverPort);
    42.  
    43.         networkIntervalMax = 0.100f;
    44.  
    45.     }
    46.  
    47.     public void Update()
    48.     {
    49.         float XBOX_X_H = -Input.GetAxis("XBOX_X_H");
    50.         float XBOX_X_V = -Input.GetAxis("XBOX_X_V");
    51.  
    52.         float speed = 10f;
    53.  
    54.  
    55.         if ((XBOX_X_H < -0.15f || XBOX_X_H > 0.15f)
    56.            || (XBOX_X_V < -0.15f || XBOX_X_V > 0.15f))
    57.         {
    58.             Vector3 move = new Vector3(XBOX_X_H, XBOX_X_V, 0f);
    59.             localIndicator.transform.position += move * speed * Time.deltaTime;
    60.  
    61.         }
    62.  
    63.  
    64.  
    65.  
    66.         networkInterval += Time.deltaTime;
    67.         if (networkInterval > networkIntervalMax)
    68.         {
    69.  
    70.  
    71.             networkInterval = networkIntervalMax - networkInterval;
    72.  
    73.  
    74.  
    75.             MsgPing msgPing = new MsgPing();
    76.             msgPing.positionX = localIndicator.transform.position.x;
    77.             msgPing.positionY = localIndicator.transform.position.y;
    78.             msgPing.timestamp = NetworkTransport.GetNetworkTimestamp();
    79.  
    80.             networkClient.SendByChannel(MsgTypeCustom.OnMsgPing
    81.                                        , msgPing
    82.                                        , channelReliableSequenced);
    83.  
    84.  
    85.         }
    86.  
    87.     }
    88.  
    89.     private void OnMsgPong(NetworkMessage networkMessage)
    90.     {
    91.         MsgPong msgPong = networkMessage.ReadMessage<MsgPong>();
    92.  
    93.  
    94.  
    95.         byte error;
    96.         int delay = NetworkTransport.GetRemoteDelayTimeMS(networkMessage.conn.hostId,
    97.                                                             networkMessage.conn.connectionId,
    98.                                                              msgPong.timestamp, out error);
    99.  
    100.  
    101.         msgPong.positionX = msgPong.positionX + (((remoteIndicator.transform.position.x - msgPong.positionX) / 1000f) * delay);
    102.         msgPong.positionY = msgPong.positionY + (((remoteIndicator.transform.position.y - msgPong.positionY) / 1000f) * delay);
    103.  
    104.  
    105.  
    106.         remoteIndicator.transform.position = new Vector3(msgPong.positionX, msgPong.positionY, 5.245f);
    107.  
    108.  
    109.     }
    110.  
    111.     private void OnConnect(NetworkMessage networkMessage)
    112.     {
    113.         print("OnConnect");
    114.     }
    115.  
    116.     private void OnError(NetworkMessage networkMessage)
    117.     {
    118.         print("OnError");
    119.     }
    120. }
    121.  
    122.  
    123.  
    124.  
    125.  
    126. SERVER ==================================================
    127.  
    128.  
    129. using UnityEngine;
    130. using UnityEngine.Networking;
    131.  
    132. using System.Collections.Generic;
    133.  
    134. public class Server : MonoBehaviour
    135. {
    136.     [HideInInspector] public byte channelReliableSequenced;
    137.  
    138.     public void Awake()
    139.     {
    140.         print("Server Started");
    141.  
    142.         NetworkTransport.Init();
    143.  
    144.         ConnectionConfig connectionConfig = new ConnectionConfig();
    145.  
    146.         channelReliableSequenced = connectionConfig.AddChannel(QosType.ReliableSequenced);
    147.  
    148.         NetworkServer.Configure(connectionConfig, 5);
    149.  
    150.         NetworkServer.RegisterHandler(MsgType.Connect, OnConnect);
    151.         NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnect);
    152.         NetworkServer.RegisterHandler(MsgType.Error, OnError);
    153.         NetworkServer.RegisterHandler(MsgTypeCustom.OnMsgPing, OnMsgPing);
    154.  
    155.         NetworkServer.Listen(22001);
    156.     }
    157.  
    158.  
    159.     private float positionPreviousX;
    160.     private float positionPreviousY;
    161.  
    162.     private void OnMsgPing(NetworkMessage networkMessage)
    163.     {
    164.         MsgPing msgPing = networkMessage.ReadMessage<MsgPing>();
    165.  
    166.         byte error;
    167.         int delay = NetworkTransport.GetRemoteDelayTimeMS(networkMessage.conn.hostId,
    168.                                                             networkMessage.conn.connectionId,
    169.                                                              msgPing.timestamp, out error);
    170.  
    171.  
    172.  
    173.         MsgPong msgPong = new MsgPong();
    174.         msgPong.timestamp = NetworkTransport.GetNetworkTimestamp();
    175.         msgPong.positionX = msgPing.positionX + (((positionPreviousX - msgPing.positionX) / 1000f) * delay);
    176.         msgPong.positionY = msgPing.positionY + (((positionPreviousY - msgPing.positionY) / 1000f) * delay);
    177.  
    178.  
    179.  
    180.         networkMessage.conn.SendByChannel(MsgTypeCustom.OnMsgPong
    181.                                          , msgPong
    182.                                          , channelReliableSequenced);
    183.  
    184.         positionPreviousX = msgPing.positionX;
    185.         positionPreviousY = msgPing.positionY;
    186.  
    187.     }
    188.  
    189.     //------------------------------------------------------------------------------------------------> Unity Network Methods
    190.     private void OnConnect(NetworkMessage networkMessage)
    191.     {
    192.         print("OnConnect");
    193.     }
    194.  
    195.     private void OnDisconnect(NetworkMessage networkMessage)
    196.     {
    197.         print("OnDisconnect");
    198.     }
    199.  
    200.     private void OnError(NetworkMessage networkMessage)
    201.     {
    202.         print("OnError");
    203.     }
    204. }
     
    Last edited: Aug 2, 2017