How an NPC builds a plan with GOAP.

A ranger needs a lit campfire. Give it a goal and a few possible actions, then let it work out the steps.

One goal. More than one way there.

Try lowering the gathering cost to 2. Then remove the fallen wood. The goal stays the same; the cheapest available plan changes.

The current plan

Pack: empty. Campfire: unlit.

  1. Pick up axeCost 2
  2. Chop woodCost 1
  3. Light campfireCost 1

3 actions. Remaining cost 4.

A symbolic browser example, not a Unity benchmark or a shipped-game case study. Trees and a means of ignition are always available. Movement, animation, danger and other agents are outside this model.

Describe the goal, then the actions.

Goal-oriented action planning (GOAP) separates what an agent wants from how it gets there. A goal describes a desired state. Actions describe their requirements and expected changes. A planner searches for a sequence that connects the current state to that goal. Jeff Orkin’s GDC paper on F.E.A.R. is a useful reference for this approach.

Here, the goal is simply fire = lit. Five true-or-false facts describe the world: whether the ranger carries wood or an axe, whether the fire is lit, and whether fallen wood or an axe is available at camp.

The four actions in this example
ActionPreconditionsEffectsCost
Gather fallen woodFallen wood available; no wood carriedCarry wood; fallen wood is used up1–8
Pick up axeAxe at camp; no axe carriedCarry axe; remove axe from camp2
Chop woodCarry axe; no wood carriedCarry wood1
Light campfireCarry wood; fire unlitLight fire; consume carried wood1

The shortest plan is not always the cheapest.

At the starting cost, gathering and lighting take two actions but cost 5. Picking up the axe, chopping and lighting take three actions but cost 4. Lower gathering to 2 and the direct route wins at a total cost of 3.

This example uses forward uniform-cost search: explore the cheapest accumulated path first, try actions whose preconditions match, and keep the best cost found for each state. With positive costs and this complete five-fact model, the first goal removed from the search queue has the lowest total cost. Equal-cost routes are both valid; action ordering breaks ties here.

The planner searches up to 32 expanded states. Running out of states means the goal is unreachable in this model. Hitting the search budget means only that the search stopped; a plan might still exist. A campfire that is already lit needs an empty plan, with cost zero.

A small Unity example.

The two C# files below reproduce the same domain and planner. Copy them into a Unity project, attach CampfireExample to a GameObject, and choose Print campfire plan from the component’s context menu. Change its Inspector fields and run it again to compare plans.

var world = World.LooseWood | World.AxeAtCamp;
var result = GoapPlanner.Plan(world, gatherCost: 4);
if (result.Status == PlanStatus.Found)
{
    foreach (var action in result.Steps)
        Debug.Log($"{action.Name}: cost {action.Cost}");
}

The planner has no Unity dependency. It uses a small list as its search queue and copies paths for readability; larger domains need a more deliberate allocation and search-budget strategy. The component prints a plan. It does not navigate or animate a character.

Planning predicts. Execution must check.

“Do next action” applies an action’s symbolic effects immediately. A game executor needs a slower loop: validate the next action, start it, wait for success, observe the resulting world, then continue or replan. If another agent takes the axe, discard the invalid step and search again from the observed facts.

For example, Unity’s NavMeshAgent.SetDestination returning true means a destination request was accepted. It does not mean the character arrived; path calculation can still be pending. Likewise, requesting a walk to the wood must not immediately set hasWood.

Try this failure case: reset, then remove both fallen wood and the axe at camp. There is no valid plan. Put wood directly in the pack and the ranger can light the fire. After picking up an axe, removing the camp’s spare axe will not remove the one already carried.

Use planning where the choices justify it.

I would start with a small state machine for a character that only patrols, chases and attacks through a few clear transitions. GOAP becomes an option when the interesting problem is combining reusable actions as resources and opportunities change. The tradeoff is another model to maintain, search and debug.

A planner can choose an action while a state machine handles its execution. Orkin’s paper discusses planning alongside a small state machine in F.E.A.R.; these approaches can work together. For this ranger, useful debugging starts with the observed facts, chosen goal, rejected preconditions and total plan cost.

Your next world
starts with a conversation.

Share your vision. I’ll bring it to life.

The idea, the challenge, the next milestone. That's a good place to start.