Daily free asset available! Did you claim yours today?

Breathing Life into Crowds: Goal-Oriented Action Planning (GOAP) in Unity

March 25, 2025

Forget pre-scripted paths and predictable puppets! If you’re tired of crowds in your Unity game that feel about as lively as cardboard cutouts, it’s time to embrace the power of Goal-Oriented Action Planning (GOAP). This isn’t just another AI technique; it’s a paradigm shift that will breathe life into your virtual populations, making them reactive, believable, and genuinely engaging.

The GOAP Revolution: Why It’s a Game Changer

Traditional AI often relies on rigid state machines or complex behavior trees. These systems struggle to adapt to dynamic environments and can result in repetitive, unnatural behavior. GOAP, on the other hand, empowers agents to make intelligent decisions based on their goals and the current state of the world. It’s about giving them the ability to act, not dictating how they act. This difference is critical for creating truly believable crowd behavior.

GOAP Under the Hood: Goals, Actions, and World State

The beauty of GOAP lies in its elegant simplicity. It boils down to three core concepts:

  • Goals: What the agent wants to achieve. Examples include “Find a seat,” “Escape the fire,” or “Buy a hotdog.” These goals drive the agent’s decision-making process.
  • Actions: What the agent can do. Each action has preconditions (what must be true for the action to be executed) and effects (how the action changes the world state). For example, the “Walk to Point” action requires the agent to be alive and changes its location.
  • World State: The current situation the agent perceives. This is a set of facts about the environment and the agent itself. Examples include “Is there a fire?” “Is there a free seat?” or “Is the agent hungry?”

The GOAP planner then searches for a sequence of actions that will achieve the agent’s goal, given the current world state. It’s essentially a sophisticated problem-solving algorithm.

Implementing GOAP for Crowd AI in Unity: Practical Examples

Let’s consider a simple example: an agent wants to “Find a seat” in a crowded stadium.

  1. Goal: Find a Seat
  2. Actions:
    • WalkTo(seatLocation): Precondition: Reachable(seatLocation), Effect: At(seatLocation)
    • LookForSeat: Precondition: True, Effect: SeatFound(seatLocation) (if a seat is found)
  3. World State: IsCrowded, SeatFound(seatLocation), At(agentLocation)

The GOAP planner might choose the following plan: LookForSeat -> WalkTo(seatLocation).

Here’s a simplified C# code snippet illustrating the concept:

// Simplified Action Class
public abstract class GOAPAction : MonoBehaviour {
    public abstract bool CheckProceduralPrecondition(GameObject agent);
    public abstract bool IsAchievableGiven(Dictionary<string, object> worldState);
    public abstract bool RequiresInRange();
    public abstract float GetCost(GameObject agent);
    public abstract Dictionary<string, object> GetEffects();
    public abstract Dictionary<string, object> GetPreconditions();
    public abstract bool Perform(GameObject agent);
    public abstract void StopPerform();
}

This is a very basic example, but it demonstrates the fundamental principles. You’ll need to implement a GOAP planner and integrate it with your Unity project.

Why GOAP is Superior for Crowd Simulation

GOAP allows individual agents to react realistically to changing circumstances. If a fire breaks out, agents can dynamically switch their goals from “Find a seat” to “Escape the fire.” This creates emergent behavior that is far more engaging than pre-programmed routines.

Furthermore, GOAP is highly scalable. You can add new goals, actions, and world states without fundamentally altering the system. This makes it easy to create increasingly complex and believable crowd simulations.

Don’t Be a Sheep: Embrace the Future of Crowd AI

GOAP is not a magic bullet, but it’s a powerful tool that can dramatically improve the quality of your crowd AI. It requires a bit of upfront investment, but the payoff in terms of realism and engagement is well worth the effort. So, ditch the rigid state machines and embrace the dynamic possibilities of Goal-Oriented Action Planning. Your crowds (and your players) will thank you for it.