Search Unity

I need help with input lag on game build

Discussion in 'General Discussion' started by Sigma266, Dec 25, 2019.

  1. Sigma266

    Sigma266

    Joined:
    Mar 25, 2017
    Posts:
    99
    So, on my Quality settings I set Vsync Count to Every V Blank, that way Unity allows me to set a specific frame rate(40) through application.targeFrameRate.
    Problem is that when I build my game and start it, my input are very noticeably delayed, which is weird, because nothing like that happens in the editor.
    I know switching Vsync count to Don't Sync fixes the issue, but my game is meant to be played at a lower frame rate.
    How do I deal with this? Thank you. Btw, I'm using version 2018.2.3f1.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,572
    In which function are you handling the input?

    One major screwup a programmer could do is using FixedUpdate instead of Update to process user input.

    Aside from that possibility, there is not enough data. You can put up onscreen indicators to indicate which keys are pressed, and how.

    Oh, and one more thing. This is actually probably it.

    By default, unity smooths out axis movement. I absolutely don't recall where this is documented, but to get direct, raw, unmodified value instead of GetAxis, you should calls GetAxisRaw.
    https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
    https://docs.unity3d.com/ScriptReference/Input.GetAxisRaw.html

    By default, unity applies "smoothing curve" of sorts to keyboard input, so a keyboard driven character won't start springint at full speed the moment you press "forward", but will take some time to speed up to the full. That may be perceived by you as input lag.

    I believe value of this smoothing is defined within input manager:
    https://docs.unity3d.com/Manual/class-InputManager.html

    This behavior is hidden and you do not become aware of it until you switch to an engine that doesn't do this by default.

    I also would not recommend using 40 FPS on modern monitor, unless you have specific use case like recording video. 30, 60, 15 or uncapped would be a better choice.
     
    Sigma266 likes this.
  3. Sigma266

    Sigma266

    Joined:
    Mar 25, 2017
    Posts:
    99
    Unfortunately every single one of my inputs are already set to raw, so that can't be the issue. Plus, as I said in my original post, the issue only happens in my build, the editor itself works fine. As for the 40 fps, that's what's I'm using because I'm going for pixelated graphics but going any lower would hurt my eyes after a while and I thought the same would apply to most people. It's not a heavy game, so there's shouldn't be any problems with that.
     
  4. Sigma266

    Sigma266

    Joined:
    Mar 25, 2017
    Posts:
    99
    Messed around a bit with the quality settings and got it to work just like in the editor(I'm sorry I can't say for sure what I did), but now there's some nasty screen tearing when I have the game on full screen.
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,195
    Vsync doesn't work like that. You can't just turn it on, set a target frame rate of 40, and have your game run at 40. What Vsync does is that it waits until the next vertical blank before sending any data to the monitor. For a 60 Hz monitor the vertical blank is 1/60th of a second.

    If the game misses the vertical blank window, it will wait the entire duration of another vertical blank. Basically if the game fails to render 60 FPS (for example it's being told to render slower than that), it will drop down to 30 FPS. If it fails to render 30 FPS, it will drop down to 15 FPS. And so on.

    Since you're forcing a 40 FPS the Vsync window is at best 30 FPS meaning there is an approximately 33ms delay on input.
     
    Last edited: Dec 25, 2019
    ShilohGames and Sigma266 like this.
  6. Sigma266

    Sigma266

    Joined:
    Mar 25, 2017
    Posts:
    99
    Yeah, I have done quite a bit of research since the original post and it looks like I had no idea what I was doing at all. Looks like it's time for balancing stuff. Oof. Thanks for your help, guys. I guess I could say I fixed the issue but not really? haha
     
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,572
    Yeah. Basically, the game will wait for vblank signal, display runs at fixed framerate, and because 60 is not a multiple of 40, you might have some odd frames once or twice per second.

    If you're running on 60 fps display you could use following numbers for fixed framerate: 60, 30, 20, 15, 12, 10, 6, 5, 4, 3, 2, 1.

    Depending on how fps limiter is implemented the game actually may produce roughly 40 fps, but you'll have odd frames with jagged movement once or twice per second, and actual time between frames will not be 1/40th of second.
     
  8. Nicseloli

    Nicseloli

    Joined:
    Oct 9, 2020
    Posts:
    1
    for me setting v sync count to don't sync in the quality settings fixed it.
    Just leaving this out here as I think people are still seeing this thread