Does unity have functionality similar interrupts?

Coming from PIC microcontroller background I generally like to do things using interrupts. For example: when an event occurs (eg. button press to move left) an interrupt is generated and the program enters a interrupt service routine which tells the program to do something(move the player left). The interrupt stops whatever the program was doing and processes the interrupt and then returns back to where the program was.

I was wondering if unity had a similar functionality so I can instantly process user inputs to move my character as soon as the key is pressed rather than having to use the update function (polling if I am correct?). The keypress will essentially work as a trigger that stops the script and processes the user input and then returns to running through the script.

Would coroutines somehow be able to help me be able to do this?

In PCs almost everything works with polling. Things like hardware interupts (there are no “software” interupts anyway) belong totally to the inner core of your operating system. They are like 2 - X times abstracted / buffered / prehandled by device drivers, virtual device drivers, the messaging system and that’s just the OS part. You really never work with “interupts” when you program at application level.

I like PICs too, i’ve done a lot funny things, but this is something completely different.

Games are almost always processed in frames. A frame is one complete iteration of the main gameloop. In Unity you don’t have direct access to the gameloop, but Unity will invoke callbacks in your scripts when it’s time to. An important thing is reliability. Unity provides states which are ensured that they stay constant during a frame. Interupts or threads can lead to unpredictable results since you can’t determine when a change happens.

So for example when a keydown occures Unity will “detect” the keydown event at the beginning of a frame. Unity sets the keydown state for this key until the next frame. So no matter where within the current frame you check the state it will be constant.

I’ve never had any problems with such a small input delay. I’m a quite advanced Quake3 / live player and i’ve never had experienced any visual lag from input delays. Quake actually works like this: At beginning of frame read inputs. Buffer or queue the input until the next network update. Send the input to the server. The server buffers the input again until the next server physics frame. The server processes the input, moves the player calculates physics and prepares a snapshot of the current “situation” (all movable items including players that are visible) and buffer the snapshot. According to the configured sendrate the server sends the snapshot to the player. The player buffers the snapshot. Now, maybe some frames later on the client, at the middle of a frame the client takes the last 2 snapshots and interpolating the position of all items at the “virtual time”. This time is actually lagging behind on purpose to provide fluid motions on the client.

I would consider Quake as one of the fastest (and best) FPS games out there. You’re moving at around 600 units per sec. and you don’t “feel” any delay at least when you have a moderate network connection / distance to the server.

If you watch a recorded demo in ultra slowmotion you can see that the major part of the player reaction time to a visual / auditive event is the player itself. Under “normal” conditions you usually calculate with a reaction time of 1 sec. In quake, due to the fast gameplay, you reach far better values, but nothing even close to 16ms.

If you watch “real” pro players it seems they react way faster, but that’s just practise and mainly prediction.

Polling is the way to go. Most Unity API calls must happen on the same thread, so interrupts wouldn’t work anyway without some scheme to marshal the processing onto the main Unity thread.

If you really want to use an event/interrupt structure, you could write an input handler to simulate that. Register event handlers with the system, call a polling method in Update, and the system would call out to your code in an interrupt-like manner.

Really though, polling input is such a trivial thing, do you really want to make it more complex than it needs to be?

You can use Start a coroutine. Then do something like this.


private IEnumerator Interrupt() {
while(true) {
// Some interrupting action
yield return null;