KEYBOARD INPUTS FOR
FIRST PERSON GAMES

By Shane Brumback · · Updated · Free Three.js tutorial · Level 1

To program keyboard input for a Three.js first person game, keep a keys object that keydown sets to true and keyup sets to false, then read it every frame to build a movement vector, rotate it by the player yaw and move the player by speed times delta time. This demo adds Shift to sprint, Space to jump, V to swap first and third person, and touch buttons for phones.

A beginner walkthrough of keydown and keyup, WASD and arrow key movement, gravity, simple collisions and a first person camera for a Three.js 3D browser game. The neon arena behind this page is the live demo running on autopilot.

✓ FREE Level 1 · Beginner WASD & arrows keydown / keyup Sprint & jump First person camera
See it live

LIVE DEMO

The neon arena behind this page is a live Three.js game scene on autopilot. Launch the full-screen demo to walk with WASD or the arrow keys, sprint with Shift, jump with Space, press V to swap camera views and drag to look around. On a phone, use the on-screen keys. Launch asks for a free Google sign-in first. Press Esc or Exit to return.

Free Google sign-in · WASD / arrows · Shift sprint · Space jump · V view · Drag to look · Esc to exit

Why Games Track Keys Instead of Reacting to Them

A keyboard event fires once when a key goes down and once when it comes up. Holding W does not send a steady stream you can rely on (key repeat is slow and depends on the computer). So games do not move the player inside the event. They just write down which keys are held, in a simple keys object, and the render loop reads that list 60 times a second.

That one idea fixes the classic beginner bugs: movement that stutters, two keys that will not work together, and a player who keeps walking after you switch tabs. We use e.code (like KeyW) instead of e.key, because code is the physical key position, so WASD still works on French AZERTY and other layouts.

From Keys to Movement

Each frame we turn the held keys into two numbers: forward (W minus S) and strafe (D minus A). Those make a direction arrow. We normalize it so walking diagonally is not faster, then rotate it by the way the player is facing (the yaw) so W always means “the way I am looking”. Multiply by speed and by the frame time, and the player moves the same distance per second on a 60 Hz laptop or a 144 Hz monitor.

Jumping is a vertical speed that gravity pulls down every frame. Collisions here are simple circles around each crate that push the player back out. For mouse look with a locked cursor, see Three.js Pointer Lock Controls, and for a full shooter start from the FPS game starter.

Step-by-Step Code Breakdown

Every excerpt below is copied exactly from the full code further down.

1. Set up the scene, camera, renderer and lights

In plain words: we make a dark blue world with fog, a wide first person camera, a renderer, and soft sky light plus a sun. Create a Scene with fog, a 70 degree PerspectiveCamera, a WebGLRenderer sized to the window, a HemisphereLight and a DirectionalLight.

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070a18);
scene.fog = new THREE.Fog(0x070a18, 20, 70);
const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight, 0.1, 300);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);

scene.add(new THREE.HemisphereLight(0xa8c4ff, 0x1a1030, 1.4));
const sun = new THREE.DirectionalLight(0xffffff, 1.6);
sun.position.set(10, 20, 8);
scene.add(sun);

2. Build a neon arena with colorful crates

In plain words: we lay a glowing grid floor and scatter bright crates to walk around, remembering how big each one is for collisions. Add a PlaneGeometry floor and a GridHelper, then 26 random BoxGeometry crates with emissive materials, storing each mesh with a collision radius.

const ARENA = 30;
const floor = new THREE.Mesh(new THREE.PlaneGeometry(ARENA * 2, ARENA * 2), new THREE.MeshStandardMaterial({ color: 0x0b0f22, roughness: 0.85 }));
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
const grid = new THREE.GridHelper(ARENA * 2, 30, 0xff3df0, 0x2a3a6a);
grid.position.y = 0.01;
scene.add(grid);

