First AI approach for the traffic system

With more than one vehicle on the road a little bit of artificial intelligence is needed. 

I decided to attach three Unity3D collider (with activated “trigger” property and one rigidbody component) to each vehicle:

  • An active front collider to detect if the vehicle is about to crash into another car or pedestrian
  • A passive body collider to be detected by the front collider of another vehicle
  • A passive back collider – also to be detected by another front collider

 

First idea of the collider and the AI behavior:

If the front collider of the vehicle hits a body collider it stops and tries to drive back a little bit. 

When the back collider is hit, the other car is probably just driving on the same track but with less speed. In this case the vehicle with the triggered front collider adjusts to the speed of the car in front of it.

The result looks more like a cartoon character using trial and error  than a defensive driving style. I call this AI behavior “Italian  driving mode” ;-)

A first animated video of some vehicles using the AI:

 

BTW: One thing I learned about Unity3Dwhile working on this: Because Unity seems to serialize enums as numbers and not by name: Use a flag number for every enum value. Otherwise when you change the order inside the enum definition the meaning swaps.