Super-fast input in Unity!

So ever since I mentioned that for SWIFT☆STITCH, I had input that was independent of framerate, I rarely go a few days  without somebody asking just how I did it, so to save myself lots of typing in the future, here’s the explanation 🙂

(UPDATE: Whilst this approach is an improvement on regular input in unity, it seems you can’t know what time inputs occurred at exactly, see here)

Why do it?

So here’s the thing, most input in unity is updated at the same rate as frames are drawn. here’s the rough process as simple as I can put it:

  1. Unity collects all the user’s inputs, and updates the Input class accordingly
  2. Unity runs your Update() functions, then draws a frame.
  3. Unity clears the Input class, and we go back to step 1

this means that when you ask for ‘Input.GetKeyDown(“space”)‘ you are asking if the space key has been pressed since the last frame. (which is why you shouldn’t look for such input in FixedUpdate() or OnGUI(), since it happens independent of framerate and you can miss it).

this is all fine and super cool (really!), so why would you ever need input to work any other way? well, what if you want user to be able to play the game faster than it is drawn?  in the case of SWIFT☆STITCH, even at framerates over a hundred, input was hindering me. I could click the mouse for a fraction of a second (less than a frame), but the ship would still react as if that input lasted an entire frame. in other cases, what if you want to be able to record multiple presses per frame?

these are rare things, and I’ve only ever needed to separate input and framerate for one game so far. however, I think it’s useful to consider frames as nothing more than snapshots, an approximation of what the world looks like. the user sees the frames and the game world is constructed from them in their head. the user isn’t waiting for frames to come around before acting, they act when it feels right to them. they are playing the game in the world that is in their heads, frames are just our way of assisting them in visualising that world.

as such, it’s better to not have the game world update at the rate of something so arbitrary as how fast a computer and screen can make pretty colours, but at the rate of the world we are trying to represent, as unlimited by technology as possible.

The framerate independent model;

so my approach is this;

  1. we grab input at the very moment the user makes it, we record all we care about and the precise time they happen
  2. when our Update() functions roll around, we simulate the game world taking into account all the inputs we have and exactly when they happened
  3. we clear our record of inputs before the frame is drawn, and we start over at 1 again

using this model, the user is interacting independent of the framerate, the world has no quirks based on the framerate (since we are using real time and not delta time).

How to do it in Unity:

so as mentioned above, the Input class is tied in to the framerate pretty closely. if you want to capture input as it happens we need to look elsewhere, and the place my come as a surprise; OnGUI().

that’s right, I didn’t typo, you didn’t misread. I said OnGUI()! you see, OnGUI() is triggered a bunch of times, by many different things. one of them, is user input. if the user presses space a hundred times since the last frame, OnGUI() will have run *at least* 200 times since the last frame too (remember, key releases are also 😉 ).

what we need is Event.current. drop this inside OnGUI() and you can find out just what event it was that triggered the function this time. and if it was keyboard or mouse input, the relevant information is passed along as part of Event.current. (no gamepad input here sorry, you’ll have to look elsewhere to make that framerate independent).

so, there you have it, you have captued input precisely when it happened, framerate has nothing to do with it!

but… you still don’t know WHEN it was pressed. like the Input class, most of the Time class you are familiar with is tied to framerate (yep, it’s only really Time.deltaTime and Time.time, but what else have you used lately? :P). what you really want, is Time.realtimeSinceStartup.

given you now have both a way of capturing input independent of framerate and recording precisely when they happen, the only step left is simulating the game world based on this information.

I suggest you add this information to a list, and when the next Update() rolls around, you step through this list, input by input, and simulate the game up to the time the next input happened (and not forgetting to simulate the time between the last frame to the first input, and the time from the last input to the present). then clear the list and you’re good to go again 🙂

Some excess blah blah:

  • yes, it takes time for a user’s input to get from their brain, to their hands, through the keyboard and so on before it gets to the game. we aren’t recording input the instant it happens, but we’re doing it as damn close as we can 🙂
  • really, the odds you even need to take advantage of framerate independent input are tiny. it’s a bit of extra hassle you don’t need unless your game plays so fast that you need crazy fast input too.
  • I do think having framerate independent input is a little bit of an ideal when it comes to interactive worlds… but a framerate of infinity is also a similar ideal. this one is more attainable sure, but if a player never notices then it doesn’t matter too much. we’re magicians after all! why do real magic when a simple trick has the same effect? 🙂
  • the unity docs have the following caution about realtimeSinceStartup: “Depending on the platform and the hardware, it may report the same time even in several consecutive frames” I’ve not had any such problems myself on PC/Mac (iOS is another story though). so be aware of that I guess
thanks for reading this far, weirdo! 🙂


10 Comments on “Super-fast input in Unity!”

  1. Dirk Van Welden says:

    Interesting article Sophie!

  2. col000r says:

    interesting read, thanks!

  3. Dataferret says:

    Interesting use of ongui. Thanks for sharing.

  4. I’m 99.999% sure that input is buffered for frames even when you’re using OnGUI.

    We queue them in the Internal GUIManager, then simply send all queued events straight after each other just before sending the Repaint Event.

    (That’s how it was when I wrote it a couple of years ago, and I would be very surprised if standalone players changed the behaviour. )

    • Sophie says:

      really? I did a bunch of tests to make sure it was working how I thought. now I’m going to have to do them again to check if I’ve gone wrong anywhere 😛

      • Fatster says:

        OK, so what’s the outcome here Sophie and Nicholas?!?!!!!!!!! Thanks

        • Sophie says:

          Nicholas is right! however my approach does let you know if multiple inputs have happened on a single key between frames, you just don’t know exactly *when* between the frames they happened.

          so it’s a little more accurate, but still not perfect ^__^;

  5. SmoMo says:

    Yay for the nice article!
    I’ve not done that in Unity before ( didn’t know how ) but I’ve buffered input before in a game and that had a totally positive effect because you could tap out sequences happy to know the order wasn’t going to change.

    P.S. The moon is hollow, probably some kind of Space Egg

  6. duhprey says:

    Although human reaction time isn’t that high (it’s usually measured in half seconds), we have really good prediction systems. So we can tell if I clicked, and the *system* isn’t reacting to the proper millisecond, it’ll look sluggish. I’ve seen the reaction time argument used to say we don’t need such high frame rates, but it really has very little to do with reaction time… besides the eye is much slower. You could run your renders at 24fps and if you capture input at 100-150 fps it’ll feel responsive.

  7. duhprey says:

    Also, thanks. OnGUI… very slick.

Leave a Reply

Your email address will not be published. Required fields are marked *