Initializing Asset

Initialize asset using the TrafficComponent script

Here is a preview of the traffic component:

Traffic Component properties


Vehicles will instantiate around this GameObject. It is recommended to assign here your player or the active camera.

Nr Of Vehicles

Maximum number of vehicles that will be available during runtime. This number cannot be increased during runtime so it should be set to the highest car density required at some point in your app.

Vehicle Pool

Assign a vehicle pool created using the instructions from Vehicle Pool

Active Squares Levels

Learn more about the active level from the Prepare Scenepage.

The number entered here represents the proximity from the player within which the adjacent cells will be active. For instance, 1 means only the cells immediately surrounding the player are active. Meanwhile, 2 signifies that not only the cells around the player are active but also the cells around those cells become active.

Min distance to add

Vehicles are spawned exclusively in the active cells, located near the player, with mechanisms in place to prevent the user from observing vehicles suddenly appearing.

Vehicles will spawn within any distance from the player's location if there is a building obstructing the view. For instance, a vehicle might spawn behind a corner only 10 meters away, but if that corner is concealed by a building, blocking the line of sight from the player, the vehicle will spawn there.

However, beyond the MinDistanceToAdd, vehicles will spawn even without any obstructions or buildings blocking the view. For instance, in the case of a straight road like a highway where no buildings hinder the view, vehicles will only spawn at distances greater than the MinDistanceToAdd.

Vehicles will exclusively spawn on active cells. If all active cells are closer to the player than the MinDistanceToAdd, no vehicle will spawn in front of the player.

It's important to ensure that the MinDistanceToAdd is smaller than 2 grid sizes, or alternatively, increase the Active Squares Levels until the MinDistanceToAdd is situated inside the active squares.

If set to -1. the system will try to approximate the best distance based on grid size.

Distance to remove

This value represents the approximate distance from the player at which a vehicle is flagged to be removed from the scene.

When a vehicle is marked as ready to be removed, the system checks if it is within the view of any camera. If the vehicle is not within any camera's view, it will be removed from the scene and respawned closer to the player. This process helps increase the local density of vehicles around the player.

The system uses OnBecomeVisible method so if you see a vehicle inside the editor with the scene camera it will never be removed since the method also accounts for the scene view camera.

If set to -1. the system will try to approximate the best distance based on grid size.

Light Time

Setting both green and yellow light times will override any other values previously set on the traffic light intersection. This serves as the master intersection time. If set to -1, the values designated at the intersection level will be used for each individual traffic light intersection.

Initial Active Vehicles

If set to -1, all vehicles will be instantiated and activated from the beginning.

In scenarios where the start location is not in a high-traffic area, such as a rural location, you can specify the number of vehicles that should be active from the start. Additionally, you can utilize the SetDensity method from the API to adjust the traffic density later in your application based on your specific needs.

Use Waypoint Priority

Get familiarized with waypoint priority by reading Waypoint Priority Setup

When this feature is enabled, waypoints will be assigned weights from the Priority Window. Implementing priority increases the likelihood of a car being spawned on a particular waypoint, determined by the weight assigned to that waypoint. This setup is particularly useful in highway scenarios that have secondary roads nearby.

Keep in mind that using priority will slightly increase the waypoint selection time. Use it only if you need it.

Lights On

If the level starts at night, the vehicle will be instantiated with lights on by default.

Disable Waypoints Area

Mark specific waypoints as disabled. Vehicles will neither spawn on nor be permitted to drive over these disabled waypoints. This feature proves useful for enabling temporary crash events or other disruptions to traffic flow.

The disabled area is circular, determined by the center point and a defined radius that can be set using the provided fields.

Initialize asset using the MultiplayerTrafficComponent script

This has the same properties as the Traffic Component but should be used when multiple players are inside the app and vehicles need to spawn around all players.

Initialize asset using API

The above Traffic Components are not mandatory; traffic initialization can be exclusively handled using API methods. Here is an example:

 void Start()
     TrafficOptions options = new TrafficOptions()
         activeSquaresLevel = activeSquareLevels,
         disableWaypointsArea = disableWaypointsArea,
         distanceToRemove = distanceToRemove,
         greenLightTime = greenLightTime,
         initialDensity = initialActiveVehicles,
         lightsOn = lightsOn,
         minDistanceToAdd = minDistanceToAdd,
         useWaypointPriority = useWaypointPriority,
         yellowLightTime = yellowLightTime,

     API.Initialize(player, nrOfVehicles, vehiclePool, options);

The player, nrOfVehicles, and vehiclePool parameters are required, while the rest of the parameters are optional and can be set using a TrafficOptions object. For further info, refer to the Complete API

Video Tutorial

This tutorial was made for the old version of the plugin, but all information from it is still valid.

Last updated