Platformer
Stomp an enemy and watch its graph change mode. The first sample: one tree that chases, searches, and patrols; one defeat state machine; a list and a struct in the blackboard; everything else left in C#.
On this page
Start here. A 2D level, coins on the platforms, and a door at the end. Four creatures walk it. Enemy Platform and Enemy Platform 2 each take their patrol and their defeat from the exact same two graphs, keeping their own place in that shared route; Enemy Ground does the same job from ordinary C# so you can see there is no magic in any of them; Enemy Params is a fourth walker whose only job is to show a graph reading its waypoints from scene Transforms. The first three can be stomped.

Play it in one minute
Under a minute
- Scene
Assets/Platformer/Scenes/Platformer.unity- Select
- Enemy Platform > Behavior Agent (EnemyPatrol) in the Hierarchy
- Controls
- Keyboard: A and D or the left and right arrows to move, Space to jump. Gamepad: left stick to move, south button to jump. There is no attack button in this sample; the slam exercise is started from the Inspector.
- Goal
- Collect five of the level's six coins so the goal door opens, then walk into the door. Land on an enemy from above to stomp it.
The first thirty seconds
Section titled “The first thirty seconds”Enemy Platform and Enemy Platform 2 each walk the same three-point route across the raised platform and stop for a blink at each end, at whatever stop and pause each one has reached on its own. Enemy Ground and Enemy Params pace the ground below them. Open EnemyPatrol in the editor and select Enemy Platform in the Hierarchy before you press Play. The object carries two Behavior Agent components, one per graph, and as the scene starts the editor attaches to the one running the graph it is showing, so the graph on screen belongs to the enemy you are watching. With no graph open, selecting the object attaches to nothing, because two agents on one object is ambiguous on purpose.
Walk within three world units of Enemy Platform or Enemy Platform 2 and it breaks off its route to chase you at a faster speed; back out of range and it travels to the spot it last saw you, searches there briefly, and returns to patrolling from wherever that leaves it. Neither one ever steps off its platform: drop to the ground below and it chases you only as far as the edge and waits there while you stay in range, and when you leave, its search happens at that edge. Enemy Ground never does this: its patrol is hand-written, with no chase or search of its own.
Touch Enemy Platform, Enemy Platform 2, or Enemy Ground from the side and the player reappears at the level start with a three-quarter-second grace window. Fall below the level and the same thing happens. Coins you already picked up stay picked up and the score stays where it was, so a death costs you position and nothing else. Enemy Params is solid but harmless in this release: it will shove you along the ground and cannot be stomped, because it carries no Enemy component, only the waypoint patrol it exists to show. Reach the door with five coins in hand and the level completes; press Return or the keypad Enter to reload the scene.
Two scene files sit in this project. Assets/Scenes/SampleScene.unity is the leftover Unity template scene: ignore this one. Everything in this chapter is in Assets/Platformer/Scenes/Platformer.unity.
Paths here follow the standalone project layout. Importing the packaged sample puts the same folders under Assets/BitQuirky/BehaviorTreesAndStateMachines/Samples/.
What this game teaches
Section titled “What this game teaches”- A first behavior tree built from three branches under one root Selector: chase, search, and a patrol small enough to read in one look on its own, a Forever repeat over a Sequence of two checks and one call.
- A first state machine with two modes, one transition, and a defeated mode whose three actions run Together and Once.
- Member bindings against the game’s real API: a property read for the gate, a method call for the work, and a comparison that turns a returned
boolinto a node result. - Value shapes past the primitives. The patrol route is a
List<Vector2>and the patrol profile is your ownstruct, both declared in the blackboard and handed to C# as typed arguments. - Two enemies, one graph. Enemy Platform and Enemy Platform 2 both run
EnemyPatrol.bqbehavior, and each keeps its own next stop and pause progress declared right there instead of in a separate record neither of them can see. - A branch that both takes over and gives itself back from the same fact, via the Both abort scope:
EnemyPatrol.bqbehavior’s chase branch starts the moment the player is seen and ends the moment sight breaks, oneCondition, one check. - A self-contained search routine run as a Hosted State Machine inside the tree:
SearchAndReturn.bqbehaviortravels to the player’s last-known position, searches for its own authored duration, and reports back, filling the search branch’s own slot in place of a Sequence of individual nodes. - A
params Transform[]method fed two ways, as individual values or one collection, and the same slot on a decision that branches by route. - Live on one instance, plus pause and step on that instance while the rest of the scene keeps playing.
What the graphs deliberately leave in C#: movement, physics, collision classification, which contact counts as a stomp, waypoint progression, pause timing, scoring, coin state, the door rule, respawn, and input. The graph decides; the game acts. That boundary is the whole point of the sample, and it is the one you keep when you copy it.
Graph versus game code
Section titled “Graph versus game code”| The graph decides | Your game code does |
|---|---|
EnemyPatrol.bqbehavior on Enemy Platform and Enemy Platform 2, hosted by GraphEnemyPatrol on each |
Enemies.EvaluatePatrol owns the waypoint order, the pause clock, and the facing sign; each agent’s own next_stop and pause_progress blackboard values carry its progress from call to call. Rigidbody2DBehaviorMovementAdapter puts the returned velocity on the body. GraphEnemyPatrol.RecordLastKnownPosition, ConsumeSearchPending, and AuthorizePatrol own the chase-to-search-to-patrol handoff; the tree decides only when each activity gets the agent. |
SearchAndReturn.bqbehavior, hosted by EnemyPatrol.bqbehavior’s own Hosted State Machine node |
GraphEnemyPatrol.MoveTowardPosition is reused for the travel state, and AdvanceSearchTimer owns the search clock against the state machine’s own authored duration; neither state knows it is reached from a tree instead of called directly. |
EnemyStateMachine.bqbehavior on Enemy Platform and Enemy Platform 2, hosted by GraphEnemyModeCoordinator on each |
Enemies accepts the first valid stomp and rejects the rest. Scoring awards the 25 points. The coordinator stops the body, clears the harmful flag, and swaps the sprite when the graph asks. |
EnemyPatrolParams.bqbehavior on Enemy Params, hosted by the same GraphEnemyPatrol script |
The three markers under Enemy Params Waypoints are ordinary Transforms. The adapter writes them into the graph’s wp0, wp1, wp2 slots and, reversed, into the altRoute array; AdvanceWaypointPatrol(ref PatrolStop, ref float, params Transform[]) converts whichever form the graph hands it into positions and threads the graph’s own next_stop and pause_progress through the shared patrol decision, RouteContainsWaypointsInOrder(params Transform[]) answers the two route decisions, and CancelPatrol() is the halt: reroute needed branch the selector reaches only when neither route is in the patrol. |
PlayerSlam.bqbehavior on Player, hosted by GraphPlayerSlam, composed disabled |
Players.Slam(float power = 8) owns the slam rule, and only the light power opens the wind-up window that raises SlamWindUpCompleted. |
| Nothing at all | Enemy Ground runs EnemyPatrol.cs with EnemyModeCoordinatorReference: the same patrol and the same defeat, hand-written, kept as the comparison. |
LevelRules holds the tunables neither side authors in a graph: five coins to open the door, 10 points a coin, 25 for a stomp, and the fall line at y = -6.
These are not the game
Section titled “These are not the game”The Behavior folder holds more graph files than the scene runs. EnemyMode.bqbehavior and ArgumentBindingFixture.bqbehavior are authoring fixtures. Everything under Behavior/Tutorials/ is a documentation and capture fixture, which is where most of the editor clips on this site come from. The Documentation*.bqbehavior set exists so the guides can show one node at a time. Open them, read them, copy from them, and expect no gameplay: nothing in the scene points at any of them. The four graphs in the table above are the playable ones.
Follow one decision
Section titled “Follow one decision”This walkthrough follows the patrol branch: a Forever repeat over a Sequence of three steps, one level down from the root now that a Selector tries chase and then search ahead of it.

