Summary of "GOAP Enemy AI FULL IMPLEMENTATION | AI Series 50 | Unity Tutorial"

Overview

This video demonstrates a full implementation of an enemy AI (“llama”) using a GOAP (Goal‑Oriented Action Planning) system for Unity (Crash Conagen’s free Apache‑2.0 implementation). The finished AI supports three goals — Wander, Kill Player, and Eat — with actions including wandering, melee attack, ranged spit, and eating. The system uses sensors and world keys to inform planning and choose actions based on costs, preconditions/effects, and in‑range thresholds.

The tutorial covers architecture and coding patterns (factory & builder), ScriptableObject configuration, dependency injection for non‑Mono classes, movement via NavMeshAgent, animation integration, object pooling for projectiles, and debugging with a GOAP node viewer.


Key concepts


Step‑by‑step methodology

  1. Install GOAP package

    • Add the Unity package by Git URL (requires Git installed) and import samples.
  2. Project organization

    • Create script folders: Actions, Behaviors, Factories, Goals, Sensors, Targets, WorldKeys, Config.
    • Keep consistent folder structure (GOP subfolder and subfolders per type).
  3. Create core GOAP classes

    • Goals: extend GoalBase to create WanderGoal, KillPlayerGoal, EatGoal.
    • Targets: extend TargetKeyBase to create WanderTarget, PlayerTarget, GrassTarget.
    • WorldKeys: extend WorldKeyBase to create keys like IsWandering, PlayerHealth, Hunger, Saliva, PlayerDistance.
  4. Sensors

    • WanderTargetSensor: LocalTargetSensorBase — pick a random navigable point (use NavMesh.SamplePosition; retry attempts) using wander config.
    • PlayerTargetSensor: LocalTargetSensorBase or event‑driven collider sensor — find player via OverlapSphere or trigger.
    • GrassTargetSensor: LocalTargetSensorBaseOverlapSphere, sort results by distance, return nearest grass.
    • HungerSensor: LocalWorldSensorBase — read hunger value from HungerBehavior (Mathf.FloorToInt).
    • SalivaSensor: LocalWorldSensorBase — read saliva from SalivaBehavior.
    • PlayerDistanceSensor: LocalWorldSensorBase — return distance to player when in sensor radius (inject attack config).
    • Prefer event‑driven enter/exit sensors (trigger collider) to avoid expensive per‑frame overlaps.
  5. Actions (extend ActionBase)

    • Define a common action data class implementing IActionData: includes ITarget and a timer.
    • WanderAction
      • Data: timer controlled by WanderConfig (injected).
      • Start: set timer to random range from config.
      • Perform: decrement timer; return Continue until timer ≤ 0 then Stop.
      • Movement handled by shared AgentMoveBehavior.
    • MeleeAttackAction
      • Data: target, timer, animator references (use [GComponent] attribute to auto‑get Animator).
      • Inject AttackConfig for melee radius, cost, delay, masks.
      • Perform: check target distance ≤ melee radius, animate/look at target, manage attack timing.
    • SpitAction (ranged)
      • Extends attack data, includes refs to SpitBehavior and SalivaBehavior ([GComponent]).
      • Use object pool to spawn projectile prefab from AttackConfig.
      • Register to SpitBehavior animation event (spawn trigger) so action receives spawn timing.
      • Start: subtract saliva (cost) and register event handler; Perform handles look/animate; End unregisters.
    • EatAction
      • Data includes HungerBehavior.
      • Start/Perform: stop hunger increment (disable HungerBehavior), animate eating, restore saliva and lower hunger over time using BioScience config rates. End re‑enable hunger behavior.
  6. Behaviors (MonoBehaviours)

    • AgentMoveBehavior
      • Requires NavMeshAgent, Animator, AgentBehavior.
      • Listens to AgentBehavior target changes, sets NavMeshAgent.destination.
      • Uses throttle (min move distance) and animates a walk boolean based on velocity.
    • AgentBehavior
      • Uses GOAP runner components to interact with GOAP system (current target events, goal setting).
    • LlamaBrain
      • Sets current goal on start (WanderGoal) and responds to player sensor enter/exit to set KillPlayer or Wander/Eat depending on hunger.
    • SpitBehavior
      • Receives animation event (BeginSpit) and raises a spawn event that actions subscribe to.
    • HungerBehavior & SalivaBehavior
      • Hold and update hunger/saliva values each frame (increment/decrement based on BioScience config). Saliva decays; eating restores it.
    • PlayerSensor MonoBehaviour
      • Trigger collider that fires player enter/exit events. Attach as child to enemy and set proper layer & isTrigger = true.
  7. Factory & Builder (GOPSetConfigFactory)

    • Implement a factory extending GOPSetFactoryBase with Create() to build a GOP set for the agent type.
    • Use a builder pattern:
      • builder.AddGoal(goalType, conditionWorldKey, comparison)
      • builder.AddAction(actionType).SetTarget(TargetKey).AddEffect(WorldKey, effectValue).SetBaseCost(...).SetInRange(...)
      • builder.AddSensor(sensorType).SetTarget(TargetKey)
    • Link goals, actions, sensors, effects, costs, and in‑range thresholds in the factory.
  8. Dependency injection for configs and non‑Mono classes

    • Create a DependencyInjector class extending GOPConfigInitializerBase and implement injector interface so the GOAP system can call Inject on IInjectable objects.
    • Make configs ScriptableObjects:
      • AttackConfig: sensor radius, melee radius, melee cost, range attack cost, range attack radius, spit prefab, layer mask, attackDelay.
      • WanderConfig: timer range, wander radius, weights, etc.
      • BioScience (or Biosigns): hunger rates, max hunger, acceptable hunger limit, food search radius, spit restoration/depletion rates.
    • Actions/sensors implement IInjectable.Inject(DependencyInjector) to receive configs.
  9. Unity editor setup & configuration

    • Create GOAP config GameObject with GOPSetConfigFactory and GOPRunnerBehavior; validate config.
    • Attach required behaviors to Llama prefab:
      • NavMeshAgent (set stopping distance)
      • Animator (parameters: walk, isEating, attack)
      • AgentBehavior, AgentMoveBehavior, LlamaBrain
      • GOPSetBinder to bind AgentBehavior to GOPset via GOPRunner
      • PlayerSensor child (trigger collider on enemy sensors layer)
    • Create ScriptableObject assets (attack config, wander config, biosigns) and reference them in DependencyInjector.
    • Configure layers, collisions, and physics matrix (player layer, grass layer, enemy sensor layer).
    • Animator: set parameters and transitions (walk, isEating, attack, idle states), and add animation event for spit spawn with exact function name.
  10. Pooling & animation events for projectile - Use Unity object pool to spawn spit prefab. - Spit prefab: Rigidbody + trigger collider + Spit script (auto‑destroy after lifetime, apply force, handle OnTrigger to hit player). - SpitBehavior listens for animation event BeginSpit and raises spawn event. SpitAction subscribes and calls pool.Get() to instantiate and position the spit.

  11. Testing and tuning - Use the GOP node viewer tool to inspect goals, current plan, and world state as seen by the GOAP system. - Tune costs, in‑range thresholds, stopping distance, and config values to achieve desired emergent behavior. - Typical tuning notes: - Wander in‑range too large causes continuous movement. - Action in‑range determines when re‑planning occurs. - Model scale affects trigger colliders and sensor radii if colliders are children and scaled.


Important implementation details, patterns & pitfalls


Tools & resources mentioned


Final lessons / takeaways


Speakers / sources featured

Category ?

Educational


Share this summary


Is the summary off?

If you think the summary is inaccurate, you can reprocess it with the latest model.

Video