Course contentsShow
← Unity Game Development
Lesson 117 of 8282. Scripting and LifecycleFree lesson

Optimizing Input Throughput for Performance

Learners will apply best practices to minimize garbage collection and CPU overhead when processing high-frequency player input data.

In this lesson, you will learn how to minimize CPU overhead and memory pressure when your game processes rapid, high-frequency player inputs.

The Cost of Input Polling

When you check for input every single frame—such as using Input.GetAxis or reading an InputAction inside the Update() method—you are technically performing a "poll." While this is common, constantly querying the state of a controller or keyboard can generate unnecessary work for the CPU, especially if you are processing that input to trigger complex logic. Even worse, if you create new objects (like storing raw input data into a new class or list) every time an input is detected, you trigger "garbage collection," which can cause your game to stutter or "hitch" during intense action sequences.

Strategies for Efficiency

To keep your game running smoothly, follow these two best practices:

  1. Cache Your References: If you are using the Input System package, don't look up your InputAction by string name (e.g., "Move") inside Update(). Instead, store a reference to the action in a variable during Awake() or OnEnable().
  2. Use Callbacks Instead of Polling: Whenever possible, use event callbacks (performed, started, or canceled) instead of reading input values inside Update(). This ensures your code only runs exactly when the player interacts with the input, rather than running every single frame whether the player is touching the controller or not.

Here is an example of moving from expensive polling to efficient event-driven processing:

// Avoid this in Update:
void Update() {
    // This runs 60+ times per second, even if no input occurs
    if (inputAction.ReadValue<float>() > 0) {
        PerformJump();
    }
}

// Do this instead:
void OnEnable() {
    // This only runs exactly when the button is pressed
    inputAction.performed += ctx => PerformJump();
}

By switching to events, you drastically reduce the number of calculations your CPU performs and prevent memory churn, keeping your frame rate stable even during complex gameplay.

Key Takeaway: Replace constant frame-by-frame input polling with event-based callbacks to reduce CPU overhead and prevent unnecessary garbage collection.