EnemyPatrol.bqbehavior at rest, all 14 nodes of it. This walkthrough follows the patrol branch on the right: the card tagged CONDITION · MEMBER tests your data; the card tagged ACTION · METHOD beside it calls your code, with all five of its arguments listed on the card, two of them, next_stop and pause_progress, in the enemy’s own progress.-
Open
Assets/Platformer/Behavior/EnemyPatrol.bqbehaviorand leave the toolbar on Static. The window is Window > Behavior & State > Behavior Tree Editor if it did not open itself. -
Open Blackboard · Whole graph in the right rail. Five declarations:
route, a List ofVector2holding(6.5, 3.8),(8, 3.8), and(9.5, 3.8);profile, onePatrolProfilestruct withPauseSeconds0.05,FaceDirection1, andMoveSpeed2;route_tag, the stringcurious;next_stop, aPatrolStopstruct starting atWaypointIndex0,Phase0,CompletedCycles0; andpause_progress, thefloat0. The first three are the enemy’s design; the last two are its progress, and the advance node below both reads and writes them. -
Select patrol authorized, the sequence’s own guard, new in this graph. It reads
patrol_suppressedfrom the blackboard and compares it withfalse: the chase and search branches set that flag while they are busy, so the patrol refuses to run out from under them. -
Select patrol is registered. Its target is the
enemiesreference slot, the exactEnemiesasset the scene uses, and it reads theRegisteredPatrolCountproperty and compares it with0. No registered patrol means no reason to walk. -
Select advance the multi-point patrol. Its target is self, meaning the
GraphEnemyPatrolcomponent on the agent’s own GameObject, and it callsAdvancePatrolwithroute,profile, androute_tagas ordinary arguments plusnext_stopandpause_progressasrefparameters: read on entry, written back with the advanced value on the way out. The method returns abool, and the node compares it withtrue, so a refused move becomes Failure instead of a silent success. -
Press Play with Enemy Platform selected and switch to the agent whose graph is
EnemyPatrol. The canvas now carries status colors. Chase and search both report Failure every tick nothing is detected, so the root Selector’s own Running comes from the patrol branch below them: the repeat above the patrol sequence holds Running while the legs below it come and go. -
Open Blackboard · Whole graph again, in Live this time, and scroll past the seven new host-authoritative fields chase and search read and write. The same five declared values are still there, read from the instance that is walking in front of you.