const COLORS = [0xff3df0, 0x2ad1d6, 0xffd400, 0x39ff14, 0xff7a00, 0x6a5cff];
const crates = [];
for (let i = 0; i < 26; i++) {
  const size = 1.2 + Math.random() * 1.8;
  const crate = new THREE.Mesh(
    new THREE.BoxGeometry(size, size, size),
    new THREE.MeshStandardMaterial({ color: COLORS[i % COLORS.length], emissive: COLORS[i % COLORS.length], emissiveIntensity: 0.25, roughness: 0.4 })
  );
  const angle = Math.random() * Math.PI * 2, dist = 6 + Math.random() * (ARENA - 9);
  crate.position.set(Math.cos(angle) * dist, size / 2, Math.sin(angle) * dist);
  crate.rotation.y = Math.random() * Math.PI;
  scene.add(crate);
  crates.push({ mesh: crate, radius: size * 0.75 });
}

3. Make the player and its state

In plain words: the player is a white capsule with a glowing visor, and a small state object remembers where it looks, how fast it falls and which camera view is on. Group a CapsuleGeometry body and a BoxGeometry visor into a player Group, and keep yaw, pitch, vertical velocity, onGround and view in a state object.

const player = new THREE.Group();
const body = new THREE.Mesh(new THREE.CapsuleGeometry(0.45, 1.0, 6, 16), new THREE.MeshStandardMaterial({ color: 0xe8ecff, roughness: 0.35 }));
body.position.y = 0.95;
const visor = new THREE.Mesh(new THREE.BoxGeometry(0.6, 0.22, 0.3), new THREE.MeshStandardMaterial({ color: 0x2ad1d6, emissive: 0x2ad1d6, emissiveIntensity: 1 }));
visor.position.set(0, 1.45, -0.32);
player.add(body, visor);
scene.add(player);
const state = { yaw: 0, pitch: 0, velocityY: 0, onGround: true, view: 'first' };

4. Track held keys with keydown and keyup

In plain words: pressing a key writes true next to its name, letting go writes false, and switching away from the tab lets go of everything. Store e.code in a keys object on keydown and keyup, call preventDefault for game keys so the page does not scroll, toggle the view on V, stop the autopilot on first input, and clear all keys on window blur.

