Skip to content
Work in progress. These docs describe Spawnite at launch, and some parts are still being built.

Chase

Chase moves an entity toward the nearest of its targets: by default, a character a player controls now. A character its player let go of, an NPC and an empty kart are not chased. Its targets prop widens or narrows whom it chooses, as Which targets it chases shows. A view reads the local player’s character with useCharacter().

Its props are in ChaseProps.

Chase moves toward the nearest target, level with the ground, and stops within reach. It moves the entity; it does not turn it, so a model that should face the way it moves needs its own facing. The physics cuts the chaser’s capsule to the body fields, and a field changed later reaches it from the next step. Its controller prop sets the character controller’s settings it walks by, a partial CharacterControllerTrait, such as controller={{ stepHeight: 0.3 }}. A Chase marks its entity a character, which the characters’ movement moves with moveCharacter by the velocity the chase sets, in the physics phase of the same tick. Where a game’s component on the same Entity already holds the capsule, the controller’s settings or CharacterTrait, the whole Chase is refused, as Write your own behaviour explains.

A chaser moves along the world’s navmesh, the floor a walker can reach with the rocks, the trees and the crates cut out of it, as every character no player controls moves by default. Each step moves it from where it stood toward where the chase puts it, round the edge of anything the navmesh leaves out. It stands on the ground there, or on a rock or a fixed collider lower than its step, whose top the navmesh keeps as floor. It runs no walk: no sweep of its capsule and no floor probe, so a crowd of monsters costs the room a fraction of what the same number of players cost.

Its capsule stays in the physics world, where the players’ walks meet it as the collision layers below say. Unity’s NavMeshAgent and Unreal’s NavWalking mode move a character the same way; Godot’s navigation agent only plans, and the game moves the body. It plans no route of its own: it heads straight for the target, so a wall square across that line holds it. A game that wants a chaser to walk round one replaces the chase, as Change an engine system shows.

On any tick it needs physics, the character controller moves it instead, as a player’s character moves, and it takes the navmesh again once it stands still on fixed ground. How an NPC moves lists when. A chaser on the navmesh follows these rules:

  • It stops at a crate, a platform or any other body a fixed collider gives, as it stops at a rock, and slides along its side. A collider whose collisionMask leaves out characters.collisionLayers.npcs is one it passes through.
  • A dynamic body in its way, a push, a launch or a step off an edge hands it to the controller, which pushes the body or stops at it, drops it, and walks it until it stands on the navmesh again.
  • A kinematic collider, such as a drawbridge, is left out of the navmesh while it moves, and the controller moves a chaser that meets it. Once it has stood still for half a second it is baked in, as Unity’s “carve only stationary” does.
  • The navmesh was baked for a player’s character. A chaser wider or taller than that character, or whose controller steps lower or stands on less steep a slope, moves through the controller every tick.

The navmesh holds the ground, the map’s scatter, every fixed collider an Entity in the Landscape gives, and every kinematic one at rest. Every Landscape bakes it whole as it mounts. A room and the CLI’s headless play bake it before their first step. A page builds its tiles in its ground worker and swaps each in between its frames, so the scene plays meanwhile, and the navmesh lands with its last tile. A page whose worker cannot start, such as one whose content policy refuses a blob: script, builds a few tiles at a time between its frames instead. The page’s first bake starts as its loading screen begins to fade, once no scene change is under way, so the load that mounts it does not wait on its slices. From then on a bake keeps going through a later load, and new obstacles bake again at once. Until the navmesh lands a walk is refused with NoNavMesh, as it is while the navmesh’s wasm loads.

A fixed collider can join or leave, move, turn, or change its shape, size, kind or layers, and a kinematic one can come to rest or start moving. The step then rebakes the navmesh tiles it touches, two tiles of about 11 m a tick, as Unreal and Unity rebuild a dynamic navmesh by tile. A tile with a placed block rebuilds in about half a millisecond on the bench field. Until its tiles rebake, the changed collider’s box belongs to the character controller, so a chaser meets a fresh crate, ramp or hole through the physics.

