G—P / FMX 320 / Tutorials / Tutorial 02

Tutorial 02 · AI

A semi-smart enemy 45 min

A semi-smart enemy is three Unreal pieces working together: a NavMesh so it can walk, an AI Perception component so it can see and hear, and a State Tree that decides what to do with what it senses. All of it is Blueprint.

Start The project from Tutorial 00 with its NavMesh and red BP_EnemyTime 45 minutes · 30 to build, 15 to make it feel smartNeed StateTree and GameplayStateTree plugins enabledServes Any level with an opponent in it, and Tutorial 03 builds directly on this

Download the demo projectUETutorials-demo.zip, 71 MB. Unzip it and open the .uproject in Unreal 5.8 to follow along.

Download the answer keyUETutorials-answer-key.zip, 72 MB. Open Lvl_ThirdPerson and AI/ST_Enemy. Press F to punch the enemy.

By the end of this tutorial you can

Brief · 5 min Five states, seven events

Perception events move the enemy between five states Patrol Chase Attack Investigate Flee sees or hears player within 150 cm player steps away lost sight health under 25% nothing found after 5 s reached a safe spot and healed
The enemy's five states and the seven events that move it between them. The accent box is the only state where the player takes damage.

Every state other than Attack exists to get the enemy to Attack or to keep it alive. Adding a state means adding a box, which is also exactly how you add one in the State Tree editor.

PieceWhere it livesJob
Nav Mesh Bounds VolumeThe levelMarks walkable ground so Move To has a path
BP_EnemyController (AI Controller)Possesses BP_EnemyHolds perception and the State Tree component
AI Perception componentOn the controllerSight, Hearing and Damage senses fire events
State Tree assetContent Browser, schema StateTree AI ComponentStates, tasks and the transitions between them
Environment Query System (EQS)Optional query assetFinds cover or a safe point for Flee

Build · 30 min From a standing enemy to a hunting one

  1. Give the enemy a controller

    Open BP_Enemy. Set AI Controller Class to a new blueprint BP_EnemyController and Auto Possess AI to Placed in World or Spawned.

  2. Add senses

    In BP_EnemyController add an AIPerception component. Add a Sight sense with radius 1500, lose-sight radius 2000, peripheral angle 70, and tick Detect Neutrals, because Blueprint actors count as neutral by default. Add a Hearing sense with range 2500.

    BP_EnemyController with the AIPerception component selected: Senses Config holds a Sight and a Hearing sense, and On Target Perception Updated is bound in the Events list below.
  3. Store what it senses

    On the component's On Target Perception Updated event, branch on Successfully Sensed. If true, store the actor in a TargetActor variable. If false, clear it and store the stimulus location in LastKnownLocation.

    The Event Graph. Begin Play calls Start Logic on the StateTreeAI component. On Target Perception Updated breaks the stimulus and branches on Successfully Sensed into TargetActor and LastKnownLocation.
  4. Add the State Tree

    Add a StateTreeAI component to the controller and create a State Tree asset with the StateTree AI Component schema. Call Start Logic on Begin Play.

  5. Make the five states

    Each state gets one task. Patrol: Move To with Get Random Reachable Point In Radius 1000, then Delay 2 seconds. Chase: Move To on TargetActor, acceptance radius 150. Attack: play an attack montage, Apply Damage, Delay 1 second. Investigate: Move To LastKnownLocation, Delay 5 seconds. Flee: Move To the result of an EQS query, or to an actor tagged SafeSpot.

    The State Tree editor with the five states expanded under Root. Each state has one task; its transitions live in the Details panel under the task.
  6. Wire the transitions

    Add transitions as conditions on each state, reading the controller's variables through property binding. TargetActor valid goes to Chase. Distance to TargetActor below 150 goes to Attack. TargetActor cleared goes to Investigate. Health below 25 percent goes to Flee. Task completion in Investigate or Flee goes back to Patrol.

  7. Watch it think

    Press Play and hold the apostrophe key for the Gameplay Debugger. You will see the perception cones and the active state. If the enemy stands still, press P and check for green under its feet.

    The Gameplay Debugger in Play with category 6, Perception, switched on. The debug actor is BP_Enemy, the green sphere is its sight sense, and the text lists its controller and movement state.

Build · 10 min Making it feel smart

Behavior Trees do the same job

Behavior Trees with a Blackboard are the older tool and have far more YouTube tutorials. State Tree is Epic's current default, and the videos map one to one: a Blackboard key is a State Tree parameter, a Task is a Task, a Decorator is a transition condition. Follow either; the diagram above is the same in both.

Before you close this

Read next