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

Managing AI Awareness Thresholds

You will implement a dynamic suspicion meter that accumulates sensory data over time before transitioning the AI into an alert state.

In this lesson, you will learn how to implement a dynamic suspicion meter that forces an AI to accumulate sensory "evidence" over time before transitioning from an idle state to a fully alert state.

The Concept of Sensory Accumulation

Rather than having an AI snap instantly into a "Chase" state the moment a player enters a detection zone, we use an Awareness Threshold. Think of this like a bucket: every tick the player spends inside the AI’s field of view or auditory range, we add a certain "weight" to the bucket. If the bucket overflows, the AI transitions to its alert behavior. If the player hides, the bucket slowly drains, representing the AI losing interest or "forgetting" the distraction.

Implementation Logic

We manage this using a simple float variable in our blackboard system. By incrementing this value based on the strength of the sensor trigger (e.g., visual contact adds 2.0/s, hearing a sound adds 5.0), we create a more natural, believable AI response.

public class AwarenessManager : MonoBehaviour {
    public float suspicionMeter = 0f;
    public float threshold = 100f;
    public float decayRate = 10f;

    void Update() {
        // Slowly lose suspicion if not actively being detected
        suspicionMeter = Mathf.Clamp(suspicionMeter - (decayRate * Time.deltaTime), 0, threshold);

        if (suspicionMeter >= threshold) {
            // Trigger transition to Alert State via Blackboard
            blackboard.SetBool("IsAlert", true);
        }
    }

    public void AddSuspicion(float amount) {
        suspicionMeter += amount * Time.deltaTime;
    }
}

Balancing the System

To make this effective, you should scale the AddSuspicion input based on distance. An enemy close to the player should fill the meter much faster than one far away. By integrating this into your existing FSM or Behavior Tree, you prevent "flickering" states where an AI rapidly toggles between states due to frame-perfect line-of-sight calculation errors. Instead, the AI maintains a sense of persistent curiosity, requiring the player to stay hidden long enough to "clear" the suspicion.

Key Takeaway: By decoupling raw sensor detection from state transitions using an accumulative meter, you create AI that feels thoughtful and reactive rather than purely robotic.