Any City Generator · Documentation

Traffic and Road Rules

Traffic runs in Play, not in the editor level. One traffic manager drives cars along the lanes of every road network in the level: they keep to speed limits, follow each other, change lanes and merge, and stop, yield or wait for green at each junction by the rules it was built with. This article covers the manager, spawning, how cars drive, junction rules, vehicles, your own vehicle Blueprint, performance and the limits.

← All Any City Generator documentation

1 · The traffic manager

One manager runs all traffic in the level

BP_AnyTrafficManager runs traffic on every road network in its level, including networks added later. Generate City spawns one at the world origin when the level has none. To add it by hand, drag it in from Place Actors > City.

▾ Start and stop

Start Automatically On by default: traffic starts at BeginPlay. Turn it off to call Start Traffic yourself
Start Traffic Starts the simulation and fills the roads with cars on the first tick
Stop Traffic Removes every car and stops the simulation. The car actors are hidden and kept for reuse
Set Max Vehicles Changes the car limit at any time. Get Vehicle Count and Is Traffic Running read the current state

These are Blueprint functions on the manager. Vehicle Types, Simulation Settings and Seed are read once, when traffic initializes at BeginPlay. To change them during play, call Shutdown Traffic, Initialize Traffic and Start Traffic.

Keep one manager per level. A second manager would run its own, separate traffic on the same lanes, and neither would see the other's cars.

2 · Spawning

Where cars appear and how many

The manager keeps a target number of cars on the road: the smaller of Max Vehicles and Vehicles Per Kilometer times the kilometres of driving lane it can spawn on. These settings are under Spawning in the manager's Details panel.

▾ Spawning

Spawn Mode Around Viewers (default) keeps cars near the players only. Whole Network fills every road network, wherever the players are
Spawn Radius 30000 cm (300 m). With Around Viewers, new cars appear on lanes within this distance of a viewer
Despawn Radius 36000 cm (360 m). Cars farther than this from every viewer are removed. Keep it larger than Spawn Radius
Max Vehicles 250. The upper limit on the car count
Vehicles Per Kilometer 10. Cars per kilometre of driving lane
Min Spawn Distance From Viewer 5000 cm. New cars never appear closer to a viewer than this, except in the first fill
Seed 7. The same Seed, roads and viewer path give the same traffic

Viewers are the local players' view points; on a dedicated server, the manager's own location. With Around Viewers, put a Player Start inside the city or switch to Whole Network. When traffic starts, the roads are filled toward the target in one go, so expect one longer frame. After that, every Spawn Interval (0.25 s) the manager removes far cars and adds up to 8 new ones.

Cars can appear in plain view. Spawning does not check what the camera sees, so a car can appear 50 m away on screen. Lowering Max Vehicles during play does not remove cars: those above the target are just not replaced.

3 · Driving

How cars drive

Each car follows the car ahead with a standard car-following model (the Intelligent Driver Model), tuned by its vehicle type. Cars react to each other, to stop lines and to the junction rules.

  • Speed. A car aims for the lane's speed limit or its type's Max Speed Kmh, whichever is lower, times its own speed factor. It slows down early for sharper curves.
  • Following. It keeps a Minimum Gap plus Time Headway of travel to the car ahead, speeds up at most at Max Acceleration and plans its braking with Comfortable Deceleration. Harder braking, up to Emergency Deceleration (9 m/s²), is kept for emergencies. Brake lights come on when it slows down harder than 1 m/s².
  • Lane changes. Once a second, a car on a road checks whether the next lane lets it go faster, with a safe gap ahead and behind. Cars don't change lanes close to the end of the road, and never change lanes to reach a turn.
  • Merges. Where two lanes join into one, the car nearer the merge goes first and the other follows it. A lane that ends along a road merges into its neighbour zipper style. At a highway, cars on the ramp wait for a gap in the mainline; the mainline never waits.
  • Routes. At each junction a car picks its next lane at random: most often straight on, less often a curb-side turn, then a far-side turn, and rarely a U-turn.

