Here’s a complete and detailed README for the Traffic Simulation Unity Project, written in markdown for clarity and structure.
This project is a Unity-based simulation of a traffic system featuring vehicles, traffic lights, sensors, and camera management. It demonstrates vehicle spawning, AI-driven movement, traffic light control, and real-time statistics in a virtual environment.
- Overview
- Project Structure
- Scripts Explanation
- Setup Instructions
- Usage
- Customization
- Known Issues
- Future Improvements
The Traffic Simulation Unity Project models traffic flow across multiple lanes and roads. Key features include:
- Vehicle Spawning: Vehicles spawn randomly on designated lanes.
- Vehicle AI: Uses Unity’s NavMesh for pathfinding and movement.
- Traffic Lights: Dynamic timing adjusts based on vehicle counts.
- Sensors: Detect vehicles to manage traffic flow and light timings.
- Camera Management: Switch between multiple camera views.
- UI Updates: Displays real-time vehicle counts and traffic light timers.
This simulation is ideal for testing traffic management systems, AI behavior, or educational purposes.
The project is organized around several C# scripts, each responsible for specific functionality:
-
Vehicle Spawning:
Lane1Spawner.cs
: Handles spawning on Lane 1.VehicleSpawnerLane2or4.cs
: Manages spawning on Lanes 2 and 4.
-
Vehicle AI:
CarAILane1.cs
,CarAILane2.cs
,CarAILane3.cs
: Lane-specific AI logic.CarAI.cs
: Generalized AI for other roads.
-
Traffic Lights:
Road1TrafficLight.cs
: Controls traffic light states and timers.TrafficSystemController.cs
: Synchronizes traffic lights across roads.
-
Sensors:
Sensor.cs
,Sensor2.cs
,Sensor3.cs
: Detect vehicles on Lanes 1, 2, and 3.SensorForOtherCars.cs
: Monitors vehicles on additional roads.
-
Camera and UI:
CameraAndPanelManager.cs
: Manages camera switching and UI panels.UpdateRoadStats.cs
: Updates UI with real-time data.
Below is a detailed breakdown of each script’s purpose and features:
- Purpose: Spawns vehicles on Lane 1 at random intervals.
- Key Features:
- Spawns 0–9 vehicles per batch.
- Delay between individual vehicle spawns: 6–15 seconds.
- Time between batches: 15–30 seconds.
- Purpose: Spawns vehicles on Lanes 2 and 4.
- Key Features:
- Spawns 0–5 vehicles per batch.
- Delay between vehicles: 8–15 seconds.
- Time between batches: 15–30 seconds.
- Purpose: Controls vehicle movement on specific lanes using Unity’s NavMesh.
- Key Features:
- Pathfinding to random or predefined destinations.
- Steering and physics managed via wheel colliders.
- Vehicles are destroyed upon reaching their destination.
- Purpose: Provides generalized AI for vehicles on other roads.
- Key Features:
- Assigns destinations based on road and lane configurations.
- Uses similar pathfinding and movement mechanics as lane-specific scripts.
- Purpose: Manages traffic light states (red, yellow, green) for Road 1.
- Key Features:
- Adjusts green light duration based on detected vehicle count.
- Includes timers for yellow and green phases.
- Purpose: Coordinates traffic lights across four roads.
- Key Features:
- Manages transitions between light states (e.g., yellow to green).
- Adjusts timing dynamically using sensor data.
- Purpose: Detects vehicles on Lanes 1, 2, and 3.
- Key Features:
- Halts vehicles at red or yellow lights.
- Counts vehicles to inform traffic light timing.
- Purpose: Detects vehicles on roads beyond Lanes 1–3.
- Key Features:
- Functions similarly to lane sensors but is tailored for broader road coverage.
- Purpose: Handles camera switching and UI panel management.
- Key Features:
- Uses keyboard inputs (0–4) to toggle between views and panels.
- Purpose: Updates the UI with real-time traffic data.
- Key Features:
- Displays current and previous vehicle counts per road.
- Shows traffic light timer statuses.
Follow these steps to set up the project in Unity:
-
Unity Version:
- Use Unity 2020.3 or later for compatibility.
-
Clone the Repository:
- Clone the project using Git:
git clone <repository-url>
- Clone the project using Git:
-
Open in Unity:
- Launch Unity Hub, select “Add,” and choose the cloned project folder.
-
Scene Setup:
- Verify that all prefabs (vehicles, traffic lights, sensors) are linked in the Unity Inspector.
- In
CameraAndPanelManager.cs
, assign the appropriate cameras and UI panels.
-
NavMesh Baking:
- Open the Navigation window (Window > AI > Navigation).
- Select the terrain or road objects and bake the NavMesh for pathfinding.
Here’s how to run and interact with the simulation:
-
Run the Simulation:
- Click the Play button in Unity.
- Vehicles will spawn automatically and move according to traffic light states.
-
Switch Cameras:
- Press keys 0–4 to switch between different camera perspectives and UI panels.
-
Monitor UI:
- Observe the UI for real-time updates on vehicle counts and traffic light timers per road.
You can tailor the simulation to your needs with these options:
-
Vehicle Prefabs:
- Add or replace vehicle models in the
vehicalsPrefabs
array in the spawner scripts.
- Add or replace vehicle models in the
-
Spawn Rates:
- Modify spawn delays and batch sizes in
Lane1Spawner.cs
andVehicleSpawnerLane2or4.cs
.
- Modify spawn delays and batch sizes in
-
Traffic Light Timers:
- Adjust timing logic in
Road1TrafficLight.cs
andTrafficSystemController.cs
.
- Adjust timing logic in
-
Destinations:
- Edit AI scripts (
CarAILaneX.cs
orCarAI.cs
) to set new or custom vehicle destinations.
- Edit AI scripts (
- Vehicle Collisions:
- Vehicles may overlap or collide unnaturally due to unoptimized physics settings.
- Timer Synchronization:
- Traffic light timers across roads may occasionally fall out of sync.
- UI Updates:
- Slight delays in UI updates may occur under heavy load.
Consider these enhancements for future development:
- Advanced Pathfinding:
- Add overtaking and lane-changing capabilities to vehicle AI.
- Pedestrian Simulation:
- Integrate pedestrians and crosswalks into the system.
- Emergency Vehicles:
- Implement priority logic for emergency vehicles (e.g., ambulances).
- Multi-Intersection Support:
- Expand the simulation to handle multiple interconnected intersections.
This README provides a comprehensive guide to understanding, setting up, and using the Traffic Simulation Unity Project. For additional details, check the inline comments within the scripts or consult Unity’s official documentation. Enjoy experimenting with the simulation!