Search Unity

How to get message delay?

Discussion in 'Multiplayer' started by HotPhone, Sep 14, 2017.

  1. HotPhone

    HotPhone

    Joined:
    May 14, 2014
    Posts:
    53
    Hi, please answer this question for me, I already googled everything and cant find an answer. I need to do a simple thing but nothing worked.

    I want to implement prediction into my game because everything has a delay on the client. For this I need to get the time between sending a message and recieving it.

    1. How do I find out how long ago the command was sent inside a [Command] method on the server?
    2. How do I find out how long ago an Rpc was sent inside a [ClientRpc] method on the client?

    Ill explain by two examples.

    1. In my game, the client sends a Command that he started charging a punch. By the time the server recieves the message, the client has already charged for 0.2 (network delay) seconds, but the server thinks it is 0 seconds. I could fix the problem by setting the Chargetime to 0.2 (delay) but how do I find the delay?

    float messageDelay = ??

    2. In my game client shoot projectiles that travel at 70 m/s, not hitscan. The client sends a command to the server that he shot a projectile at a position. But by the time the server recieves thr message, the projectile is already somewhere else on the client. I need to push it forward depending on the network delay.

    please help, thanks
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    You want lag compensation?
    https://twotenpvp.github.io/lag-compensation-in-unity.html

    But to calculate the delay. You need to use the LLAPI methods.
     
  3. HotPhone

    HotPhone

    Joined:
    May 14, 2014
    Posts:
    53
    Thanks, I read a bit about the LLAPI. Do I have to quit using HLAPI, if I want to synchronize time? Or could I use the networkTransport just for one message to synchronize time, and keep working with the HLAPI? I need some clarification before I rewrite my whole network code (again).
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    HLAPI is a opensource C# library ontop of the LLAPI
     
  5. HotPhone

    HotPhone

    Joined:
    May 14, 2014
    Posts:
    53
    I am using this script to establish a NetworkTransport connection.

    https://gist.github.com/LinaAdkins/a3bc0cee6f39bfd80110

    It works, but the problem is that I cant connect with the networkmanager anymore when this script runs. Any ideas why that is? The networkmanager uses another port.
     
    Last edited: Sep 17, 2017
  6. HotPhone

    HotPhone

    Joined:
    May 14, 2014
    Posts:
    53
    I think that I solved the problem with a modified version of a script from zee_ola from this thread:
    https://forum.unity.com/threads/how-to-sync-network-time.395809/

    If you need to synchronize time, try out the script on your player object:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Networking;
    5.  
    6. public class NetworkTimer : NetworkBehaviour
    7. {
    8.     public bool isNetworkTimeSynced = false;
    9.  
    10.     // timestamp received from server
    11.     private int networkTimestamp;
    12.  
    13.     // server to client delay
    14.     private int networkTimestampDelayMS;
    15.  
    16.     // when did we receive timestamp from server
    17.     private float timeReceived;
    18.  
    19.     private float startTime;
    20.  
    21.     protected void Start()
    22.     {
    23.         if (isLocalPlayer)
    24.             StartCoroutine(RequestTimeCO());
    25.     }
    26.  
    27.     IEnumerator RequestTimeCO()
    28.     {
    29.         while (true)
    30.         {
    31.             yield return new WaitForSeconds(5);
    32.             CmdRequestTime();
    33.         }
    34.     }
    35.  
    36.     [Command]
    37.     private void CmdRequestTime()
    38.     {
    39.         int timestamp = NetworkTransport.GetNetworkTimestamp();
    40.         RpcNetworkTimestamp(timestamp);
    41.         Debug.Log("SERVER SEND TIME");
    42.     }
    43.  
    44.     [ClientRpc]
    45.     private void RpcNetworkTimestamp(int timestamp)
    46.     {
    47.         Debug.Log("CLIENT RECIEVE TIME");
    48.         isNetworkTimeSynced = true;
    49.         networkTimestamp = timestamp;
    50.         timeReceived = Time.time;
    51.  
    52.         // if client is a host, assume that there is 0 delay
    53.         if (isServer)
    54.         {
    55.             networkTimestampDelayMS = 0;
    56.         }
    57.         else
    58.         {
    59.             byte error;
    60.             networkTimestampDelayMS = NetworkTransport.GetRemoteDelayTimeMS(
    61.                 NetworkManager.singleton.client.connection.hostId,
    62.                 NetworkManager.singleton.client.connection.connectionId,
    63.                 timestamp,
    64.                 out error);
    65.             Debug.Log(networkTimestampDelayMS);
    66.         }
    67.         startTime = ((float)networkTimestamp / 1000f) +
    68. ((float)networkTimestampDelayMS / 1000f);
    69.     }
    70.  
    71.     void OnGUI()
    72.     {
    73.         if (!isLocalPlayer)
    74.             return;
    75.         GUI.color = Color.red;
    76.         GUI.Label(new Rect(50, 50, 200, 200), GetServerTime().ToString());
    77.     }
    78.  
    79.     public float GetServerTime()
    80.     {
    81.         return startTime + (Time.time - timeReceived);
    82.     }
    83. }
    84.  
     
    Deleted User likes this.