logo
AutoRunnerDriver

vehicle · vehicle-combat

AutoRunnerDriver

1
Unlock

Install with the CLI:

bjs download AutoRunnerDriver

AutoRunnerDriver

The drivers of every car the player isn't driving.

What it does

AutoRunnerDriver works in road space, "how far along" and "how far across", because that is how a driver thinks: stay in lane two, pull alongside, get behind the player and push. Each role decides where across the road it wants to be and how fast it wants to go, then writes throttle, brake and steer onto its car's CarDrive.

RoleWhat it does
civilianholds its lane at its own pace; follows a slower car, changes lane when there is room, merges when its lane ends
rammercatches the player from behind, rides alongside, swerves in
gunnerholds a lane over and fires a shotgun volley across
shovergets behind and alongside and leans on the player toward the edge
truckholds the slow lane at a steady pace
policedrives like a civilian until the player speeds near it or hits it, then gives chase

Use it in a scene

A scene is just data — a list of entities, each with its components. At startup the SceneLoader turns this JSON into a live world; edit the file and reload to rebuild it.

Build it from scratch with the bjs CLI:

  1. 1Scaffold a project
    npm create @babylonjsmarket/arcade@latest my-game

    World, renderer, and dev server — ready to run.

  2. 2Install dependencies
    cd my-game && npm install
  3. 3Add AutoRunnerDriver
    bjs download AutoRunnerDriver

    Copies its source into src/ so the scene resolves.

    First time? Run bjs login once.

  4. 4Paste the scene into src/scenes/arcade-room.ts and run
    npm run dev

    SceneLoader builds the world from the JSON; reload to rebuild.

JSON
{
  "Gunner": {
    "tags": ["car", "enemy", "gunner"],
    "components": {
      "MeshPrimitive": {
        "primitive": "box",
        "width": 1.9,
        "height": 1.3,
        "depth": 4.4
      },
      "CarDrive": { "keyboardControlled": false, "maxSpeed": 60 },
      "AutoRunnerDriver": {
        "role": "gunner",
        "cruiseSpeed": 40,
        "sideGap": 5.4,
        "shellPool": "shell",
        "pellets": 3
      }
    }
  }
}

The player's car carries the player tag, which is what attackers look for. A Gunner also needs a shell pool of AutoRunnerProjectile rounds. With no HighwayRoad in the scene, the road is taken to be straight along +Z with three lanes.

Steering by pure pursuit

Every role steers the same way. Pick a point some way up the road at the offset you want, and turn toward it:

TypeScript
  // Pure pursuit: a point up the road at the offset we want.
  const look = 10 + Math.abs(i.speed) * 0.45;
  const pt = road.toWorld(i.s + look, aimU);
  const desired = Math.atan2(pt.x - i.x, pt.z - i.z);
  let steer = steerFor(i.heading, desired, p.steerGain);
text
civilian at 26 m/s, moving over one lane (3.7 m) on a straight road
look ahead = 10 + 26 × 0.45     = 21.7 m
aim angle  = atan(3.7 ÷ 21.7)   ≈ 0.17 rad
steer      = 0.17 × 1.7         ≈ 0.29 of full lock

A faster car looks further up the road, so the same lane change asks for less wheel and it drifts over instead of jerking.

Attack runs

An attacker closes in fast from behind and matches pace once level. Once level, every attackEvery seconds (give or take 30%), it makes a run lasting attackLength. A rammer swerves in, a shover leans the player toward the nearer edge, and a gunner fires one volley of pellets shells. It gives up once the player is more than giveUpBehind metres ahead, or 45 m behind.

Police

A police car keeps its lane at cruiseSpeed. It starts a chase when the player goes faster than speedLimit within sight metres, ahead or behind, or when the player rams it or shoots it. In a chase it drives like a shover, flashes its roof lights (headlights.beacon.set) and sends autoRunnerDriver.pursuit with on: true.

The chase ends after calmSeconds in a row of the player keeping to the limit, or being out of sight. Out of sight means hidden (a car in the weapons truck is a ghost, and drivers cannot see ghosts) or more than giveUpBehind metres ahead. While the player keeps to the limit, the police car only follows, 12 m back.

