# Pixeldex · Unity Top-Down Player Controller Starter

Drop-in 2D top-down player controller for Unity 2022.3+. WASD movement with normalized diagonals, sprite flip on horizontal input, animator hooks (`speedX`, `speedY`, `isMoving`), a 0.05s input buffer so dashes feel responsive, and an optional snap-to-pixel mode.

License: **CC0**. Use it however you want, no attribution required.
Source: https://pixeldex.dev/prompts/unity-top-down-2d-player-controller

---

## What's in this folder

| File | Where to put it | What it does |
|---|---|---|
| `TopDownController.cs` | `Assets/Scripts/` | The controller. Attach to your Player GameObject. |
| `PixeldexTopDownSetup.cs` | `Assets/Editor/` | Editor utility. `Pixeldex → Spawn Top-Down Player` (Cmd/Ctrl+Shift+T) creates a fully-wired Player in one click. |

---

## Fastest path (~30 seconds)

1. Create `Assets/Scripts/` and `Assets/Editor/` if they don't exist.
2. Drop `TopDownController.cs` into `Assets/Scripts/` and `PixeldexTopDownSetup.cs` into `Assets/Editor/`.
3. Wait a moment for Unity to compile.
4. Click `Pixeldex → Spawn Top-Down Player` in the menu bar.
5. Press Play. WASD or arrow keys to move. Try walking diagonally, speed stays consistent (normalized diagonals).

---

## Manual setup (~2 minutes)

1. Drop `TopDownController.cs` into `Assets/Scripts/`.
2. Create an empty Player GameObject in your scene.
3. Add `Rigidbody2D` (Body Type **Dynamic**, **Gravity Scale 0**, **Freeze Rotation Z**, Interpolation **Interpolate**).
4. Add `BoxCollider2D` sized to your sprite.
5. As a child, add a `SpriteRenderer` with your character sprite.
6. Optionally add an `Animator` component to the sprite child with float params `speedX`, `speedY` and a bool `isMoving`.
7. Attach `TopDownController` to the Player. Drag the SpriteRenderer and Animator into the inspector fields.
8. Tag the Player as `Player`.
9. Press Play.

---

## Tunable fields

| Field | Default | What it does |
|---|---|---|
| `speed` | 5 | Move speed in units/sec |
| `inputBufferTime` | 0.05 | How long an input press is remembered |
| `snapToPixel` | false | Round position to 1/16 unit each FixedUpdate (16 PPU default, change in code if your PPU differs) |
| `spriteRenderer` | auto | Sprite to flip on horizontal input |
| `animator` | auto | Animator to drive |

---

## Common gotchas

**Character jitters.** Set Rigidbody2D Interpolation to `Interpolate`. The Setup utility does this for you.

**Diagonals are faster.** Make sure you're using `inputVector.normalized` in the code, already done in this version.

**Animator transitions look stuck.** Set `isMoving` as a transition condition (not the speed floats), and configure your blend tree off `speedX` / `speedY`.

---

## Where to go next

- Pair with the **save / load** starter to persist position between sessions.
- Pair with the **Godot FSM** prompt to graduate to a state-machine pattern once you have 3+ states.
- For platformer projects, swap to the [coyote-time platformer starter](https://pixeldex.dev/prompts/unity-2d-platformer-coyote-time).

If you ship a game with this, a link to https://pixeldex.dev in your README helps the next solo dev find it. Not required.