A page bakes a crate where the room streamed it, to the centimetre, and the room bakes it at that same precision, so both find the same paths. A walker whose path crosses a rebaked tile searches it again. A collider change the engine did not hear, such as one written in place with no mark, rebakes its box with markNavMeshDirty(step, { min, max }). The navmesh reads the ground, the scatter and the Entities’ colliders alone for where a walker stands, so geometry a game collides with its own code never reaches it. A navmesh modifier changes what a route costs and never where a walker stands, so a chaser, which plans no route, heads through one.

movementMode: NpcMovementMode.Walking on its Entity, or on its prefab, has the controller move it every tick. It then climbs and slides by its controller’s stepHeight and maxSlopeRadians, falls, and meets a moving collider as a player does, at the cost of a player’s walk each step it moves:

packages/engine/test/outside/Climber.tsx
import {
Chase,
Entity,
NpcMovementMode,
Views,
type EntityProps,
} from "@spawnite/engine";
/** A chaser the character controller moves every tick: it climbs, falls
* and meets a moving collider as a player does. */
export function Climber({ position }: Pick<EntityProps, "position">) {
return (
<Entity position={position} movementMode={NpcMovementMode.Walking}>
<Chase speed={4} />
<Views>
<mesh position={[0, 0.9, 0]}>
<capsuleGeometry args={[0.5, 0.8]} />
<meshStandardMaterial color="white" />
</mesh>
</Views>
</Entity>
);
}

A chaser also moves through the controller where the world has no navmesh, and where no part of the navmesh lies under its feet, such as on top of a rock taller than its step.

A chaser walks through the players by default: its collisionMask, chaserCollisionMask, leaves out characters.collisionLayers.players, so a player walks through a chaser and a chaser through a player, in the room and on every client alike. A crowd of monsters never pins a player where the player’s character stands. A game that wants its monsters to block the players gives them a mask that leaves out the other characters alone, and a chaser on the navmesh then keeps out of the players as well:

import { Chase, Entity, Views, type EntityProps } from "@spawnite/engine";
import { characters } from "@spawnite/engine/characters";
const blockingChaserMask = { except: [characters.collisionLayers.npcs] };
export function Guard({ position }: Pick<EntityProps, "position">) {
return (
<Entity position={position}>
<Chase speed={4} collisionMask={blockingChaserMask} />
<Views>
<mesh position={[0, 0.9, 0]}>
<capsuleGeometry args={[0.5, 0.8]} />
<meshStandardMaterial color="white" />
</mesh>
</Views>
</Entity>
);
}

A chaser ignores other chasers by default too: its mask leaves out characters.collisionLayers.npcs, so a chaser that walks never tests another walker’s capsule and a crowd costs the room no more than the same chasers spread apart. Chase keeps the crowd apart itself, moving any two chasers closer than their two radii to that distance each step. Two colliders meet only when each one’s mask holds the other’s layer: a monster that must block a doorway meets npcs again, and it stops the characters whose mask holds npcs too, not a chaser in the default. The player page lists the layers.

A chaser runs at the nearest target along the ground, so jumping or standing on a rock adds no distance. It never runs at itself, at an ally, or at one its hit would spare, by the team and match questions. A chaser with no target stands still.

targets names whom it chooses, with the kinds, layers, teams and rules Trigger lists. With TargetKind.ControlledEntities, a chaser follows a kart once the player drives it, and leaves it once the player gets out. A rule’s test takes the world, the chaser and the candidate, so it reads the chaser’s own traits: “chase this one while this happens” is a few lines.

A game whose monsters leave a downed player alone gives them a rule. DownedTrait is the engine’s, which a character carries while it is down, as Health says. The engine’s tests mount this wolf and run it past a downed character:

packages/engine/test/outside/IgnoreDowned.tsx
import {
Chase,
defineTargetRule,
DownedTrait,
Entity,
Views,
type EntityProps,
} from "@spawnite/engine";
/** Keeps the characters who stand: a downed one is left alone. As a rule
* alone in a list, it narrows the default, a character a player controls
* now. */
export const standing = defineTargetRule(
"standing",
(_world, _chaser, candidate) => !candidate.has(DownedTrait),
);
export function Wolf({ position }: Pick<EntityProps, "position">) {
return (
<Entity position={position}>
<Chase speed={4} targets={[standing]} />
<Views>
<mesh position={[0, 0.5, 0]}>
<boxGeometry args={[1, 1, 1.6]} />
<meshStandardMaterial color="grey" />
</mesh>
</Views>
</Entity>
);
}

