Alyssa Knez

Gameplay Programmer

AI PROGRAMMING / UNITY

GOAP System

Unity C# Game AI GOAP Action Planning

This Goal-Oriented Action Planning (GOAP) system allows an AI agent to select goals and execute sequences of actions based on the conditions of the world. Instead of relying entirely on a fixed sequence of behaviors, the agent can calculate a plan, validate action preconditions, and execute actions through a structured update loop.

01

GOAP Agent & Action Execution

CORE AI SYSTEM

Planning & Execution Loop

C# / Unity

The agent calculates a plan when it has no current action, validates the next action's preconditions, and updates execution until the action completes.

GOAPAgent.cs C#
private void Update()
{
    if (!isActive) return;

    // Get a new plan if we don't have an action
    if (currentAction == null)
    {
        CalculatePlan();

        if (actionPlan != null &&
            actionPlan.Actions.Count > 0)
        {
            navMesh.ResetPath();

            currentGoal = actionPlan.AgentGoal;
            currentAction = actionPlan.Actions.Pop();

            if (currentAction.Preconditions.All(
                b => b.Evaluate()))
            {
                currentAction.Start();
            }
            else
            {
                currentAction = null;
                currentGoal = null;
            }
        }
    }

    // Execute current action
    if (actionPlan != null && currentAction != null)
    {
        currentAction.Update(Time.deltaTime);

        if (currentAction.Complete)
        {
            currentAction.Stop();
            currentAction = null;

            if (actionPlan.Actions.Count == 0)
            {
                currentGoal = null;
            }
        }
    }
}

Goal

Create a flexible AI architecture that allows an agent to pursue goals through sequences of actions, rather than relying exclusively on hard-coded behavior sequences.

Implementation

The update loop requests a plan when no action is active, retrieves the next action, and evaluates its preconditions before starting execution. While an action is active, it receives frame updates until it reports completion.

Once complete, the agent stops the action and clears its reference so the next action can be processed. The current goal is also cleared when the plan has no remaining actions.

Key Technical Decisions

  • Plan calculation when no action is active
  • Precondition evaluation before execution
  • Lifecycle methods for action start and stop
  • Frame-based action updates
  • Goal and action state tracking

Result

The system separates planning from action execution, making the agent's behavior easier to organize and extend. New goals and actions can be integrated into the architecture without placing every behavior inside a single monolithic update function.

02

System Architecture

DESIGN OVERVIEW

From Goals to Actions

01 / Goal Selection

The agent identifies a goal to pursue and obtains an action plan associated with it. Goal selection and plan generation determine what the agent attempts to accomplish.

02 / Preconditions

Before an action begins, its preconditions are evaluated. If the conditions are not satisfied, the current action and goal references are cleared instead of starting an invalid action.

03 / Action Execution

The active action is updated each frame. When it reports completion, the agent calls its stop method and prepares to process the next action in the plan.

04 / Plan Completion

When the action stack is empty, the agent clears its current goal. A subsequent planning cycle can then determine what the agent should pursue next.