Archives for : Programmer Lab

CC: Traffic Simulation

Hello world!
Today its time to dig down into the backend of how we simulate traffic in Station 5.

Our traffic simulation is still in its early stages and needs a lot of work, but the basics are down and vehicles can navigate the network of roads without mayor problems. But why even make a traffic AI from scratch? Unity’s asset store already got multiple ready made traffic AI systems. iTS Traffic System, TI Traffic AI, A.I Car Traffic PACK, just to name a few.

The problem with all of these solutions, is that none of them is very versatile or adaptable. Dealing with traffic AI in an chaotic open world game, control over the code is key. Adapting any of the above solutions, to work with physics and collisions, proves to be a much more complex undertaking, than just rewriting it all from scratch. Not saying that all of the above solutions are bad, but they are just not what we needed.

So how does it actually work?
First of all we need to look at the criteria’s for how we want the AI vehicles to behave

  • Be able to accelerate and decelerate and semi realistic rates
  • Wheels suspension to make it look like a real vehicle, and not a block translating.
  • Navigate the road network
  • Stop for red lights, players, road blocks and other AI vehicles
  • Spawn and despawn when inside or outside the players viewport.

The first 2 criteria’s hints that we need a way to simulate wheels on the vehicle. Fortunately Unity already got exactly what we need, “WheelColliders”! WheelColliders gives us the ability to add individual suspension to all 4 wheels, accelerate and decelerate using torque directly on the wheels and gives the vehicle a way to steer.

Setting the vehicle velocity. After November 2016, all roads in Seattle downtown will have a speed limit of 25mph, so our roads use the same limit to cap the speed of AI vehicles. To make the vehicles seems less “robot like”, they all have a random value that determines whether the AI will drive slightly above or below 25 mph.

Vehicles accelerate to a desired speed using a simple formular

(desiredSpeed - currentSpeed) * Acceleration Sensitivity

Where the desiredSpeed is most often the speed limit of 25mph, currentSpeed is the vehicles current speed in mph and the Acceleration Sensitivity is a value between 0-1 that decides how quickly the car accelerates, to allow for different kind of vehicles.

When a vehicle has to deceleration due to a redlight or a car braking in front, the vehicle calculates a deceleration value based on attaining the desiredSpeed in 1 second.

(V2 - V02) / 2(X - X0)
desiredSpeed = (targetVelocity - currentVelocity) / 2 * (Vector3.Distance(target.position, transform.position));


Detecting Collision. Every vehicle casts a SphereCollider in front of it, to check if there is a chance of a collision with the vehicle in front or if the player suddenly walked in front of the vehicle. If the vehicle in front gets within a specified range, the vehicle adjust its speed to match or be slightly below. If another car or vehicle gets to close, the vehicle “blocks” the brake and engages the handbrake, effectively applying the maximal amount of deccaleretion within the laws of physics


End of the road. When a vehicle hits the end of a straight road or an intersection it has to decide on where to go next. Currently to simplify things we only have 3 possible situations, based on the vehicles current lane.

Far-left lane Left or straight
Far-right lane Right or straight
Any center lane Straight


If the intersection has a traffic light the vehicle will wait for green, before choosing a new direction. U-turns are not a thing as it doesn’t look good and have a bad habit of creating collisions.

That’s all for today, if you have any questions you are welcome to post them in the comments below or on our Facebook or Twitter