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

Debugging GOAP Plans with Visualizers

Build custom editor tools to visualize the current plan and the chain of state transitions for easier debugging of AI behavior.

In this lesson, you will learn how to create a custom Editor window that exposes the inner workings of your GOAP planner, transforming abstract node chains into readable visual data.

The Problem with Black-Box Planning

When using A* search to generate a GOAP plan, the logic happens in a split second. If an NPC stands still or performs an unexpected action, it is difficult to know if the planner found an invalid path, failed to find a goal, or if the "World State" is incorrectly reporting conditions. By visualizing the plan, we turn this "black box" into a transparent sequence of state transitions.

Building the Visualizer

To visualize a plan, you need to access the Queue<GoapAction> currently being executed by your agent. We can use a custom EditorWindow to iterate through this queue and display the path taken.

[CustomEditor(typeof(GoapAgent))]
public class GoapVisualizer : Editor {
    public override void OnInspectorGUI() {
        base.OnInspectorGUI();
        GoapAgent agent = (GoapAgent)target;

        if (agent.CurrentPlan != null) {
            EditorGUILayout.LabelField("Current Plan Steps:", EditorStyles.boldLabel);
            foreach (var action in agent.CurrentPlan) {
                EditorGUILayout.LabelField($"> {action.name}");
            }
        }
    }
}

Visualizing State Transitions

Beyond the action list, the most powerful debugging tool is visualizing the State Chain. During the planning phase, your algorithm generates a series of "World States" representing what the world should look like after each action.

You should cache the WorldState associated with each node in your A* search. By drawing these as nodes in a GUI window using Handles.DrawLine or GUI.Box, you can see exactly where a precondition failed. If an NPC cannot find a "Get Wood" action, your visualizer will show you the exact state (e.g., HasAxe = False) that blocked the chain, saving you hours of stepping through code.

Key Takeaway: By caching the plan and its associated state transitions into a custom Inspector or Editor Window, you convert invisible planning logic into a step-by-step diagnostic roadmap that highlights exactly why an AI chose—or failed to choose—a specific action.