In this lesson, you will learn how to replace linear utility calculations with mathematical response curves to create AI that behaves with natural, non-linear nuance.
Up until now, your AI likely used linear thresholds to make decisions—for example, if health is below 30%, flee. While functional, this creates robotic, binary transitions. In reality, an NPC’s urgency should shift subtly as a situation progresses. By mapping your input variables through an AnimationCurve, you can dictate exactly how "important" a specific input feels at different stages.
Mapping Inputs with AnimationCurves
An AnimationCurve allows you to define a graph where the horizontal axis (X) represents your normalized input (e.g., 0.0 to 1.0 for health) and the vertical axis (Y) represents the final utility score (0.0 to 1.0).
By tweaking the tangent handles in the Inspector, you can create a "soft start" where the AI ignores a low-priority need, followed by an "exponential spike" as the need becomes critical.
public class UtilityEvaluator : MonoBehaviour
{
// Define the shape of the response in the Inspector
public AnimationCurve responseCurve;
public float GetUtilityScore(float input)
{
// Clamp the input to ensure it stays within the curve's range
float normalizedInput = Mathf.Clamp01(input);
// The curve transforms the raw input into a nuanced score
return responseCurve.Evaluate(normalizedInput);
}
}
Why This Matters
If you are calculating the "Flee" utility based on distanceToEnemy, a linear approach might make the AI panic too early. By using an AnimationCurve shaped like a sigmoidal (S-curve) function, you can ensure the AI remains calm when the enemy is distant, hesitates as they approach, and only registers a "High Utility" score for fleeing once the enemy crosses a specific danger threshold.
This allows you to tune the "personality" of your AI—making them appear cowardly, brave, or unpredictable—simply by dragging the curve handles rather than hard-coding magic numbers into your logic gates.
Key Takeaway: Response curves decouple your raw data from decision urgency, allowing you to fine-tune AI personality through visual mapping rather than rigid, linear conditional logic.