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.
After creating the road system, now the traffic system has to be programmed.
First conceptual drawings for the traffic system:
The goal was to create a self-connecting traffic network, which should work for left- and right-hand traffic.
Everything went fine until the cars had to drive around corners.
But a little bit later (and with some vector calculation) this also works very well.
A view of the transport system in the editor. The red squares and red lines represent the center lines of the street.
A first animated video
When creating the city map system for the new game I decided to use a block size of 10 meters x 10 meters.
First conceptual drawings for the road system:
I have set The level of detail of the pedestrian-map to 50 cm x 50 cm. Both together should allow a fairly high degree of flexibility when auto-generating the maps without too much memory or processing power needed.
For tests of the pedestrian system a simple road forming a closed loop should be optimal:
For debugging purposes it is useful to draw logical information (e.g. places a pedestrian is allowed to walk) on the screen. In Unity3d this is very simple to handle using the OnDrawGizmos functionality.
On the green areas, the pedestrian are allowed to walk:
The next step will be to design the traffic system.
It's been three years since I developed my last game Pumpkin Jumpin. Time to start with a new one :-)
It will be my first deeper experience with Unity3D and also my first game with auto-creating maps. So playing the game may surprise myself because I cannot know the automatically created maps before playing them myself.
The game will take place in a sort of city - so my first steps will be:
- Creating a level map system and pedestrian system
- Creating a traffic system for cars