EngineUnity 2022.3 LTS or newer
LanguageC#
NeedsRigidbody2D + Collider2D on the enemy
Time to run~3 minutes
LevelIntermediate
OutputOne file (~110 lines)
DependenciesNone. No navmesh.

The prompt

Open a fresh chat at claude.ai/new, paste, and send.

$ Build me a Unity 2022.3+ 2D patrolling enemy as a single C# MonoBehaviour.

// shape
- One file, EnemyPatrol.cs, on an enemy GameObject with Rigidbody2D and a Collider2D.
- A small enum State { Patrol, Chase }.
- No navmesh, no packages.

// patrol
- Move horizontally at a [SerializeField] patrolSpeed (default 2).
- Ledge check: Physics2D.Raycast down from a point slightly ahead of the feet, short distance, against a [SerializeField] LayerMask groundLayer. If it does NOT hit, turn around.
- Wall check: Physics2D.Raycast forward at body height against groundLayer. If it hits, turn around.
- Flip the sprite/facing when turning.

// optional chase
- [SerializeField] bool canChase (default true).
- Detect the player with an OverlapCircle (or forward ray) against a [SerializeField] LayerMask playerLayer within a [SerializeField] detectRange.
- In Chase, move toward the player at chaseSpeed but STILL respect the ledge check so it does not chase off a cliff.
- Return to Patrol if the player leaves detectRange.

// constraints
- Physics in FixedUpdate, detection in Update.
- Expose the raycast origin offsets and distances as SerializeField so they can be tuned.
- No magic numbers.

// return format
- One file. No prose before or after the code.
- Brief comments only on the ledge raycast and the state switch.
Open in Claude ▸ Run · 3 min

What this gets you

An enemy that behaves like it belongs in the level: it paces its platform, turns at the edge, bumps off walls, and only leaves its patrol to come after you if you get close, without lemming-ing off the nearest cliff the moment it sees you.

It is a single script with the detection ranges and speeds exposed, so a slow dumb patroller and a fast alert hunter are the same code with different inspector values.

Why it matters

Ledge detection is the whole trick

The number one beginner enemy bug is the guard that walks straight off the platform. A short downward raycast ahead of the feet, checked every frame, is the entire fix. Without it you are faking patrol with hard-coded turn points that break the moment you move the platform.

Chase must respect the ledge too

It is easy to make Chase override movement entirely and send the enemy sailing off the edge after the player. Keeping the ledge check active in Chase is what makes the AI read as careful rather than suicidal.

Two rays beat a navmesh

For 2D platformer patrol, raycasts are simpler, faster and easier to tune than any pathfinding system. Reaching for navmesh here is the classic over-engineering trap.

What good output looks like

How to wire it up

  1. Save the output as EnemyPatrol.cs.
  2. Create the enemy GameObject with a Rigidbody2D (Gravity Scale > 0, Freeze Rotation Z) and a Collider2D.
  3. Attach the script. Set groundLayer to your platform layer and playerLayer to the player's layer.
  4. Put the player on that player layer and give it a collider.
  5. Press Play. The enemy should pace the platform, turn at the edge, and chase when you get close.

Common gotchas

The enemy walks off the platform anyway

The ledge ray starts from the center, or its distance is too short. Move the origin ahead of the feet in the facing direction and lengthen the ray a little past the collider bottom.

It only turns on one side

The ray offset is not flipping with facing. When the enemy turns, negate the horizontal offset so it checks the direction it is actually walking.

It chases straight off a cliff

The Chase branch skips the ledge check. Add the same ledge guard there so it stops or turns at the edge even while chasing.

Where to go next

  1. Attack. Add an Attack state when in melee range, with a windup before the hit.
  2. Waypoints. Swap edge-turning for a list of patrol points for enemies on open ground.
  3. Pool them. Spawn enemies from the object pool so waves cost nothing.

Use it however you want

The prompt is CC0, paste it, modify it, ship a game with it. The output Claude gives you is yours. A link back to pixeldex.dev in your project's README helps the next solo dev find this stuff.