|
| 1 | +--- |
| 2 | +tags: movement |
| 3 | +title: First-person 3D camera and movement |
| 4 | +brief: Control a first-person camera using WASD and mouse to look with cursor lock. |
| 5 | +author: Defold Foundation |
| 6 | +scripts: character_controller.script |
| 7 | +thumbnail: collection.png |
| 8 | +--- |
| 9 | + |
| 10 | +This example shows how to build a simple first-person controller for a 3D scene. You can look around with the mouse and move on the XZ plane using the keyboard (WSAD). |
| 11 | + |
| 12 | +## What you'll learn? |
| 13 | +- How to implement a FPP camera with mouse to look around. |
| 14 | +- How to lock/unlock the mouse cursor for immersive camera control. |
| 15 | +- How to move on a simple XZ plane logic with keyboard input. |
| 16 | + |
| 17 | +## Controls |
| 18 | +| Input | Action | |
| 19 | +|------------------------|------------------------------------------------------| |
| 20 | +| Left mouse click | Lock the cursor and enable mouse look | |
| 21 | +| Mouse movement | Rotate camera | |
| 22 | +| `Esc` | Unlock the cursor | |
| 23 | +| `W`/`S`/`A`/`D` | Move forward/backward/left/right on the ground plane | |
| 24 | + |
| 25 | +## How it works? |
| 26 | +When the cursor is locked, the script reads mouse movement deltas and rotates the camera accordingly. Movement is normalized to keep a consistent speed in all directions and is clamped within a square area so you cannot wander off the demo scene. |
| 27 | + |
| 28 | +Example collection consists of 3 main parts: |
| 29 | + |
| 30 | +- `character` - The player character game object includes: |
| 31 | + - A *script* `character_controller.script` component that implements mouse look, cursor lock/unlock, and WASD movement. |
| 32 | + - A *camera* component configured with perspective projection. |
| 33 | + |
| 34 | +- `scene` - the models used to create a basic 3D environment: |
| 35 | + - Ground plane scaled to form a walkable area with prototype texture |
| 36 | + - Walls built from simple cube models with prototype textures |
| 37 | + - Some decorative trees |
| 38 | + |
| 39 | +- `gui` - An on-screen GUI with short instructions. |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +### Assets |
| 44 | +Tree models with textures by Kay Louseberg: https://kaylousberg.itch.io/kaykit-forest |
| 45 | +Prototype textures for Defold by Visionaire: https://github.com/Thevisionaire1/3Deforms |
| 46 | + |
| 47 | +## Script |
| 48 | + |
| 49 | +Tuning parameters are defined at the top of `character_controller.script`: |
| 50 | +- `look_sensitivity` (degrees per pixel) controls how fast the camera rotates |
| 51 | +- `move_speed` (world units per second) controls walking speed |
| 52 | +- `move_limit` (half-size in world units) clamps movement within bounds |
| 53 | + |
0 commit comments