Skip to content

2. A player ​

Swap the fixed camera for someone who can walk.

ts
import { Player } from '@maomi/sge/player';

export const createGame = (): HTMLElement => {
  const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight, 0.1, 4000);
  const fps = new FPS();

  fps.scene.add(defaultLights());
  fps.scene.add(createSkybox('/assets/skybox/night'));
  fps.add(new Grid(100));

  const player = new Player({ camera });          
  player.position = new THREE.Vector3(0, 1, 0);   
  fps.add(player);                                

  return fps.start(player);                       
};

Click the canvas: the mouse is captured, WASD walks, Space jumps, Esc releases. On a phone (or with ?touch in the URL) the same code mounts a stick and a jump button instead.

What you got for free:

  • a capsule rigid body — the player collides, gets pushed, can't walk through things;
  • a character controller that hugs floors, refuses steep slopes, and jumps only from the ground;
  • the camera synced to eye height, its orientation owned by the mouse;
  • a respawn guard — walk off the grid and fall far enough, and you're back at the spawn point.

Try walking off the edge and falling: that teleport-back is the engine's guard rail for unfinished levels — which is exactly what we have until part 3.