Skip to content
Fungi

Drive an actor with the movement keys

Let the player walk one actor with WASD or the arrow keys, over real terrain.

Most games move actors by route: the player clicks, the actor walks there. Some games want one actor on the keys instead, like a survivor or a ship. Name that actor in the page settings:

TS
export const page = {
// ...
directControl: { entity: "acorn.hero" },
};

The player’s movement keys then drive it directly. The first key press takes over, and a route in flight stops. The world moves the actor in fixed steps, so every player sees the same result and a saved game resumes mid-step.

An actor with Traversal is a terrain walker. When it is driven directly:

  • it climbs or drops at most maxStepCells voxels in one step, and a taller ledge stops it;
  • it never steps where there is no floor;
  • a finished wall, a closed door or a structure filling a cell stops it, and it slides along the wall when the key points partly past it;
  • an open door lets it through.

When the keys let go, your rules can route the actor again as usual. The route starts from wherever the actor stands, even between cells. So a rule can also shove it a little way (a knockback) and send it on its way afterwards.

A game driven with WASD often wants twin-stick aiming. Name the command that attacks in page.aim:

TS
export const page = {
// ...
directControl: { entity: "acorn.hero" },
aim: { command: "attack", pointer: true, arrows: true },
};

With pointer, a plain click on the world sends the command with { at: { x, z } }. The point is the clicked actor, or the ground at the hero’s height. With arrows, holding an arrow key sends { toward: { x, z } } (up is north, the way W walks) at once, then every repeatSeconds (a quarter second by default) while the key is held. The arrows then no longer move the hero. Your command decides what each attack does and how often one lands. Games without aim keep plain clicks for selection and the arrows for moving.