Add Random Traffic
To add a Random Traffic to your scene you need the Random Traffic Simulator Script.
Create a new Game Object as a child of
Environmentand call it
Click a button
Add Componentin the
Inspectorto add a script.
A small window should pop-up. Search for
RandomTrafficSimulatorscript and add it by double clicking it or by pressing enter.
After clicking on the newly created
RandomTrafficSimulator object in the Scene tree you should see something like this in the
Random Traffic Simulator, as the name suggests, generates traffic based on random numbers. To replicate situations you can set a specific seed.
You can also set
Vehicle Layer Mask and
Ground Layer Mask.
It is important to set these layers correctly, as they are a base for vehicle physics.
If set incorrectly the vehicles may fall through the ground into the infinity.
Random Traffic Simulator Script moves, spawns and despawns vehicles based on the configuration. These settings can to be adjusted to your preference.
Setting Max Vehicle Count.
This parameter sets a limit on how many vehicles can be added to the scene at one time.
These are models of vehicles that should be spawned on the scene, to add NPC Prefabs please follow these steps:
To do this click on the "+" sign and in the new list element at the bottom and click on the small icon on the right to select a prefab.
Change to the
Assetstab in the small windows that popped-up.
Search for the Vehicle prefab you want to add, e.g.
Available NPC prefabs are shown in the NPC Vehicle section.
Control NPC Vehicle spawning
Random Traffic Simulator Script will on random select one prefab from
Npc Prefabslist every time when there are not enough vehicles on the scene (the number of vehicles on the scene is smaller than the number specified in the
Max Vehicle Countfield).
You can control the odds of selecting one vehicle prefab over another by adding more than one instance of the same prefab to this list.
Add spawnable lanes
Spawnable lanes are the lanes on which new vehicles can be spawned by the Random Traffic Simulator Script. Best practice is to use beginnings of the lanes on the edges of the map as spawnable lanes.
Make sure you have a lanelet added into your scene. The full tutorial on this topic can be found here.
Adding spawnable lanes is similar to Adding NPC Prefabs.
Add an element to the
Spawnable Laneslist by clicking on the "+" symbol or by selecting number of lanes directly.
Now you can click on the small icon on the right of the list element and select a Traffic Lane you are interested in.
Unfortunately all Traffic Lanes have the same names so it can be difficult to know which one to use. Alternatively you can do the following to add a traffic lane by visually selecting it in the editor:
RandomTrafficSimulatorin the Inspector view.
Select the Traffic Lane you are interested in on the Scene and as it gets highlighted in the Hierarchy view you can now drag and drop this Traffic Lane into the appropriate list element.
The last thing to configure is the behavior of NPCVehicles. You can specify acceleration rate of vehicles and three values of deceleration.
This value is used for every acceleration the vehicle performs (after stop line or traffic lights).
This deceleration value is used for most casual traffic situations like slowing down before stop line.
This deceleration rate is used for emergency situations - when using standard deceleration rate is not enough to prevent some accident from happening (e.g. vehicle on the intersection didn't give way when it was supposed to).
This deceleration rate is a last resort for preventing a crash from happening. When no other deceleration is enough to prevent an accident this value is used. This should be set to the highest value achievable by a vehicle.
This configuration is common for all vehicles managed by the
Random Traffic Simulator Script.
The last thing that needs to be done for
RandomTraffic to work properly is to add intersections with traffic lights and configure their sequences. Details here.