const keys = {};
const GAME_KEYS = ['KeyW', 'KeyA', 'KeyS', 'KeyD', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Space', 'ShiftLeft', 'ShiftRight'];
let inputEnabled = true;
let autopilot = true;
function stopAutopilot() {
  if (!autopilot) return;
  autopilot = false;
  for (const k in keys) keys[k] = false;
}
addEventListener('keydown', (e) => {
  if (!inputEnabled) return;
  if (GAME_KEYS.includes(e.code)) e.preventDefault();
  stopAutopilot();
  keys[e.code] = true;
  if (e.code === 'KeyV' && !e.repeat) state.view = state.view === 'first' ? 'third' : 'first';
});
addEventListener('keyup', (e) => { keys[e.code] = false; });
addEventListener('blur', () => { for (const k in keys) keys[k] = false; });

5. Draw an on-screen key HUD that also works for touch

In plain words: we draw W, A, S, D, Shift, Jump and V buttons that light up when held, and on a phone you can press them with your thumb. Build a CSS grid of key caps, set keys on pointerdown and clear them on pointerup or pointerleave, and recolor each cap every frame from the keys object, including the arrow key aliases.

const hud = document.createElement('div');
hud.style.cssText = 'position:fixed;left:16px;bottom:16px;display:grid;grid-template-columns:repeat(3,54px);gap:6px;user-select:none;touch-action:none;z-index:5;';
const KEY_CAPS = [['', ''], ['W', 'KeyW'], ['', ''], ['A', 'KeyA'], ['S', 'KeyS'], ['D', 'KeyD'], ['Shift', 'ShiftLeft'], ['Jump', 'Space'], ['V', 'KeyV']];
const capEls = {};
for (const [text, code] of KEY_CAPS) {
  const cap = document.createElement('div');
  cap.textContent = text;
  if (!code) { hud.appendChild(cap); continue; }
  cap.style.cssText = 'height:48px;display:flex;align-items:center;justify-content:center;border-radius:10px;font:700 14px system-ui,sans-serif;color:#fff;background:rgba(20,24,48,0.75);border:1px solid rgba(255,255,255,0.25);';
  cap.addEventListener('pointerdown', (e) => {
    e.preventDefault();
    stopAutopilot();
    if (code === 'KeyV') state.view = state.view === 'first' ? 'third' : 'first';
    else keys[code] = true;
  });
  const release = () => { keys[code] = false; };
  cap.addEventListener('pointerup', release);
  cap.addEventListener('pointerleave', release);
  capEls[code] = cap;
  hud.appendChild(cap);
}
document.body.appendChild(hud);
function updateHud() {
  for (const code in capEls) {
    const on = keys[code] || (code === 'KeyW' && keys.ArrowUp) || (code === 'KeyS' && keys.ArrowDown) ||
      (code === 'KeyA' && keys.ArrowLeft) || (code === 'KeyD' && keys.ArrowRight) || (code === 'ShiftLeft' && keys.ShiftRight);
    capEls[code].style.background = on ? '#2ad1d6' : 'rgba(20,24,48,0.75)';
    capEls[code].style.color = on ? '#000' : '#fff';
  }
}

6. Drag to look around

In plain words: dragging the mouse or a finger across the game turns your head left, right, up and down. On pointerdown on the canvas start a drag, then convert pointer movement into yaw and clamped pitch until pointerup.

let dragging = false, lastX = 0, lastY = 0;
renderer.domElement.addEventListener('pointerdown', (e) => { dragging = true; lastX = e.clientX; lastY = e.clientY; });
addEventListener('pointerup', () => { dragging = false; });
addEventListener('pointermove', (e) => {
  if (!dragging || !inputEnabled) return;
  stopAutopilot();
  state.yaw -= (e.clientX - lastX) * 0.005;
  state.pitch = THREE.MathUtils.clamp(state.pitch - (e.clientY - lastY) * 0.004, -1.2, 1.2);
  lastX = e.clientX;
  lastY = e.clientY;
});

7. Add an autopilot that presses keys for you

In plain words: until you touch anything, a tiny robot holds W, taps A and D, sprints now and then and jumps every few seconds, so the scene is never still. Write virtual key presses into the same keys object from sine waves of time and slowly turn the yaw.

function runAutopilot(t) {
  keys.KeyW = true;
  keys.ShiftLeft = Math.sin(t * 0.5) > 0.4;
  keys.KeyA = Math.sin(t * 0.9) > 0.6;
  keys.KeyD = Math.sin(t * 0.9) < -0.6;
  keys.Space = (t % 3.2) < 0.1;
  state.yaw += Math.sin(t * 0.3) * 0.006 + 0.004;
}

8. Move, sprint, jump and collide

In plain words: every frame we turn held keys into a direction, face it the way the player looks, move at walk or sprint speed, apply gravity and push the player out of crates. Build a move vector from forward and strafe, normalize it, rotate by yaw with applyAxisAngle, scale by speed and dt, integrate jump velocity and gravity, resolve circle collisions with crates and clamp to the arena.

const WALK = 5, SPRINT = 9, JUMP = 7, GRAVITY = 20;
const move = new THREE.Vector3();
function updatePlayer(dt) {
  const forward = (keys.KeyW || keys.ArrowUp ? 1 : 0) - (keys.KeyS || keys.ArrowDown ? 1 : 0);
  const strafe = (keys.KeyD || keys.ArrowRight ? 1 : 0) - (keys.KeyA || keys.ArrowLeft ? 1 : 0);
  move.set(strafe, 0, -forward);
  if (move.lengthSq() > 0) move.normalize();
  move.applyAxisAngle(THREE.Object3D.DEFAULT_UP, state.yaw);
  const speed = keys.ShiftLeft || keys.ShiftRight ? SPRINT : WALK;
  player.position.addScaledVector(move, speed * dt);

  if (keys.Space && state.onGround) { state.velocityY = JUMP; state.onGround = false; }
  state.velocityY -= GRAVITY * dt;
  player.position.y += state.velocityY * dt;
  if (player.position.y <= 0) { player.position.y = 0; state.velocityY = 0; state.onGround = true; }

  for (const c of crates) {
    const dx = player.position.x - c.mesh.position.x, dz = player.position.z - c.mesh.position.z;
    const d = Math.hypot(dx, dz), min = c.radius + 0.45;
    if (d < min && d > 0.0001) { player.position.x += (dx / d) * (min - d); player.position.z += (dz / d) * (min - d); }
  }
  const limit = ARENA - 1;
  player.position.x = THREE.MathUtils.clamp(player.position.x, -limit, limit);
  player.position.z = THREE.MathUtils.clamp(player.position.z, -limit, limit);
  player.rotation.y = state.yaw;
}

9. Update the first or third person camera and render

In plain words: in first person the camera sits in the player head; in third person it follows smoothly from behind. Then we draw the frame. Place the camera at eye height with a YXZ Euler for first person, or lerp it to an offset behind the player and lookAt for third person, then run the render loop and handle resize.

const chase = new THREE.Vector3();
function updateCamera(dt) {
  if (state.view === 'first') {
    camera.position.set(player.position.x, player.position.y + 1.6, player.position.z);
    camera.rotation.set(state.pitch, state.yaw, 0, 'YXZ');
    player.visible = false;
  } else {
    player.visible = true;
    chase.set(0, 2.6, 6).applyAxisAngle(THREE.Object3D.DEFAULT_UP, state.yaw).add(player.position);
    camera.position.lerp(chase, 1 - Math.exp(-6 * dt));
    camera.lookAt(player.position.x, player.position.y + 1.2, player.position.z);
  }
}

const clock = new THREE.Clock();
function render() {
  requestAnimationFrame(render);
  const dt = Math.min(clock.getDelta(), 0.05);
  if (autopilot) runAutopilot(clock.elapsedTime);
  updatePlayer(dt);
  updateCamera(dt);
  updateHud();
  renderer.render(scene, camera);
}
render();

addEventListener('resize', () => {
  camera.aspect = innerWidth / innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(innerWidth, innerHeight);
});

Because the autopilot writes into the same keys object as the real keyboard, the movement code never needs to know who is driving. That is the same trick games use for AI players and replays.

The Full Code

Paste this module into a page with the same import map as this one (Three.js 0.160 from unpkg). It is one file with no models or images to download.

import * as THREE from 'three';

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070a18);
scene.fog = new THREE.Fog(0x070a18, 20, 70);
const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight, 0.1, 300);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);