A scene is just data — a list of entities, each with its components. At startup the SceneLoader turns this JSON into a live world; edit the file and reload to rebuild it.

Build it from scratch with the bjs CLI:

  1. 1Scaffold a project
    npm create @babylonjsmarket/arcade@latest my-game

    World, renderer, and dev server — ready to run.

  2. 2Install dependencies
    cd my-game && npm install
  3. 3Add AutoRunnerDriver
    bjs download AutoRunnerDriver

    Copies its source into src/ so the scene resolves.

    First time? Run bjs login once.

  4. 4Paste the scene into src/scenes/arcade-room.ts and run
    npm run dev

    SceneLoader builds the world from the JSON; reload to rebuild.

JSON
{
  "PoliceBlueprint": {
    "tags": ["car", "police"],
    "components": {
      "AutoRunnerDriver": {
        "role": "police",
        "cruiseSpeed": 28,
        "speedLimit": 36,
        "sight": 45,
        "calmSeconds": 3
      },
      "Headlights": { "beacon": true, "beaconHeight": 1.45 }
    }
  }
}

Losing control

Smoke (autoRunnerDriver.blind) makes a driver wander, slow down and hold fire. In a spin-out it lets go of the throttle, brakes lightly and steers half as hard. A car tagged wreck coasts to a stop, and brakes only while it is still rolling, because CarDrive turns a brake held at a standstill into reverse.

Settings

FieldDefaultWhat it does
role"civilian"civilian, rammer, gunner, shover, truck or police.
cruiseSpeed26Pace on an empty road, m/s.
steerGain1.7How hard the driver steers toward its line.
sideGap5Gunner: how far across from the target it rides, metres.
attackEvery2.6Seconds between an attacker's runs.
attackLength0.8Seconds one run lasts.
giveUpBehind60How far ahead the player can get before an attacker gives up, metres.
speedLimit36Police: the speed that starts a chase, m/s.
sight45Police: how far along the road it spots a speeder, ahead or behind, metres.
calmSeconds3Police: seconds of good driving, or out of sight, that end a chase.
targetTag"player"Tag of the car an attacker goes after.
lane0Lane a civilian starts in. The spawner sets it.
shellPool"shell"Gunner: the pool its shells come from.
shellSpeed75Gunner: shell speed on top of the car's own, m/s.
shellDamage5Gunner: damage per shell.
pellets3Gunner: shells per volley.
spread0.1Gunner: angle between pellets, radians.

Events

EventPayloadDirection
autoRunnerDriver.fired{ entityId, x, z, targetId }emits
autoRunnerDriver.pursuit{ entityId, targetId, on }emits
headlights.beacon.set{ entityId, on }emits
autoRunnerDriver.blind{ entityId, seconds }listens
carCollision.hit{ a, b, x, z, closing, dvA, dvB }listens
autoRunnerProjectile.hit{ entityId, targetId, owner, kind, x, z, damage }listens

Needs

  • CarDrive is the car each driver steers. The driver writes its throttle, brake and steer.
  • CarCollision tells the driver when its car is spinning, and how long each car is for following gaps. Its hit event is how police notice a ram.
  • MeshPrimitive gives every car's position.
  • HighwayRoad supplies the lanes, width and curve of the road.
  • AutoRunnerProjectile fires a Gunner's shells, and its hit event is how police notice a shot.

More like this

AutoRunnerDirector
AutoRunnerProjectile
CarCollision
CarDrive
Headlights
HighwayRoad
MeshPrimitive
RoadAudio
RoadHazard
AIGoalSeek
AIKick
AIZone
AiDriver
AiPilot
AutoRunnerBomber
AutoRunnerHUD
AutoRunnerWeapons
BallPossession
BallPursuit
BoatMode
BoatWater
CarPaint
ContactDamage
ContactImpulse
DayNightCycle
Enemy
EnemySpawner
FreighterChain
HighwaySpawner
LineOfSight
PokerAI
PokerAIBill
PokerAICali
PokerAIRandy
PokerAIShark
PokerAISurge
PokerAITilt
RoadCamera
RoadFX
RoadSpawner
RoadTreadmill
RoadsideScenery
SmokeScreen
SupplyTruck
SurfacePatch
TrafficConvoy
TwinStickEnemy
WaveDirector
WeaponsVan

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search