Skip to content

Train 3D - Video Preview of Intersection Traffic Management

Developer Update: I've got some basic traffic management at intersections now. The vehicles had traffic management to space out and follow each other but would drive right through each other at intersections. I wanted to add some controls to make vehicles yield to others already in the intersections. This adds to the strategy of how you design and populate your roads. Make them too busy with too many intersections and your vehicles will be slowed.

There could be hundreds or thousands of intersections in the game so it didn't make sense to have a Unity game object for each one. Instead, I decided to use the game tiles in the map as a storage mechanism and have the vehicles sort things out for themselves. This has the benefit that intersections without vehicles do not take any processing time.

Vehicles check upon arrival at a new road tile to see if it is an intersection. If it is, they register themselves as arriving and their total wait time is set to zero. The game tile has a list of all registered vehicles. Because the vehicles are supposed to be spaced out and not run into each other, the max vehicles evaluated should not exceed the four compass directions + any vehicles moving.

Each frame, the vehicles have an internal state that knows if they are waiting at an intersection. If they are waiting, they ask the game tile if they can proceed. The game tile checks to see if any vehicles are currently in motion. If not, it looks at the array of vehicles waiting, pulls the one with the longest wait time, and marks that one as the id allowed to proceed. If the requesting vehicle's id matches the currently allowed one on the game tile, the vehicle puts itself in motion.

At the end of the move through the intersection, the vehicle calls back to the game tile to let it know it is done and unregisters it's vehicle id from the intersection.

There are a couple of extra situations to account for too. First, what happens if a vehicle is deleted while it is the active one or waiting in the intersection queue? Any vehicle that is sold/deleted unregisters itself as part of the cleanup. The final safety check/cheat is that vehicles never wait more than 5 seconds at an intersection. It's a hack that helps ensure you don't end up in traffic jams where every vehicle is waiting on another. It only really happens when you jam far too many vehicles on the road but it makes game play annoying if you have to manually cleanup a bunch of stuck vehicles. The 5 second rule is just long enough to still impact the revenue making abilities of the vehicles while balancing the fun of building your transportation empire.