▾ Simulation Settings

Enable Lane Changes On
Lane Change Duration 2.5 s. How long the move into the next lane is blended
Lane Change Min Distance To Junction 4000 cm. No lane changes closer than this to the end of the road
Speed Factor Min, Max 0.92, 1.08. The range of each car's own speed factor

These are under Simulation > Simulation Settings on the manager, next to the junction timings used below. They are read when traffic initializes.

4 · Junction rules

Who goes first at a junction

Each junction's rules are fixed when its road network builds. A car asks for permission as it nears the stop line and never enters a junction without it: it waits at the line. The rule set is the junction node's Control, under Network > Layout in the road network's Details panel.

▾ Control

Traffic Signal Green: go, while far-side turns wait for a gap in oncoming traffic. Yellow: only cars that can no longer stop comfortably. Red: stop
All Way Stop Every car stops at the line for at least 1 s (Min Stop Seconds), then first come, first served
Priority Stop The major road has right of way. Cars on a minor road come to a full stop, wait for a gap in major traffic, then go in order of arrival
Priority Yield As Priority Stop, without the full stop
Roundabout Used by every roundabout. Entering cars need a 3 s gap (Merge Gap Seconds) in ring traffic and room on the ring; cars on the ring have right of way
Uncontrolled First come, first served where lanes cross or merge. A junction without conflicting lanes lets every car through
Auto The default, picked from the road classes that meet. Arterial with Collector or larger: signals. Arterial with Local: Priority Stop. Only Collectors: All Way Stop. Collector with Local: Priority Yield. Only Local streets: All Way Stop at four or more roads, Priority Yield at three

Signals cycle through green, yellow and all-red for the node's Signal Green Seconds (18 s), Signal Yellow Seconds (3 s) and Signal All Red Seconds (1.5 s). Each junction starts at a different point of its cycle, so neighbours are out of step, the same way every run. At highway interchanges the ramps yield and the mainline never waits.

Gridlock clears itself. At stop and priority junctions, a car that has waited 12 s (Deadlock Break Seconds) stops giving way to earlier arrivals and to waiting cars with priority, but still waits for cars in its path and for room after the junction. A car stuck for 60 s (Stuck Despawn Seconds) is removed, and spawning replaces it.

5 · Vehicles

Vehicle types and the six stock vehicles

The manager's Vehicles > Vehicle Types lists vehicle type data assets. Each new car picks a type by Spawn Weight, then one of that type's Vehicle Classes and one of its Body Colors at random.

▾ Vehicle type

Vehicle Classes The vehicle Blueprints this type spawns, one picked at random. List a Blueprint twice to make it twice as likely. Empty: never spawned
Body Colors Paint colours, one picked per car. Empty: white
Length, Width 450, 185 cm. The footprint used for gaps and spacing; match it to the Blueprint's body
Max Speed Kmh 150 km/h. Cars of this type aim for the lower of this and the lane's speed limit
Max Acceleration, Comfortable Deceleration 2.5, 3 m/s². How hard the car speeds up and plans to brake
Minimum Gap, Time Headway 2 m, 1.4 s. The distance kept to the car ahead
Spawn Weight 1. This type's share of new cars; 0 never spawns

The plugin ships six types, DA_Vehicle_*, each spawning the BP_AnyTraffic_* Blueprint of its name, and BP_AnyTrafficManager lists all six:

  • Sedan: 450 × 185 cm, up to 150 km/h.
  • Hatchback: 400 × 175 cm, 140 km/h.
  • SUV: 480 × 195 cm, 140 km/h.
  • Van: 520 × 200 cm, 130 km/h.
  • Bus: 1200 × 255 cm, 90 km/h, Max Acceleration 1.2, Time Headway 1.8 s, Spawn Weight 0.3.
  • Truck: 900 × 250 cm, 100 km/h, six wheels, Max Acceleration 1.0, Time Headway 1.8 s, Spawn Weight 0.4.