scene.add(new THREE.HemisphereLight(0xa8c4ff, 0x1a1030, 1.4));
const sun = new THREE.DirectionalLight(0xffffff, 1.6);
sun.position.set(10, 20, 8);
scene.add(sun);

const ARENA = 30;
const floor = new THREE.Mesh(new THREE.PlaneGeometry(ARENA * 2, ARENA * 2), new THREE.MeshStandardMaterial({ color: 0x0b0f22, roughness: 0.85 }));
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
const grid = new THREE.GridHelper(ARENA * 2, 30, 0xff3df0, 0x2a3a6a);
grid.position.y = 0.01;
scene.add(grid);

const COLORS = [0xff3df0, 0x2ad1d6, 0xffd400, 0x39ff14, 0xff7a00, 0x6a5cff];
const crates = [];
for (let i = 0; i < 26; i++) {
  const size = 1.2 + Math.random() * 1.8;
  const crate = new THREE.Mesh(
    new THREE.BoxGeometry(size, size, size),
    new THREE.MeshStandardMaterial({ color: COLORS[i % COLORS.length], emissive: COLORS[i % COLORS.length], emissiveIntensity: 0.25, roughness: 0.4 })
  );
  const angle = Math.random() * Math.PI * 2, dist = 6 + Math.random() * (ARENA - 9);
  crate.position.set(Math.cos(angle) * dist, size / 2, Math.sin(angle) * dist);
  crate.rotation.y = Math.random() * Math.PI;
  scene.add(crate);
  crates.push({ mesh: crate, radius: size * 0.75 });
}

const player = new THREE.Group();
const body = new THREE.Mesh(new THREE.CapsuleGeometry(0.45, 1.0, 6, 16), new THREE.MeshStandardMaterial({ color: 0xe8ecff, roughness: 0.35 }));
body.position.y = 0.95;
const visor = new THREE.Mesh(new THREE.BoxGeometry(0.6, 0.22, 0.3), new THREE.MeshStandardMaterial({ color: 0x2ad1d6, emissive: 0x2ad1d6, emissiveIntensity: 1 }));
visor.position.set(0, 1.45, -0.32);
player.add(body, visor);
scene.add(player);
const state = { yaw: 0, pitch: 0, velocityY: 0, onGround: true, view: 'first' };

