Search code examples
unity-game-engineairconsole

Hide Airconsole Latency


With Airconsole, there's a noticeable lag between pressing a button and seeing the result on-screen. In a presentation about Airconsole available here, it's mentioned you can "cheat" by backtracking based on a timestamp from the client.

Is there a simple/built-in way to do this with Unity? I can see how you could do it by having the server and client agree on a mutual timestamp, but that seems tricky and I was wondering there's a built-in way to do this.


Solution

  • It's rather simple to do it using the getServerTime() function.
    On the controller, you send the timestamp at which the input happens with the message. On the screen, you compare said timestamp with the current GetServerTime() and the difference is the latency.

    Controller:

    airconsole.message(AirConsole.SCREEN, {"action": "jump", "ts": airconsole.getServerTime()});
    

    Screen:

    void OnMessage(int from, JToken data) {
        if ((string)data ["action"] == "jump") {
    
            long latency_ms = AirConsole.instance.GetServerTime () - (long)data ["ts"];
    
            Debug.Log ("device " + from + " latency: " + latency_ms);
    
            //Jump quicker depending on latency
    
        } 
    
    }