route and profile decode their composite values directly, and next_stop shows its struct fields. Static is still where you change any of them.On the other side of that call, AdvancePatrol hands the route, the profile, and the tag to Enemies.EvaluatePatrol along with the body position, the fixed delta, and this instance’s own next_stop and pause_progress by ref, gets one PatrolInstruction back with those two values advanced, and applies the instruction: horizontal velocity onto the Rigidbody2D, the facing sign onto the sprite, the idle emote token onto its observation. Enemies still decides which waypoint comes next and how long a pause lasts; what moved is where that decision’s progress lives between calls. It is no longer a patrol session keyed by this enemy’s id: it is next_stop and pause_progress on this exact graph instance, the same two rows you just watched in Live.

Paused, that whole chain holds still for you. The condition resolves in one step, the call holds Running across several, and the other enemy keeps pacing the whole time because the pause belongs to this agent alone.
Two enemies, one root graph
Section titled “Two enemies, one root graph”Select Enemy Platform 2 instead, right beside Enemy Platform in the Hierarchy. Its Behavior Agent (EnemyPatrol) points at the identical EnemyPatrol.bqbehavior asset, and its own next_stop and pause_progress sit wherever that enemy has walked to on its own clock.


PatrolEnemy physics layer and exclude it from each other, so two bodies sharing one lane never becomes the reason their graph progress would diverge.Change something and watch
Section titled “Change something and watch”
Use this in your own game
Section titled “Use this in your own game”Copy as-is: the shape. One tree for “how do I do this job”, one state machine for “should I still be doing this job”, one thin host component per graph. GraphEnemyPatrol and GraphEnemyModeCoordinator are the two scripts worth reading line by line before you write your own, and they are short on purpose: assert what they need, wire the generated provider and the reference slot, forward one call, apply one instruction.
Rebind: the member targets. Your patrol authority is not called Enemies and your method is not called AdvancePatrol, so point the condition at your own property and the method node at your own method, then set the argument sources to your own declarations. The member picker lists what your assemblies actually expose, so this is picking from a list, not typing names.
Needs new C#: anything the graph asks for that your game cannot yet do. A new capability such as climbing, a lift, or a special attack is a new method on your own component first and a node second. The same goes for a fact the graph wants to test: expose it as a property or a field and the condition can read it.

