Unity Traffic Simulation
Traffic Simulation is a Unity asset that lets you easily simulate simple traffic into your scene.
Developed / Tested with Unity 2018.3.x and plus.
How to use
Note
- If you have any problem with the traffic system, press the Re-Structure Traffic System button on the Traffic System GameObject to re-generate the traffic system.
- Make sure to be in Top Orthographic view, in the editor, when you create your waypoint system.
Create a Traffic System
- Create a Traffic System via Component -> Traffic Simulation -> Create Traffic Simulation
- Select the Traffic System object in the hierarchy
- Press Ctrl + Left Click to place the first waypoint of the segment (create a new segment)
- If you press Shift + Left Click it will continue the segment, otherwise step 3. to create a new segment. Usually a segment stops at an intersection. Note that the vehicle will follow the waypoints depending on the order you placed them.
- The next step is a bit tedious at the moment. Basically you have to link the segments between each other - so when the the segment ends at an intersection, you can go to a certain number of segments which will be picked up randomly by the VehicleAI. To do so, select a segment and plug your next segments in the Next Segments parameter.
- To create an intersection, press Alt + Left Click and select the intersection type (stops or traffic lights).
- Set the dimension of the intersection's box collider so it covers all the last waypoints (cf: below image). Also make sure that the height of the box collider is high enough to collide with your vehicle.
Editing Segments / Intersections
If you delete existing segments or intersections, you will have to press the Re-Structure Traffic System button in order to rebuild the system.
Stops
- Select STOP as the intersection type.
- If you have any, select which lanes have the priority (without stop). This means if you leave it empty every lanes will include a stop and work on a first in first out basis.
Traffic Lights
- Select TRAFFIC_LIGHT as intersection type.
- Configure the two light groups by placing the segment on the group they belong to. Usually, two opposite lane share the same light group. In the example below, you would have to place the two segments ending with the blue round in one group and the two segments with the gray round on the other group.
- Changing the light of your 3D model can be done via your own script. An example of this is provided in the example folder.
Vehicle Configuration
-
Setup your vehicle as follow (you can also refer to the vehicle in the example scene):
-> Main object with Rigidbody + BoxCollider
-> Object with vehicle's body mesh (without the wheels!)
-> Empty object for the 4 wheels
-> Object with WheelCollider (but no mesh)
-> Object with WheelCollider (but no mesh)
-> Object with WheelCollider (but no mesh)
-> Object with WheelCollider (but no mesh)
- Select the vehicle object main parent and click on Component -> Traffic Simulation -> Setup Vehicle. This will add the needed scripts, set the proper layers and tag as well as create a raycast anchor.
- Place the raycast anchor in the front of the vehicle's hood.
- Configure WheelDrive and VehicleAI with your wanted paramters (torque, maximum speed, etc.). Set the left and right wheel mesh - they will be instantiate in runtime where your wheelcolliders are located.
MIT License
Copyright (c) 2019 Matthieu Cherubini
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.