const keys = {};
const GAME_KEYS = ['KeyW', 'KeyA', 'KeyS', 'KeyD', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Space', 'ShiftLeft', 'ShiftRight'];
let inputEnabled = true;
let autopilot = true;
function stopAutopilot() {
  if (!autopilot) return;
  autopilot = false;
  for (const k in keys) keys[k] = false;
}
addEventListener('keydown', (e) => {
  if (!inputEnabled) return;
  if (GAME_KEYS.includes(e.code)) e.preventDefault();
  stopAutopilot();
  keys[e.code] = true;
  if (e.code === 'KeyV' && !e.repeat) state.view = state.view === 'first' ? 'third' : 'first';
});
addEventListener('keyup', (e) => { keys[e.code] = false; });
addEventListener('blur', () => { for (const k in keys) keys[k] = false; });

const hud = document.createElement('div');
hud.style.cssText = 'position:fixed;left:16px;bottom:16px;display:grid;grid-template-columns:repeat(3,54px);gap:6px;user-select:none;touch-action:none;z-index:5;';
const KEY_CAPS = [['', ''], ['W', 'KeyW'], ['', ''], ['A', 'KeyA'], ['S', 'KeyS'], ['D', 'KeyD'], ['Shift', 'ShiftLeft'], ['Jump', 'Space'], ['V', 'KeyV']];
const capEls = {};
for (const [text, code] of KEY_CAPS) {
  const cap = document.createElement('div');
  cap.textContent = text;
  if (!code) { hud.appendChild(cap); continue; }
  cap.style.cssText = 'height:48px;display:flex;align-items:center;justify-content:center;border-radius:10px;font:700 14px system-ui,sans-serif;color:#fff;background:rgba(20,24,48,0.75);border:1px solid rgba(255,255,255,0.25);';
  cap.addEventListener('pointerdown', (e) => {
    e.preventDefault();
    stopAutopilot();
    if (code === 'KeyV') state.view = state.view === 'first' ? 'third' : 'first';
    else keys[code] = true;
  });
  const release = () => { keys[code] = false; };
  cap.addEventListener('pointerup', release);
  cap.addEventListener('pointerleave', release);
  capEls[code] = cap;
  hud.appendChild(cap);
}
document.body.appendChild(hud);
function updateHud() {
  for (const code in capEls) {
    const on = keys[code] || (code === 'KeyW' && keys.ArrowUp) || (code === 'KeyS' && keys.ArrowDown) ||
      (code === 'KeyA' && keys.ArrowLeft) || (code === 'KeyD' && keys.ArrowRight) || (code === 'ShiftLeft' && keys.ShiftRight);
    capEls[code].style.background = on ? '#2ad1d6' : 'rgba(20,24,48,0.75)';
    capEls[code].style.color = on ? '#000' : '#fff';
  }
}

let dragging = false, lastX = 0, lastY = 0;
renderer.domElement.addEventListener('pointerdown', (e) => { dragging = true; lastX = e.clientX; lastY = e.clientY; });
addEventListener('pointerup', () => { dragging = false; });
addEventListener('pointermove', (e) => {
  if (!dragging || !inputEnabled) return;
  stopAutopilot();
  state.yaw -= (e.clientX - lastX) * 0.005;
  state.pitch = THREE.MathUtils.clamp(state.pitch - (e.clientY - lastY) * 0.004, -1.2, 1.2);
  lastX = e.clientX;
  lastY = e.clientY;
});

function runAutopilot(t) {
  keys.KeyW = true;
  keys.ShiftLeft = Math.sin(t * 0.5) > 0.4;
  keys.KeyA = Math.sin(t * 0.9) > 0.6;
  keys.KeyD = Math.sin(t * 0.9) < -0.6;
  keys.Space = (t % 3.2) < 0.1;
  state.yaw += Math.sin(t * 0.3) * 0.006 + 0.004;
}

const WALK = 5, SPRINT = 9, JUMP = 7, GRAVITY = 20;
const move = new THREE.Vector3();
function updatePlayer(dt) {
  const forward = (keys.KeyW || keys.ArrowUp ? 1 : 0) - (keys.KeyS || keys.ArrowDown ? 1 : 0);
  const strafe = (keys.KeyD || keys.ArrowRight ? 1 : 0) - (keys.KeyA || keys.ArrowLeft ? 1 : 0);
  move.set(strafe, 0, -forward);
  if (move.lengthSq() > 0) move.normalize();
  move.applyAxisAngle(THREE.Object3D.DEFAULT_UP, state.yaw);
  const speed = keys.ShiftLeft || keys.ShiftRight ? SPRINT : WALK;
  player.position.addScaledVector(move, speed * dt);

  if (keys.Space && state.onGround) { state.velocityY = JUMP; state.onGround = false; }
  state.velocityY -= GRAVITY * dt;
  player.position.y += state.velocityY * dt;
  if (player.position.y <= 0) { player.position.y = 0; state.velocityY = 0; state.onGround = true; }

  for (const c of crates) {
    const dx = player.position.x - c.mesh.position.x, dz = player.position.z - c.mesh.position.z;
    const d = Math.hypot(dx, dz), min = c.radius + 0.45;
    if (d < min && d > 0.0001) { player.position.x += (dx / d) * (min - d); player.position.z += (dz / d) * (min - d); }
  }
  const limit = ARENA - 1;
  player.position.x = THREE.MathUtils.clamp(player.position.x, -limit, limit);
  player.position.z = THREE.MathUtils.clamp(player.position.z, -limit, limit);
  player.rotation.y = state.yaw;
}

const chase = new THREE.Vector3();
function updateCamera(dt) {
  if (state.view === 'first') {
    camera.position.set(player.position.x, player.position.y + 1.6, player.position.z);
    camera.rotation.set(state.pitch, state.yaw, 0, 'YXZ');
    player.visible = false;
  } else {
    player.visible = true;
    chase.set(0, 2.6, 6).applyAxisAngle(THREE.Object3D.DEFAULT_UP, state.yaw).add(player.position);
    camera.position.lerp(chase, 1 - Math.exp(-6 * dt));
    camera.lookAt(player.position.x, player.position.y + 1.2, player.position.z);
  }
}

const clock = new THREE.Clock();
function render() {
  requestAnimationFrame(render);
  const dt = Math.min(clock.getDelta(), 0.05);
  if (autopilot) runAutopilot(clock.elapsedTime);
  updatePlayer(dt);
  updateCamera(dt);
  updateHud();
  renderer.render(scene, camera);
}
render();

addEventListener('resize', () => {
  camera.aspect = innerWidth / innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(innerWidth, innerHeight);
});

Frequently Asked Questions

How do you handle keyboard input in Three.js?

Three.js does not have its own keyboard system; you use normal browser events. Listen for keydown and keyup on window, store the held state in an object such as keys[e.code] = true, and read that object inside your requestAnimationFrame loop to move the player.

How do I make WASD movement in a Three.js game?

Each frame compute forward = W minus S and strafe = D minus A, put them in a Vector3, normalize it, rotate it by the player yaw with applyAxisAngle, and add it to the player position times speed times delta time.

Why use e.code instead of e.key?

e.code names the physical key, like KeyW, no matter the keyboard layout or Shift state. e.key gives the typed character, which changes on AZERTY keyboards and when Caps Lock is on, so WASD controls can break.

How do I stop the arrow keys and Space from scrolling the page?

Call e.preventDefault() in your keydown handler for the game keys while the game is active. In this tutorial that only happens when the demo is running, so the page scrolls normally otherwise.

Why does my player keep walking after I switch tabs?

If the tab loses focus while a key is held, the keyup event never arrives. Clear every key on the window blur event so nothing stays stuck down.

How do keyboard games work on phones?

Phones have no keyboard, so add on-screen buttons that set the same keys entries on pointerdown and clear them on pointerup. The movement code stays exactly the same.

Why do I need to sign in to launch the live demo?

The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site.

Play It and Keep Learning

WASD movement, sprint and jump like this drive Super Soldier Battle, a free Three.js browser FPS and Zombie Attack. Browse every Three.js tutorial, read the articles, or search the site.

Need Three.js or WebGL work for your site or game? I take freelance and contract work: contact me.

Play Game