| Engine | Unity 2022.3 LTS or newer |
|---|---|
| Language | C# |
| Needs | Rigidbody2D + Collider2D on the enemy |
| Time to run | ~3 minutes |
| Level | Intermediate |
| Output | One file (~110 lines) |
| Dependencies | None. 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.
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
- Ledge ray originates ahead of the feet. The downward ray should start from a point offset in the facing direction, not from the center. A center ray never sees the edge until the enemy is already falling.
- Turning flips both movement and the ray direction. When it turns, the forward wall ray and the ahead-of-feet offset must flip too, or it only ever checks one side.
- Chase keeps the ledge guard. In the Chase branch there should still be a ledge check that stops or turns the enemy at the edge.
- Detection is range-based, not collision-based. Look for OverlapCircle/distance against detectRange, not an OnTriggerEnter that only fires once.
How to wire it up
- Save the output as
EnemyPatrol.cs. - Create the enemy GameObject with a
Rigidbody2D(Gravity Scale > 0, Freeze Rotation Z) and aCollider2D. - Attach the script. Set
groundLayerto your platform layer andplayerLayerto the player's layer. - Put the player on that player layer and give it a collider.
- 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
- Attack. Add an Attack state when in melee range, with a windup before the hit.
- Waypoints. Swap edge-turning for a list of patrol points for enemies on open ground.
- 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.