Click-to-Move in an Isometric Three.js Game
Click the ground in Ash & Oath and your hero walks there. Click on the far side of a wall and the hero looks for a way around. Those two clicks start with the same bit of Three.js code. I cast a ray from the pointer onto a flat ground plane, then hand the point to the movement code to decide where the hero can actually go. Ash & Oath project page
Turn a canvas click into a ground position
The game uses an orthographic camera that follows the hero from a fixed angle. With an orthographic camera, rays from different pixels have parallel directions and different starting points. Raycaster.setFromCamera() works out both using the camera and the pointer position on screen. Three.js Raycaster documentation
A pointer event gives you clientX and clientY in viewport CSS pixels. Subtract the canvas rectangle’s left and top edges, then divide by its displayed width and height. The centre of the canvas becomes (0, 0) in normalized device coordinates. The top-left corner becomes (-1, 1) because browser Y increases down the page and Three.js expects Y to increase upward. Using the displayed rectangle keeps the click aligned when the renderer changes its internal resolution.
Here’s a simplified version of the game’s projection helper. Pass it a pointer event, the canvas and the camera used to draw the scene. The example assumes a borderless canvas and a flat floor at world y = 0.
import { Plane, Raycaster, Vector2, Vector3 } from 'three';
const ground = new Plane(new Vector3(0, 1, 0), 0);
const raycaster = new Raycaster();
const pointer = new Vector2();
const hit = new Vector3();
function groundPointFromPointer(event, canvas, camera) {
const rect = canvas.getBoundingClientRect();
if (!rect.width || !rect.height) return null;
pointer.set(
((event.clientX - rect.left) / rect.width) * 2 - 1,
1 - ((event.clientY - rect.top) / rect.height) * 2,
);
raycaster.setFromCamera(pointer, camera);
const groundHit = raycaster.ray.intersectPlane(ground, hit);
return groundHit ? { x: groundHit.x, z: groundHit.z } : null;
}
Keep the camera’s matrices current before picking. Ash & Oath updates the world matrix when the camera follows the hero and calls updateProjectionMatrix() when its zoom changes. The example returns Three.js’s ground axes as { x, z }. In the game’s movement code I store those same axes as { x, y }, with the renderer mapping the simulation’s y to Three.js’s z. Three.js OrthographicCamera documentation
Decide what the player clicked
In the field, a regular left click checks for an enemy first, then an interactable such as an NPC or chest. The game projects each candidate’s feet and head into canvas coordinates and measures how close the pointer is to the line between them. A taller target gets a wider picking radius, so a click on a large enemy’s shoulders can still select it. If there's nothing to pick, the game uses the ground point as the walk destination.
That leaves each action free to decide when it can happen. An NPC can respond as soon as the hero is in reach. An enemy needs to be in weapon range with a clear line for the attack. A ground click only needs a route.
Give the hero a route
I start by sampling the straight line from the hero to the destination against the same collision rules the game uses for movement. If the line is clear, the destination is the only waypoint. If a wall blocks it, the planner searches a grid for a way around. A diagonal step checks both adjacent cells so the route can’t cut across a solid corner. After the search, I skip intermediate waypoints wherever a longer straight segment is clear.
If the planner can’t reach the destination within its search budget, it can return a path to the closest point it explored. One test clicks inside a sealed obstacle and checks that the hero stops outside it. Another places a wall across the direct route and walks the resulting waypoints to check that the hero reaches the other side without crossing the wall.
Holding the mouse button adds one wrinkle. As the camera follows the hero, the same screen pixel can point at a different patch of ground. In the field I recast about every 120 milliseconds while the player holds a ground click, then replan when the destination has moved more than 30 world units or there’s no active route. Those thresholds are tuning values from Ash & Oath.
If you’re adding this to your own game, draw a marker at the ray hit before hooking up movement. Check that it stays under the pointer after zooming or resizing. Then place a wall between the character and the marker and check the route independently. The first check tells you whether your screen-to-world maths lines up. The second tells you whether the hero can get there. For the camera and rendering setup around this input code, see Building an Isometric Action RPG in the Browser with Three.js.