A Unity solution for simulating basic traffic behavior using waypoints. This system is designed for simplicity, making it ideal for quick prototypes or basic traffic implementations.
- Waypoint-Based Movement: Vehicles move along predefined paths using waypoints.
- Dynamic Route Selection: Vehicles randomly choose paths at intersections.
- Collision Avoidance: Vehicles detect and stop for others using raycasting.
- Customizable Speed and Behavior: Configure vehicle speed, detection range, and stopping thresholds.
- Visual Debugging: Waypoint connections are displayed in the scene view with Gizmos and labels for easy setup.
- Download the
TrafficSystemController.unitypackage
file from this repository. - Open your Unity project and go to Assets > Import Package > Custom Package.
- Select the
TrafficSystemController.unitypackage
file and click Import.
-
Setup Waypoints:
- Add
WaypointController
to GameObjects in your scene. - Link waypoints by assigning
nextWaypoints
in the Inspector to define paths.
- Add
-
Add Vehicles:
- Attach the
VehicleController
script to your vehicle prefabs. - Assign the first
WaypointController
to thecurrentWaypoint
property in the Inspector. - Specify the tag for vehicles (e.g.,
Car
) in thevehicleTag
field to enable collision detection.
- Attach the
-
Customize Behavior:
- Adjust vehicle speed, raycast distance, and stop time thresholds in the Inspector.
- Set
nextWaypoints
for waypoints to control traffic flow.
-
Test Your Setup:
- Play the scene and observe vehicles navigating the waypoints.
- Use Gizmos in the Scene view to visualize waypoint connections and debug traffic paths.
- Waypoints are connected by green lines.
- Blue spheres indicate the location of each waypoint.
- Arrowheads show the direction of the paths, making it easier to identify connections in the Scene view.
- Place waypoints in a road network to define vehicle paths.
- Add vehicles and configure their initial waypoints and movement parameters.
- Vehicles will follow the waypoints, handle intersections dynamically, and stop for other vehicles when needed.
- Designed for simple traffic systems. More advanced behaviors (e.g., traffic lights) are not included.
- Ensure waypoints are properly connected; disconnected waypoints will halt vehicle movement.
This project is licensed under the MIT License. See the LICENSE file for details.