NPC AI with GOAP 1: Understanding and Approach

A series to implement Goal Oriented Action Planning from scratch in unity.

Pankaj Basnal
5 min readJun 27, 2021


Photo by Markus Winkler on Unsplash

What is Goal Oriented Action Planning?

It is an AI planning technique where we encode Goals and Game State in a format which can be understood by code. These are then consumed by a Planner to create a list of actions which can achieve those goals when executed.

An Action is a process that an AI agent can execute to make some changes in the game state. These changes are called effects. Every action also has some preconditions which is state of the game that should be true before executing it.

Note: giving npc brain can cause a lot of havok. Radiant AI used to kill other NPCs. Guards would kill anyone caught stealing apple. Some NPCs killed important NPCs and blocked quests. This is why control of the actions is important. A balance between Smart AI and developer driven scripted AI is required to ensure a good game experience.


A high level view of what we will try to achieve. There will be 2 important objects, AI Agent and Smart Object.

AI Agent

Any Non Playable Character(NPC) can become an AI agent if it has the following important behaviors-

  1. Fact: A fact is piece of information which describes the perceived environment of the agent. It is simple Key Value data. Keeping it simple will grant us a lot of flexibility.
  2. State: Collection of facts which describe the environment in which the agent is present.
  3. Memory: This is the collection of objects that the NPC has encountered and can interact with.
  4. Goals: Goals are desired state which the NPC wants to achieve.
  5. Planner: This behavior searches through the available actions and creates a list of actions which should be executed by the NPC to achieve it’s assigned goals.
  6. Action: It’s a unit of behavior that an NPC can execute to modify its state.
  7. Sensors: Sensors help NPC to see and observe it’s environment.

Smart Objects

Any interactable object that is present in the game. For example a door, a chair, a keypad, a weapon, a vehicle etc.

  1. State: State will contain facts about the object itself. For example door is open or door is close.
  2. Actions: Actions that are present on the smart object are executed by the AI Agent during interaction. This is an extensible way of adding new objects and the actions that an AI Agent can perform on/with them. So, when an Agent is interacting with the smart object, it’ll search through the actions that are available on the object and execute an action which helps the agent achieve it’s desired goal.

An Example Scene

Agent Actions

Below are the list of actions that I’m thinking of. However, we will start small and use only 2 or 3 out of the list. Once our understanding gets better about the performance and process of the system, we can think about implementing other actions.

Actions that don’t need a target

  1. Sleep: Even NPCs have their labor rights.
  2. Reload: A full gun is better than an empty one.
  3. Idle: Even NPCs deserve some idle time for self reflection and wonder about their existence inside a software written with 0’s and 1's.
  4. Heal: To increase health if the NPC is damaged.
  5. Rest: To increase stamina.
  6. Check messages from other NPCs: Will they overtake us if they can collaborate? A question for future me.
  7. Explore Location: This action will analyze the whole environment which is accessible by sensors and add all the objects in memory for further use by planner and other actions.

Actions that need 1 target

  1. Consume: To consume an item which can increase health or stamina or different attributes.
  2. MoveTo: This action will move the AI Agent to specified position.
  3. Interact: This action will interact with a smart object. Smart objects will expand agent’s action vocabulary by allowing it access to the actions that the object has.
  4. Explore Object: This action will store an object’s information in memory.
  5. Attack: To inflict damage on player. Should be extensible enough to damage other NPCs. Rats can be a problem.
  6. Shout: To vocalize their plan and ensure that player knows what the NPC is about to do. This will make players feel superior and smarter. Apparently if this is not done, players think that the game is cheating and if we do this, they say that AI is stupid -_-

Actions that need multiple targets

  1. Throw: Why shoot if you can blow them up with a grenade 😈.

Agent Architecture

Diagram shows different components of the AI Agent.

But how would an action work?

Not all actions are possible all the time. For example, agents can’t open a door which is already open, or they cannot move to a location that they are already in. To handle these conditions and checks, PreConditions will be evaluated before simulating the action.

Once we know that we can execute an action, we want to simulate the effects of the actions during planning. Simulation should return an updated copy of the state to the planner so that the planner can decide if the action should be executed or not.

After planning is over, we will use the Execute method of the action to perform expected changes to the state.

Putting it all together

Planner will use all this information to generate a graph of actions and updated states. To do this, it’ll use memory and current state of the agent. To prepare the list of actions it’ll search through this graph to find a path which can lead to our goal state.

What’s next?

In the next blog we will see how these actions will get executed and how we can optimize graph search to find the best path to achieve goals.