Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Keyboard Input lag

Discussion in 'Editor & General Support' started by dgoemans, Nov 13, 2010.

  1. dgoemans

    dgoemans

    Joined:
    Nov 13, 2010
    Posts:
    8
    Hi All,

    I'm working on a game prototype in Unity, simple 2D platformer gameplay, using the keyboard as the primary controller. The problem i'm continually running into no matter what i do is some amount of input lag from the keyboard. Using the FPS walker script as is ( and later modifying it to make sure that the delay isn't cause by the 'if(grounded)' check ), i sometimes get delays in jumping. It's a few hundred milliseconds, but it's definitely noticeable in my game. Is this a Unity core issue, that the engine lags a bit and that affects input? I thought for a while that it was just on my netbook, but after i sent the link to a few friends, they also mentioned it's a bit annoying and basically makes the game unplayable at points. I really like Unity, so i'd hate to have to leave it for something like XNA for this game.

    I'm pretty technical ( C++ programmer by day ), so please don't hold back in any answers :D

    Game is up here and the controls are standard Horiz Axis and space for jump. Note that on some jumps, you hit space, but it doesn't respond immediately or at all.
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I didn't notice any such problems (Windows XP, IE).

    There are no 'core issues' related to input lag in Unity that I'm aware of. If you're seeing delays in the hundreds of milliseconds, then I would imagine something else is going on.

    Have you observed these problems specifically in the web build you posted? I played through both levels; all key presses were detected and there was no noticeable delay. What hardware configuration(s) are you and the other testers testing on?

    This may be overly obvious, but make sure you're checking for the jump button in Update() rather than FixedUpdate(). You might also try creating a new project that does nothing but, say, change the color of a quad when you press the button in question, and see if you encounter the same problems.
     
  3. dgoemans

    dgoemans

    Joined:
    Nov 13, 2010
    Posts:
    8
    Hi Jesse, thanks for the fast response.

    I'm using a netbook ( dual 1.2Ghz Atom, with GeForce 9400M, 2GB Ram ), Windows 7 and Chrome. The friends were using something a lot more advanced ( Core2 Duo or Quad, with > 2GB ram ), Windows 7 and Chrome ( although this might suggest Chrome is the issue ). I've just retested the web version i posted in IE, and had similar results. Basically what happens is that sometimes it registers my key just too late or it holds in a key just too long, making me think it's lag related. It's especially noticeable in parts with lots of small platforms. Sort of seems like the input is only being passed to the API the frame after it's registered, so lag in the frame before delays the passthrough. I've also noticed from my own tests on a faster machine that it's less noticeable. That said, the game is certainly nothing complicated at the moment, so there shouldn't really be any lag.

    I am doing the check in Update, not FixedUpdate. I'll also create a mini sample app and try to reproduce it.

    Update:
    Made the sample and still get the same results:
    http://davidgoemans.com/Lagtest/WebPlayer/WebPlayer.html
    Basically, tapping either space or d a lot ( waiting for the cube to return to rest between taps ) causes the delay, one time in every 20 or so taps. Script below

     
    Last edited: Nov 13, 2010
  4. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,546
    Unity webplayer crashes in Chrome for me, I'm quite surprised your friends can run it with Chrome
     
  5. dgoemans

    dgoemans

    Joined:
    Nov 13, 2010
    Posts:
    8
    Always run great in Chrome for me, i'd suggest uninstalling and reinstalling your web player :)
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I don't get any delays or any lack of response. OS X, Safari 5.

    --Eric
     
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    No delays but it sounds like you're running it on a really low hardware computer and maybe the GC is kicking in at points where you experience the lag.
     
  8. dgoemans

    dgoemans

    Joined:
    Nov 13, 2010
    Posts:
    8
    Yes, i think you're right, but that still leaves 2 questions:

    The first is why the input is only being handled 1 frame later, in other words, if it GC's on a frame where i'm pressing a key, the GetButton call only responds on the next frame.

    Secondly, why is any GC happening? My little script here only creates Vector3's which are all structs. The engine itself really shouldn't be making any garbage.
     
  9. JJoosten

    JJoosten

    Joined:
    Nov 14, 2010
    Posts:
    1
    I have tested it on my PC and I noticed the lag to. Its a high end gaming PC so GC shouldn't give any issues.
     
  10. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    This does sound a little odd (especially since some people are observing lag on their systems and some aren't).

    I'm curious though, how are you determining that there's a 1-frame delay exactly? I know even very small delays in responsiveness can be noticeable, but it seems that a 1-frame delay in input might be potentially unavoidable with any engine (and on any platform).

    In most cases, a game is going to have a main loop that runs through a series of operations in sequence. At some point in that sequence, events will be polled for/received and processed accordingly. If you happen to (e.g.) press the space bar right at that moment or right after it, that event likely won't be processed until the next update cycle (if my logic is correct, that is); in other words, a potential delay of 1 frame or update might be unavoidable, essentially.

    If it really is only one frame/update, then I'm curious as to how that's being measured (and I'm a little surprised that people are finding it so noticeable). If it's *more* than one frame/update, then it would seem there's some problem that's manifesting on some people's systems but not on others'. (At least that's the way it seems to me, but I may be overlooking something obvious somewhere.)
     
  11. RKSandswept

    RKSandswept

    Joined:
    Apr 26, 2013
    Posts:
    22
    I have a similar problem. I think it is than in unity the order of calling scripts Update methods is undetermined.

    So the typical chain of input is Raw Input Mapper (we use cInput) ==> InputController ==> Character Motor ==> Render to Screen.
    There may be more scripts in that chain that I am not aware of.
    Any way if your luck they all get called in that order in a single tick (good), but if they were called in the revers order then each stage in that chain is one tick, so it might be 5 frames from hitting a mouse button until the gun fires.
    At 30 fps this is 1/6 second.

    I will post s solution if I find one.

    Update:
    I did a scene with only a sphere, a camera, and a directional light.
    Then a script on the sphere makes the sphere double in size when you hold the mouse down...
    Code (csharp):
    1.  
    2.     // Update is called once per frame
    3.     void Update () {
    4.         if(Input.GetMouseButton(0))
    5.         {
    6.             transform.localScale = new Vector3(2.0f, 2.0f, 2.0f);
    7.         }
    8.         else
    9.         {
    10.             transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
    11.         }
    12.        
    13.         // burn CPU to get a more typical in-game delay.
    14.         for(int i=0; i<100000; i++)
    15.         {
    16.             float f = Mathf.Sin(i);
    17.         }
    18.     }
    19.  
    There is no perceptible delay between pressing the mouse and seeing the sphere change size. If I bump the 100000 to 1000000 then there is a very slight delay as expected. But that is not an input delay.

    More...

    Adding a 5 script 'bucket brigade' where a flag for the button press is relayed along made no perceptible difference.
     
    Last edited: Aug 4, 2013
  12. muzzl

    muzzl

    Joined:
    Feb 2, 2018
    Posts:
    3
    This is still the first google hit for unity input lag. I ran into now though and what I found the problem to be in my case was that I was using Input.GetAxis(...) and expecting it to jump instantly from 0 to 1 when using the keyboard, but actually the value changes from 0 to 1 over about 10 frames with several values in between.
     
  13. muzzl

    muzzl

    Joined:
    Feb 2, 2018
    Posts:
    3
    And the solution that worked for me: Use Input.GetAxisRaw() instead of Input.GetAxis(). Huge difference!
     
    LuyangMoo and giggioac like this.
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You could just edit the axis in the input manager to get rid of the smoothing.

    --Eric
     
    2a0c40 likes this.
  15. unic

    unic

    Joined:
    Oct 24, 2016
    Posts:
    3
    I have the same problem with the input lag. My laptop runs my game at ~70 FPS, and I expected a in lag about 1 frame, but it's much greater. It's nicely noticeable when I show hardware cursor and make some object to follow the cursor - I can see how the object tries to overtake the hardware cursor's position during next several frame (not just one, as I expected).

    Before Unity I did make games in Game Maker 8 and I had similar issue. But in case of GM8 I had lag in just 1 frame running a game at 60 FPS. But in Unity it's much bigger; experienced gamers, who play really fast games which require fast reaction, will definitely notice such big lag.

    I understand that Unity and GM8 both have different implementation of how the input events are processing, but anyway it's a pity that a Unity game at 70 FPS has much slow input response than a GM8 game at 60 FPS; having this I feel like I play a game on some cheap mobile device that has noticeable lag because of slow processing of tap events.
     
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's probably a programming or setup error somewhere. No such lag here, or indeed any Unity game I've played (which by now is a lot).

    --Eric