That is the loop you will live in: insert a node, pick a target, pick a member, bind the arguments. Nothing in this sample was authored any other way.
When it goes wrong
| Symptom | Check | Fix |
|---|---|---|
| Your edit changed nothing. | Which enemy are you watching? Enemy Ground runs the hand-written C# patrol and has no agent on it at all. | Watch Enemy Platform or Enemy Platform 2 for anything driven by EnemyPatrol or EnemyStateMachine. |
| The wrong graph opens when you select the enemy. | Enemy Platform carries two Behavior Agent components, one for the patrol tree and one for the state machine. Selecting the object attaches to the one running the graph the editor shows. | Open the graph you want before you select the enemy. Attachment is exact and never guesses a sibling. |
| You edited a graph and the scene ignored it. | Confirm the file you opened is one of the four playable graphs and not a Documentation, Tutorials, EnemyMode, or ArgumentBindingFixture file. | Open the graph the scene names in the table above. |
| A Live value looks wrong and the saved graph looks right. | Read who owns the row in Blackboard · Whole graph. Enemy Params declares its waypoints as none and the host writes real Transforms in at runtime. | Judge a running agent from its Live values. A saved none is not proof of a missing reference. |
| The patrol registration condition fails forever. | Inspect the enemies reference slot on the agent and confirm it holds the project Enemies asset the scene uses. | Assign that exact asset. Another asset of the same type has no registered patrols. |
| A member node reports its target as unresolved. | Look for the generated provider component on the same GameObject as the agent, and confirm it was generated for this graph. | Regenerate it from Tools > Platformer, and keep the provider and the graph paired. A provider baked for another graph binds nothing. |
| The slam never starts. | Read the Graph Player Slam checkbox on Player. | Enable the host. There is no input binding waiting to start that graph for you. |
| The enemy walks into a wall or stalls at a tile seam. | Inspect the collider layout, not the tree. | The level merges adjacent floor tiles into continuous collision surfaces. Keep that when you extend it; no amount of graph editing fixes a snagging collider. |
What next
Section titled “What next”- Dungeon Heist: the same ideas with priority and interruption, where a guard changes its mind mid-stride.
- Patrol and chase: this sample’s tree grown into the shape most enemies want.
- Watch it run: the Live view in full, on this scene.
- GameObject hosting: what a Behavior Agent does on your own prefabs.
- Adapters and services: the boundary
GraphEnemyPatrolis an example of.
Credits and reuse
Section titled “Credits and reuse”The art and audio are by Kenney, who gives away thousands of game assets for free:
- New Platformer Pack: every sprite and sound in the level, from the player and the enemies to the tiles and the door.
We took ours from Kenney Game Assets All-in-1, the paid bundle of everything Kenney has released; the links above go to the same packs on kenney.nl, where each one is a free download.
The pack is CC0, which asks nothing of you and allows commercial use; we always credit Kenney when we use their assets, and support them by buying the packs. We hope you will too! The pack sits under Assets/ThirdParty/Kenney/ with its own License.txt, and the project’s Assets/CREDITS.md repeats this credit. Keep both with the art if you reuse it. The asset’s own license covers the code and the graph files; the art license is a separate thing, so check both before you ship a derivative.