Skip to main content

Projectiles

Use projectiles for arrows, spells, bullets, beams, and other temporary moving effects that need server-side impact validation without synchronizing their position every frame. The server is authoritative. It emits compact projectile spawn, impact, and destroy batches. The client predicts the visual movement locally, including a single local raycast at spawn time to avoid drawing projectiles past obvious client-side hitboxes while it waits for the server impact, and renders each projectile with a registered CanvasEngine component. The batches follow the sync rate of the map, not the number of shots: what emit() queues is sent once per sync interval (throttleSync, 50 ms by default), so the shots of one interval share one message and a shot is drawn up to one interval after emit(). A projectile that stops on its hit sends its impact once, in its destroy entry; the client still calls projectiles.onImpact for it. When a projectile uses a custom server-side canHit filter, the client keeps predicting movement but skips local impact raycast clamping by default because arbitrary server gameplay rules cannot be represented safely on the client. If the filter still matches client-visible physics, opt back in with collision.predictImpact: true.

Server Usage

Emit a projectile from a player:
spreadDegrees applies a random direction offset of +/- spreadDegrees / 2. You can use accuracy instead when you prefer a normalized precision value from 0 to 1; 1 is perfectly accurate, 0 can deviate up to 30 degrees. payload stays server-side and is useful for damage, states, knockback, or any other gameplay data. params is sent to clients and should contain only visual data used by the projectile component. You can also emit from the map:

Shoot Toward The Pointer

Projectiles can also use a direction vector. For pointer-based attacks, send the target position through the normal action input flow, then validate and resolve the shot on the server.
For keyboard attacks, configure the action key to send the same payload:
This same input shape can be reused for future map or event interactions, for example map:click with a world position or event:click with an event id. The client sends intent and context; the server still owns validation, collisions, damage, and impacts.

Aim At A Target

Give target instead of direction to aim at a point or at an entity (a player or an event, aimed at its center). The server computes the direction.
An arc lands on its target; range is then its maximum reach, so a target that is too far makes it land short. landing makes the arc hit what stands where it lands (see Behaviors). Other trajectories only take their direction from the target and keep flying their range.

Repeats And Patterns

Use repeat for a compact burst. The server broadcasts one spawn batch with per-projectile delays, so clients can render the whole burst without receiving a position update every frame.
Use pattern for common shapes:

Trajectories

trajectory.type picks the path. The server simulates it and the client draws it with the same function, so only its name and parameters are synchronized.
The way back of return aims at the current position of the launcher, on the server and on the client.

Custom trajectories

Register a trajectory once in shared code (imported by both the server and the client). It is a pure function of the elapsed time:
The context also gives owner(), the current center of the launcher. z is the height above the ground and phase is a free label sent to the component.

Behaviors

These options work with every trajectory: An arc flies over everything, so landing is how it hits: one target for an arrow, an area with pierce for a bomb. Each entity it hits triggers onHit and can still answer through onProjectileHit. A landing that hit something does not linger on the ground, unless the target replied stick. Hooks of one projectile, on the server: Every hook receives projectile, owner and map. Bounces, returns and planted projectiles are sent to the clients as compact updates.

Targets Reacting To Impacts

Any event (or player) hit by a projectile can react itself with onProjectileHit, without changing the weapon. It runs on the server, before the projectile onHit, and its reply decides what the projectile does:
The hook receives the projectile (type, ownerId, params, trajectory) and { owner, map, x, y, hit }. Module hooks can reply too: event.onProjectileHit and player.onProjectileHit in defineModule() may return "pass", "stick" or "stop" at once (a promise cannot decide: the projectile does not wait). The first module that replies decides, and the event’s own onProjectileHit has the last word. Scripts reply the same way with event.projectileHit (see the scripts guide).
Loot pickups never stop projectiles: they fly over them.

Impact Hooks

Use projectile hooks to apply gameplay effects when the server confirms an impact:
You can also filter hits per projectile:
If the canHit filter only narrows server gameplay targets and the normal map physics hitboxes are still safe for visual prediction, enable local clamping:

Standard Look

A projectile type without a registered component is drawn by the standard projectile component of @rpgjs/client. The server only names the projectile with its type; its look is declared once on the client, in projectiles.looks:
It draws the projectile above a shadow at its height z, tilts it along its climb and fall, leaves it planted when it sticks, and plays an impact that depends on what it ended on: sparks on a target, sparks thrown back by a wall, dust on the ground. These fields are typed by ProjectileLook, exported by @rpgjs/client. A look can also be registered at runtime with engine.registerProjectileLook(type, look). A preset of its own ({ emitters: [...] }, such as the parts of a Studio Visual Effect) gives a projectile particles that no built-in preset has:
The same fields sent in the server params override the look for one shot. Keep that for what really changes per shot, such as the color of an elemental arrow; everything else belongs to the client look, so it is not sent with every projectile:
Register your own component when a type needs a look these fields cannot give. A look registered for that type is then handed to the component, merged into its params.

Client Registration

Register a CanvasEngine component to draw a projectile type yourself:

Projectile Component

Projectile components receive predicted movement props from the client runtime.
Common props:
  • id, type, ownerId
  • x, y, origin, direction, angle
  • flightAngle, the angle to draw the sprite with: angle tilted up while the projectile climbs and down while it falls
  • speed, range, ttl, distance, elapsed, progress
  • z, the height above the ground: draw the sprite at y - z and a shadow at y
  • phase, the part of the path (out, back, or stuck once the projectile is planted or lingering)
  • trajectory, its name and parameters
  • impactProgress from 0 to 1 while the client keeps an impacted projectile visible for its impact animation
  • index, count for repeated or patterned projectiles
  • params for visual customization
  • impact when the server confirms a collision. impact.surface tells what the projectile ended on: target, wall, or ground for an arc that landed without hitting anything. While impact is present, x, y, and distance are pinned to the authoritative impact point. Before the server confirmation arrives, the client may also pin x, y, and distance to a locally predicted impact point; impact remains undefined until the server confirms the collision.
Position and progress props (x, y, z, phase, angle, flightAngle, distance, elapsed, progress, impact, impactElapsed, impactProgress, destroyed) are updated on every frame without rebuilding the component. Before an impact, impact is null and impactElapsed / impactProgress are 0. Do not apply damage in the component. Components are visual only; gameplay effects belong in server projectile hooks.

Recipe: an arrow with a trail

A component can keep its own state. This arrow remembers its last positions to draw a fading trail, stops the trail once planted (phase is stuck), and reads its color from the params sent by the server:
Register it like any projectile component: projectiles: { components: { arrow: ArrowProjectile } }. For a particle trail, use <Fx name="..." loop /> from @canvasengine/presets instead of the Graphics line.