A system sets the chaser’s target itself by writing ChaseTrait’s target: setTrait(step, chaser, ChaseTrait, { target: flagCarrier }), with both names from @spawnite/engine/core. While target names a living entity, the chaser follows it and chooses no other, whatever targets says, as a Behavior Tree follows the blackboard key its game writes in Unreal. Writing null lets it choose again. A chaser with targets={TargetKind.None} never chooses: it stands still until a system sets its target. The set target is the game’s word, so the ally rule does not apply to it, which lets a pet follow its owner. A downed target is followed and never reached, and the chaser does not follow itself or an entity of another match. Where the carrier carries a trait of the game’s own, a rule does the same with no system: defineTargetRule("flagHolder", (_world, _chaser, candidate) => candidate.has(FlagTrait)).

The step marks reached with the target a chaser has within reach, or null, and emits an event on the step it changes:

  • ChaseReachedEvent, on the chaser, on the step it comes within reach of a target, a kart included where targets names it, with that entity as target.
  • ChaseExitedEvent, on the chaser, on the step it no longer has the target in reach, with it as target. The target moved off, went down or left the room, stopped being one of its targets, as when the target’s player lets it go, the game set the chaser on another, or the chaser went down.

Each is emitted once per crossing: a chaser that stays within reach of the target emits no second ChaseReachedEvent. Once the target leaves, it reaches the next target within reach, on the same step. A downed character is never reached, and a downed chaser reaches nobody. A system reads either event with readEvents for a one-off outcome, such as a bite, a lost match or a sound, and a view hears it with useEvent. A rule that lasts while the target stays caught, such as health drained each second, reads reached on each step.

The engine’s tests run this bite against a real room: a game’s own rule reads ChaseReachedEvent and damages the character it names.

packages/room/test/outside/bite.ts
import {
ChaseReachedEvent,
dealDamage,
definePlugin,
readEvents,
type AuthoritativeStep,
defineSystem,
Phase,
} from "@spawnite/engine/core";
// A monster's bite, built as a creator's plugin is, from the engine's
// public entry alone: each time a chaser comes within its reach of a
// player's character, the room deals the character 10 damage. The chaser
// must leave the character's reach, or the character must, before
// it bites again.
/** Health a bite takes off. */
export const biteDamage = 10;
export const bite = definePlugin(() => ({
name: "outside-bite",
systems: [
// No `runsOn`, so it runs on the room alone, which adds the
// event, and in a game played alone.
defineSystem({
name: "bite",
phase: Phase.Rules,
run: (step: AuthoritativeStep) => {
for (const { record } of readEvents(step, ChaseReachedEvent))
if (record.target)
dealDamage(step, record.target, {
amount: biteDamage,
});
},
}),
],
}));

Godot’s NavigationAgent3D.target_reached, Unreal’s OnMoveCompleted and Roblox’s MoveToFinished signal arrival from the mover too. Unity, PlayCanvas and Phaser leave it to a trigger or an overlap callback. For a mover that is not a chaser, a Trigger on it gives the same events in the room, as TriggerEnteredEvent and TriggerExitedEvent.

Server. Chase has a consequence: it moves the entity toward its target, and it decides whom the entity reached. In a room, the room emits ChaseReachedEvent and ChaseExitedEvent and streams them with reached to every page, where useEvent hears them. A page never emits its own. In a room that culls by interest, a chaser no player is near moves on one tick in four and keeps hunting, as the NPC page says, unless its entity’s unseen says otherwise.

import { Chase, Entity, Views, type EntityProps } from "@spawnite/engine";
export function Yeti({ position }: Pick<EntityProps, "position">) {
return (
<Entity position={position}>
<Chase speed={4} />
<Views>
<mesh position={[0, 0.9, 0]}>
<capsuleGeometry args={[0.5, 0.8]} />
<meshStandardMaterial color="white" />
</mesh>
</Views>
</Entity>
);
}