What We Did
· Lane Commitment Logic:
· We introduced a system where, once an AI car selects an open lane, it “commits” to that lane for a minimum amount of time (or until it becomes blocked), instead of constantly re-evaluating and switching lanes every frame.
· Safe Lane Selection:
· We added logic to ensure the AI only commits to a lane if it is both open and safe (not too close to the road edge or hazards like telephone poles, hydrants, or parked cars).
· This uses a buffer check and a dedicated hazard layer mask.
· Bounds Checking:
· We added checks to prevent the AI from trying to steer to an invalid lane index, which prevents errors and ensures the car always has a valid target.
---
Why We Did It
· Reduce Erratic Steering:
Previously, the AI would constantly try to return to the center or switch lanes too frequently, causing unnatural “back and forth” steering, especially when open lanes were available.
· Prevent Collisions with Roadside Hazards:
Without awareness of the road edge, AI cars could select far left or right lanes and hit objects on the sidewalk or curb.
· Increase Realism and Smoothness:
Real drivers (and race cars) commit to a lane and only change when necessary, especially when a lane is clear and safe.
___________________________________________________________________________________________________________________________________________________________________________________________________________
How It Works
· Lane Commitment:
· When the AI finds an open and safe lane, it sets a timer and “commits” to that lane.
· It will stay in that lane for a set duration, unless the lane becomes blocked.
· Only after the timer expires or the lane is blocked will the AI reconsider changing lanes.
· Safe Lane Check:
· Before committing to a lane (especially the outer lanes), the AI checks for hazards using raycasts or spherecasts to the side and ahead.
· Only lanes that are both open (no traffic) and safe (no hazards) are considered.
· Bounds Checking:
· The AI always checks that the lane index is valid before steering, preventing crashes and fallback to a safe lane if needed.
---
Benefits
· Smoother, More Realistic Driving:
AI cars now drive in a chosen lane for a realistic amount of time, just like human drivers, instead of jittering between lanes.
· Fewer Collisions:
By avoiding unsafe lanes near the road edge, AI cars are much less likely to hit telephone poles, hydrants, or parked cars.
· Better Traffic Flow:
AI cars make smarter, less frequent lane changes, leading to more believable and efficient traffic movement.
· Easier Tuning and Maintenance:
The code is modular, with clear helper methods and safety checks, making it easier to adjust and extend in the future.
---
In summary:
These changes make your AI cars drive more like skilled, cautious human drivers—committing to open lanes, avoiding hazards, and steering smoothly—resulting in a more fun, realistic, and robust city racing experience.
What We Changed
· Persistent Learning Data Structure:
· Added serializable classes (LearningData, VehicleLearningData, PassData) to store AI learning data (track, vehicle, passes, etc.).
· Save/Load Logic:
· Implemented methods to save learning data to disk after a race and load it at startup if enabled.
· Inspector Checkbox:
· Added a checkbox in the Unity Inspector to let users enable or disable saving learning data.
· Naming Conflict Fix:
· Renamed the serializable learning data field to avoid a naming conflict with the in-memory learning dictionary.
---
Why This Is Good
· AI Progression:
The AI now remembers what it learned from previous races, so it gets better over time instead of starting from scratch each session.
· User Control:
The checkbox gives users the choice to save or not, respecting privacy and disk usage.
· Performance:
Data is only saved after the race, not during, so there’s no performance hit while racing.
· Maintainability:
Clear separation between runtime learning (in-memory) and persistent storage (on disk) makes the code easier to understand and extend.
---
What’s Different Now?
· Before:
The AI would forget all learning after each session—no persistent improvement.
· Now:
The AI can load previous learning data and continue improving, making it more realistic and competitive over time. Users can opt in or out of this feature.

