Search Unity

Input lag

Discussion in 'Netcode for GameObjects' started by Jicefrost, May 24, 2022.

  1. Jicefrost

    Jicefrost

    Joined:
    May 13, 2019
    Posts:
    40
    Hi. Simple game - 2 players connected by phone, control by joystick prefab
    1 phone host+ client
    2 phone client.
    they flying on scene with rigidbody 2d, try to hit each other. I'm using server RPC call -

    Code (CSharp):
    1. public class PlayerController : NetworkBehaviour
    2. {
    3.     [SerializeField] private Joystick _joystick;
    4.     public float x, y;
    5.     [SerializeField] public Rigidbody2D _rigidbody2D;
    6.     [SerializeField] private float XfromServer, YFromServer;
    7.  
    8.     public float Speed = 5f;
    9.     void Start()
    10.     {
    11.         if (IsLocalPlayer && IsClient)
    12.         {
    13.             Debug.Log("THIS IS LOCAL player!!");
    14.            _joystick = GameObject.FindGameObjectWithTag("GameController").GetComponent<Joystick>();
    15.         }
    16.  
    17.         if(IsHost)
    18.         {
    19.             Debug.Log("this is host!!");
    20.         }
    21.         if (!IsLocalPlayer)
    22.         {
    23.             _joystick = GameObject.FindGameObjectWithTag("GameController").GetComponent<Joystick>();
    24.             _rigidbody2D = GetComponent<Rigidbody2D>();
    25.         }
    26.  
    27.     }
    28.     public void Update()
    29.     {
    30.        if (IsLocalPlayer )
    31.         {
    32.             x = _joystick.Horizontal * Speed;
    33.             y = _joystick.Vertical * Speed;
    34.  
    35.             PlayerControlServerRpc(x, y);
    36.         }
    37.     }
    38.  
    39.     [ServerRpc]
    40.     private void PlayerControlServerRpc(float x, float y)
    41.     {
    42.         Debug.Log($"Server received X={x} and Y={y}");
    43.         _rigidbody2D.AddForce(new Vector3(x, y, 0));
    44.     }
    45. }
    everything seem to work fine and smooth but the client always have "input lag" example
    fit joystick right, player fly right, fit joystick left need to wait about 1 sec to fly left.
    All tested on different phones, and even on same 1 PC, always same issue, but host is moving of course instantly. How can I speed up clients control?
     
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    The delay is because of two things: one is the lag due to the distance/network speed between the server and client (which is zero when on the same machine), and the other is due to the time it takes for the Rpc to be sent to the server plus back to the client (through NetworkTransform/NetworkRigidbody I assume). The latter can be sped up by increasing the "Tick Rate" in your NetworkManager, which you can view as the number of communications between server and client per second. If your tick rate is 20 for example, the minimum amount of time between the player providing input and seeing a result (even on the same machine) is 2*1/20=0.1 seconds.

    The input lag can be entirely avoided by processing and applying the input on the client directly (using a ClientNetworkTransform instead of a NetworkTransform, and no NetworkRigidbody). There will of course still be a delay for everyone else when a player provides an input, but not for themselves. The downside is that this makes cheating easier, since it means the client's inputs are fully trusted.

    The best but hardest option, which is currently not a part of Netcode but for very simple games could be coded up yourself without too much effort, is to do client-side prediction: apply the input immediately on the client, but then also send it to the server. If the server then finds a mismatch for some reason, the client is corrected. Most AAA fast-paced multiplayer games (e.g. Overwatch) do something like this – but it's much harder than just using a ClientNetworkTransform and trusting the clients.
     
    Jicefrost likes this.
  3. Jicefrost

    Jicefrost

    Joined:
    May 13, 2019
    Posts:
    40
    exactly we use much rigidbodies :( yes on "ClientNetworkTransform" it all were good.
    Well thx for good advice man! I will try to play with tick rate and maybe write some client - prediction.
    Thanks a lot for help!
     
    Last edited: May 25, 2022