One size per type. Every Blueprint in a type's Vehicle Classes shares its footprint and driving, so list models of about the same size, and make another type for a different size. Vehicle types are read when traffic initializes.

6 · Your own vehicle

Making a vehicle Blueprint

A vehicle Blueprint is a child of Ax Traffic Vehicle with a body mesh and one wheel component per wheel. The simulation moves it; the actor never moves itself. The quickest start is to duplicate a BP_AnyTraffic_* and swap its meshes. From scratch:

  1. In the Content Browser, click Add > Blueprint Class, expand All Classes, pick Ax Traffic Vehicle and name it, for example BP_MyTaxi.
  2. In the Blueprint's Components panel, select Body Mesh and set its Static Mesh. The actor origin is the centre of the footprint on the ground, with +X forward; use the component's Location and Scale for a mesh with another pivot.
  3. Click Add > Ax Traffic Wheel once per wheel, attached to Vehicle Root. Set each wheel's Static Mesh (pivot at the wheel centre, axle along Y), its Location, and Wheel > Wheel Radius to the visible radius in cm after scale (35 by default).
  4. Compile and Save. Add the Blueprint to a vehicle type's Actor > Vehicle Classes, set the type's Length and Width to the body's footprint, and list the type in the manager's Vehicles > Vehicle Types.

Wheels roll by the distance the car moves. Paint and brake lights go through the body mesh's Custom Primitive Data: indices 0 to 2 hold the body colour, 3 the brakes (0 or 1). Build your car material on MF_AnyTraffic_VehiclePaint, or read those values directly. For per-car setup, override On Vehicle Spawned in My Blueprint > Functions > Override and call Parent: On Vehicle Spawned to keep the paint. On Vehicle Despawned and On Braking Changed are events for the Event Graph.

Reused actors keep their state. Cars are pooled: a removed car's actor is hidden and reused for the next car of its class, with its variables, attached actors and material changes. Reset them in On Vehicle Spawned or On Vehicle Despawned. An On Vehicle Spawned override that never paints leaves the previous car's colour.

7 · Performance

Collision, distant cars and debug drawing

Every car is an actor that follows the simulation. Collision is off by default, and cars that are far away and out of sight update less often.

▾ Collision and Vehicles

Collision On Spawn Off. On the vehicle Blueprint, under Collision. On: the body blocks traces and pushes physics objects. Set Vehicle Collision Enabled switches it during play
Full Rate Distance 8000 cm. On the manager, under Vehicles. Cars within this distance of the nearest viewer update every frame
Far Update Interval 3 frames. Cars farther away and not on screen update every this many frames; 1 updates every car every frame

A car on screen always updates every frame, however far away. Only the actors are throttled: the simulation still steps every car, so spacing and junctions don't change. Stopped cars cost almost nothing, and with collision off the bodies stay out of physics. Spawning an actor is the expensive step: actors are pooled and reused, but the first fill can spawn up to Max Vehicles actors in one frame. Profile with stat game and Unreal Insights.

Draw Debug (Debug on the manager) shows each car's footprint, green and red while braking. Near a stop line it draws a line to the junction lane the car wants: green when granted, yellow when allowed but not yet committed, red when it must stop. A car waiting a second or more shows Wait N s.

8 · Limits

Limits and networking

What traffic does not do, so you can plan around it.

  • Local, cosmetic traffic. Nothing replicates, the car actors included. Every machine that runs a manager, dedicated servers too, simulates its own cars. Turn Start Automatically off where traffic isn't needed.
  • Collision is one-way. With collision on, car bodies block traces and push physics objects, but the simulation never reacts to them: cars react only to each other and to stop lines, and drive through, or push, whatever is in their lane.
  • No pedestrians. Nobody uses the crosswalks.
  • Random routes. Cars have no destinations; each picks its next lane at random.

Next: how roads are built from profiles, and how to swap in your own meshes and materials.