First AI approach for the traffic system

First AI approach for the traffic system

Unity3D Video Game Development MiniTrue

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:

AI

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” ;-)

AI

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.