Universal Traffic System

Shanto Lab Unity Tool

Universal Traffic System

Production-ready AI traffic framework for Unity. Author lanes, control intersections, spawn believable vehicles, and ship with stable performance across mobile, PC, and console.

Overview

The Universal Traffic System (UTS) is a modular runtime + editor toolkit for creating road traffic in Unity.
It combines lane & waypoint authoring, vehicle AI with lane changes and overtakes, smart intersection control, perception sensors, pooled spawning, lights & signals, and vehicle audio—wrapped in an in-editor documentation window.

Feature Highlights

  • Lane & Waypoint Authoring — Build multi-lane graphs with left/right neighbors and connections.
  • Vehicle AI — Smooth following, adaptive time-headway, safe merges, optional opposite-lane overtakes.
  • Intersections — Stop&Go, Timed Plan, Actuated Plan (gap-out + demand arbitration), Priority Main/Minor.
  • Perception — Forward spherecasts with FOV, dynamic detect distance, robust layer filtering.
  • Spawner & Pooling — Distance & visibility-safe spawns, overlap checks, ground snap, object pooling.
  • Lights & Signals — HDR emissive meshes via MaterialPropertyBlock + optional real Lights with LOD.
  • Vehicle Audio — Speed-scaled engine loop (pitch/volume) with distance pause and one-shot horn.
  • Editor Docs — In-editor window with guides, examples, FAQ, and changelog.

Modules

Lane & Waypoint Authoring

  • Create and update lanes from Tools ▸ Universal Traffic System.
  • Place waypoints (5–15 m on straights, 2–5 m on tight curves).
    Keep lanes ~100–300 m and chain them for stability.
  • Connect lanes (next/previous) and link left/right neighbors for multi-lane roads.

Vehicle AI (Lane Change & Overtake)

  • Evidence-based block detection with rise/decay and hysteresis thresholds.
  • Same-direction merges using capsule occupancy windows (ahead/behind).
  • Optional opposite-lane pass with oncoming and return clearances; intersection-aware inhibition.
  • Dynamic tuning curves adapt minGap, headway, detect distance, and emergency stop to speed.

Intersections

  • Stop&Go — uncontrolled, always green.
  • Timed Plan — deterministic phases with min/amber/all-red per phase.
  • Actuated Plan — gap-out after min green; selects next phase by recent demand.
  • Priority Main/Minor — keep Main green; serve Minor on demand with safe transitions.
  • APIs: GetSignalForLane(lane), AllowsRightOnRed(lane), GetStopPoint(lane, pos).

Perception Sensor

  • Spherecast-based lead detection within configurable FOV and layer masks.
  • Dynamic detection distance from speed; ignores self colliders.

Spawner & Pooling

  • Multiple prefab pools with preload and maxPoolSize; reuse over allocation.
  • Spawn strategies: AnyWaypoint, RandomSegment, StartEndBias.
  • Safety gates: min/max distance from player, off-screen requirement, overlap sphere checks, ground snap.

Lights & Turn Signals

  • Emissive mesh control for tail, brake, reverse, head, indicators (left/right) with HDR colors.
  • Optional real Light components enabled only within a near distance threshold.
  • Blink control with blinkHz + per-vehicle phase offset; automatic signals from AI or lane geometry.

Vehicle Audio

  • Engine loop pitch/volume mapped to speed; idle and max tunables with speedAtMaxPitchKph.
  • Distance LOD: pause beyond audioEnableDistance; one-shot horn via Honk().

Performance & Optimizations

  • Pooling-First — avoid GC and instantiate spikes by reusing vehicle instances.
  • Visibility-Aware Spawns — off-screen + distance gates reduce pop-in and camera-near work.
  • LOD Updates — near/far tick rates for lights and audio; real Lights only when close.
  • GPU-Friendly EmissionMaterialPropertyBlock + instancing on shared materials.
  • Physics Load Control — tight LayerMasks for sensors and occupancy; sensible waypoint spacing.
  • Actuated Signals — demand-aware timing minimizes wasted red/green time.
  • Dynamic Tuning — speed-adaptive headway/gaps/detection with smoothing to prevent jitter.

Recommended Starting Values

  • City: headway 1.1–1.3 s, minGap 5–8 m, decisionDistance 22–28 m, oncomingFrontClear 38–45 m.
  • Highway: headway 1.4–1.6 s, minGap 8–14 m, decisionDistance 28–36 m, oncomingFrontClear 45–60 m.
  • Merge window: ahead 18–22 m, behind 10–12 m, radius 1.6–1.8 m; Debounce enter 0.7 / exit 0.35.

Quick Start (10 minutes)

  1. Create lanes & waypoints; link neighbors for multi-lane roads.
  2. Drop a vehicle prefab (Rigidbody + your controller) and add AI components.
  3. Place an IntersectionZone and pick a mode (optional).
  4. Add the prefab to VehicleSpawner pools and press Play.

API Highlights

// Intersections
LightColor sig = zone.GetSignalForLane(myLane);
bool ror = zone.AllowsRightOnRed(myLane);
Vector3 stopPt = zone.GetStopPoint(myLane, transform.position);

// Lane change / AI
laneChange.Tick(agent, Time.deltaTime);
agent.SetLane(newLane);
blackboard.desiredBrake = Mathf.Clamp01(x);

// Lights & audio
lights.SetTurnSignal(TurnSignal.Left);
lights.SetHeadlights(true);
audio.Honk();

Compatibility

  • Unity 2021.3+ (tested on 2021 LTS/2022/2023/6000 series).
  • Render Pipelines: Built-in, URP, HDRP (renderer-agnostic core).
  • Targets: PC/Console/Mobile (settings provided for each tier).
  • Dependencies: none beyond Unity; works with your own vehicle controller (requires Rigidbody and a speed readout).

What’s Included

  • Runtime & Editor C# scripts (full source) — no DLL lock-in.
  • Lane authoring tools with gizmos.
  • PerceptionSensor, LaneChangeBehavior, AICarAgent + Blackboard adapters.
  • IntersectionZone with multiple modes and phase timing.
  • VehicleSpawner with multiple strategies and pooling.
  • VehicleLightController & VehicleSignalAIAdapter; VehicleAudioController.
  • In-editor documentation window (Overview, Setup, Overtake, Intersections, Spawner, Lights/Audio, FAQ, Changelog).

Use Cases

  • Open-world driving games and city scenes.
  • Simulation and training environments.
  • Rapid prototyping of believable road networks.

Notes & Limits

  • Focus: single-player simulation; deterministic lockstep networking is out of scope.
  • Very large worlds may require floating origin/scene streaming.
  • Provide your own vehicle meshes/materials if desired.

Support & Updates

For support, include Unity version, render pipeline, target platform, and a minimal repro scene.
Updates follow semantic versioning (MAJOR.MINOR.PATCH) with hotfixes for stability.

Play