Class TrafficBehaviour
Abstract MonoBehaviour that translates sensor collision events into active TrafficProcess instances, driving autonomous actor behaviour such as gap-keeping, stopline compliance, and pedestrian avoidance.
public class TrafficBehaviour : MonoBehaviour
Inheritance
Derived
Remarks
Traffic actors must respond to dynamic obstacles (other vehicles, pedestrians, stoplines)
without a global path-planner. TrafficBehaviour achieves this reactively: when a
sensor fires its OnDetected event, Collider) creates
a new TrafficProcess configured with keep/stop distances and adds it to the
active list. Each process drives the actor's speed independently of the movement command
system, so braking and path-following coexist.
The IsHijacked / Object) mechanism lets
AIBrainComponent take over the update cycle when the Behaviour Tree is running.
Update is suppressed; the BT calls DoUpdate explicitly each tick,
giving it precise control over when traffic processes are evaluated relative to movement
commands.
Concrete subclasses (VehicleTrafficBehavior, PedestrianTrafficBehavior)
override ContactStruct@) to map detected layer masks to the correct process
type without the base class knowing about vehicle physics or pedestrian rigs.
Fields
keepDistanceOtherCarsAreaSensors
Narrower forward-facing sensors for gap-keeping behind other vehicles. Multiple sensors cover lane-change scenarios where the lead vehicle drifts laterally.
[Space(10)]
[Tooltip("Sensors used to maintain distance from other cars.")]
[SerializeField]
protected Optional<Sensor[]> keepDistanceOtherCarsAreaSensors
Field Value
searchAreaSensor
Wide-area sensor that sweeps for all traffic objects (vehicles, pedestrians, stoplines).
[Header("Sensors References")]
[Tooltip("Sensor responsible for detecting traffic in the surrounding area.")]
[SerializeField]
protected Optional<Sensor> searchAreaSensor
Field Value
stoplineContactParameter
Distance thresholds and dwell time applied when stopping at a stopline detected by searchAreaSensor.
[Tooltip("Contact parameters for handling stoplines, including stop duration.")]
[SerializeField]
protected TrafficBehaviour.StoplineContactStruct stoplineContactParameter
Field Value
trafficObjectContactParameter
Distance thresholds applied when reacting to objects detected by the wide-area searchAreaSensor that are not stoplines.
[Space(10)]
[Tooltip("Contact parameters for handling general traffic object interactions.")]
[SerializeField]
protected TrafficBehaviour.ContactStruct trafficObjectContactParameter
Field Value
Properties
CanProcessTraffic
true when this component is enabled and has at least one active process to run.
Used by the BT to determine whether to call DoUpdate this tick.
public bool CanProcessTraffic { get; }
Property Value
bool
IsHijacked
true when an external controller (e.g., AIBrainComponent) is driving
this actor's update cycle, suppressing Update so the BT can call
DoUpdate at its own cadence.
public bool IsHijacked { get; }
Property Value
bool
KeepDistanceSensors
Active gap-keeping sensors, or null if none are assigned in the Inspector.
public Sensor[] KeepDistanceSensors { get; }
Property Value
Sensor[]
Processes
Read-only view of the active process list for external inspection or debugging.
public List<TrafficProcess> Processes { get; }
Property Value
Methods
CheckCanAddedProcess(TrafficProcess)
Validates that process can be added without violating its declared
ProcessConstraint.
protected bool CheckCanAddedProcess(TrafficProcess process)
Parameters
| Name | Type | Description |
|---|---|---|
process | TrafficProcess | Candidate process to validate. |
Returns
bool — true if the process may be added; false if the constraint rejects it.
Remarks
Prevents duplicate gap-keeping processes from stacking when the same vehicle is detected by overlapping sensors simultaneously, which would cause the actor to over-brake.
DoUpdate()
Advances all active processes by one tick and removes any that have completed.
public virtual void DoUpdate()
Remarks
Marked virtual and public so the BT (AIBrainComponent) can call
it explicitly when IsHijacked suppresses the Unity Update loop.
OnDestroy()
Unsubscribes from sensor events to prevent callbacks on a destroyed component.
protected void OnDestroy()
OnKeepDistanceSensorDetected(Collider)
Creates and starts the appropriate TrafficProcess when a gap-keeping sensor detects another object, subject to the constraint check.
protected virtual void OnKeepDistanceSensorDetected(Collider other)
Parameters
| Name | Type | Description |
|---|---|---|
other | Collider | The detected collider from the gap-keeping sensor. |
OnSearchAreaSensorDetected(Collider)
Reserved callback for the wide-area search sensor; not yet implemented in the base class. Subclasses may override this to react to objects entering the search area.
protected void OnSearchAreaSensorDetected(Collider other)
Parameters
| Name | Type | Description |
|---|---|---|
other | Collider | The detected collider. |
SetController(Object)
Registers controller as the external driver of this actor's update
cycle, setting IsHijacked to true.
public void SetController(Object controller)
Parameters
| Name | Type | Description |
|---|---|---|
controller | Object | The BT component (or other controller) taking over updates. |
Start()
Initialises the process list and subscribes to sensor collision events.
protected void Start()
TryGetProcess(GameObject, out TrafficProcess, out ProcessData, out ContactStruct)
Maps otherObject to the correct TrafficProcess subclass
and populates its ProcessData, using the object's layer
to distinguish vehicles from pedestrians.
protected virtual bool TryGetProcess(GameObject otherObject, out TrafficProcess process, out TrafficProcess.ProcessData processData, out TrafficBehaviour.ContactStruct currentContactValueParameter)
Parameters
| Name | Type | Description |
|---|---|---|
otherObject | GameObject | The detected object. |
process | TrafficProcess | Receives the newly created process instance. |
processData | TrafficProcess.ProcessData | Receives the process initialisation data. |
currentContactValueParameter | TrafficBehaviour.ContactStruct | Receives the contact thresholds for this interaction. |
Returns
bool — true if a process was created; false if the object should be ignored.
Remarks
The base implementation differentiates on the "Mine" layer (pedestrians/player)
versus all other layers (vehicles). Subclasses override this to add vehicle-type-specific
logic (e.g., checking relative speed before spawning a braking process).