05 — Platformer Enemies Beyond Back-and-Forth

The classic question: "Is there anything smarter than enemies that march until they hit a wall, then turn around?" Yes — a whole ladder of techniques, each one cheap, and you can stop at any rung. The kit's PlatformerEnemy2D climbs the first five; run the platformer demo and watch both patrollers before reading on.

The ladder

Rung 1 — Feelers instead of triggers. The truly dumb enemy turns around at invisible trigger boxes a designer placed. The first upgrade is self-sufficiency: two raycasts — one forward (wall ahead?) and one down-from-ahead (floor about to run out?). Now the enemy patrols any platform you drop it on, with zero level markup. Select a demo patroller to see the red (wall) and blue (ledge) feeler gizmos.

Rung 2 — Directional vision. Mathf.Sign(toPlayer.x) != facing — one line, and the enemy only sees what's in front of it, within a distance and a height band, with a wall-blocking linecast. Suddenly "sneak up behind it" is a mechanic, and the enemy stops feeling psychic. (This is the 2D flattening of the 3D vision cone from 03 - The 3D Enemy.)

Rung 3 — The alert telegraph. On spotting the player, the demo enemy stops dead for 0.6 s (tinting yellow — swap in a "!" sprite and a sound). The player gets a fair warning and a moment to react. Nearly every beloved platformer/stealth enemy does this; it's the cheapest fun-per-line in this kit.

Rung 4 — Edge-aware chasing. While chasing, the enemy runs toward the player's x — but its own ledge feeler veto means it stops at cliff edges and glares instead of lemming-ing off. Unreachable player (above you, across a gap) + the lose-sight timer = it gives up and goes back to work. Chasing that respects the terrain reads as genuinely intelligent, and it's the same two raycasts from rung 1, reused.

Rung 5 — States, not booleans. All of the above is organized as Patrol → Alert → Chase → Attack — the same FSM shape as the other two enemies. The platformer enemy is proof the pattern scales down as well as up.

The rungs above this kit (know they exist)

Rung 6 — Investigate: walk to where you last saw the player before giving up (exercise in 06 - Tuning and Extending).

Rung 7 — Jump-capable pursuit (real platformer pathfinding). To chase across gaps and up ledges, an enemy needs to know how platforms connect: a node graph — nodes on platform surfaces, edges for "walk", plus special edges for "jump from here to there" and "drop down here", each annotated with the required jump arc. Then ordinary A* (lesson 04!) runs on that graph, and the enemy executes the moves the winning edges describe. That's how it's done in practice — the graph is usually hand-authored or semi-generated. Two solid reads: Generalized Platformer AI Pathfinding (GameDev.net) and this two-part deep dive (Game Developer). The A* Pathfinding Project can also be coaxed into platformer graphs. This is honors-project territory — a semester capstone, not a week-two lab.

Rung 8 — Flying/ghost enemies (the honest shortcut): enemies that ignore gravity can just use lesson 04's grid A* verbatim. Many "smart-feeling" platformer enemies in shipped games are secretly flyers because it sidesteps the jump problem entirely. Legitimate design move.

Design truth worth saying out loud

Dumb-marching Goombas aren't a failure — they're a role. Platformer enemy design is mostly about placement and readability, and a level usually wants a mix: many rung-1 patrollers (rhythm obstacles), a few rung-4 chasers (pressure), and maybe one rung-7 hunter (setpiece). Smarter isn't automatically better; appropriately smart is.

Exercises

  1. Add a jump-gap reflex: when chasing and the ledge feeler fires, raycast ahead at the same height — if there's floor within 3 units, set an upward+forward velocity. One rule, and it clears small gaps (this is rung 7's tiniest cousin).
  2. Give the alert state a shared alarm: OnAlert static event that other enemies within 6 units subscribe to — one spots you, nearby ones join. Pack behavior in ~10 lines.
  3. Make a coward variant: on seeing the player, flee to the far platform edge and shoot from range. Same sensors, inverted decision — a great "states are just decisions" quiz.

Next: 06 - Tuning and Extending