Course contentsShow
← Unity Game Development
Lesson 564 of 8288. Artificial IntelligenceFree lesson

Managing Action Selection with Utility Selectors

Build a selection engine that evaluates multiple competing actions and executes the one with the highest calculated utility score.

In this lesson, you will learn how to build a robust selection engine that evaluates multiple competing actions and executes the one with the highest calculated utility score.

While traditional planning systems like GOAP focus on logical chains of actions, Utility Selectors allow your AI to prioritize behaviors based on nuance and urgency. Rather than asking, "Can I do this?" the selector asks, "How much do I want to do this?"

The Utility Selector Pattern

At its core, a Utility Selector is a collection of Consideration objects. Each Consideration evaluates a specific aspect of the game world (e.g., "how hungry am I?" or "is there an enemy nearby?") and outputs a normalized value between 0 and 1. By combining these, you calculate a total utility score for an action.

Consider this simplified selection loop:

public Action GetBestAction(List<Action> actions) {
    Action bestAction = null;
    float highestUtility = -1f;

    foreach (var action in actions) {
        float score = action.CalculateUtility();
        if (score > highestUtility) {
            highestUtility = score;
            bestAction = action;
        }
    }
    return bestAction;
}

Balancing Competing Needs

The power of this system lies in the CalculateUtility method. An action shouldn’t just rely on a single variable. For instance, a "Heal" action might be calculated by multiplying your health deficiency (normalized 0-1) by the current danger level. If the player is low on health but safe, the utility remains moderate. If health is low and enemies are closing in, the utility score spikes, forcing the AI to prioritize survival over combat.

By cycling through your list of actions every few frames, the selector creates smooth, fluid transitions. If a "Patrol" action has a utility of 0.4 and a "Reload" action jumps to 0.7 due to low ammo, the AI instantly pivots to the higher-scoring task without needing a complex state machine trigger.

Key Takeaway: Utility Selectors enable believable, nuanced AI behavior by dynamically comparing the urgency of multiple possible actions based on real-time sensory data.