Status

Mostly complete, although not available publicly yet. When i get around to packaging this nicely I will release it.

System In Action

In the middle of creating this thing, I came across an interesting bug - so naturally I turned it into a little bit of a meme ;). Enjoy:

How does it work?

The whole system can be divided into a couple of elements:

  1. A graph of "development" or marker nodes
  2. A graph of "live" nodes
  3. A vehicle manager
  4. Some vehicles

Nodes

the marker nodes, are created in the editor, when the map loads, these are converted into live nodes. The reason they are two separate entities, is that there were some properties which were only appropriate for the editing phase and not needed during the game - and more importantly, I didn't want the nodes to be actual fully-fledged GameObjects, which they would have to be in order to be place-able in the Unity editor.

This way I can also have a singular node in the editor correspond not to just one "actual" node! A good example of this is the Turn Marker, it guides the cars around a curve.

Now, curves are tricky, I wanted this to be pretty flexible, and work with many different road shapes, so the most appropriate kind of curve was a Bezier. However calculating the position along the curve on the fly for each of the cars each frame would be quite a lot. I decided to create a single type of marker node which would be turned into multiple live nodes spread around the Bezier curve between the nodes directly near whenever the game started.

This approach turned out pretty well and simplified car turning a lot! It also let me introduce a "granularity" parameter which would decide how many nodes should be produced along a curve - letting you balance memory consumption and the smoothness of the turns.

Vehicles

The actual vehicle spawning is pretty simple. The nodes have characteristics which make them either "inflow" or "outflow" nodes. The inflow nodes for example are the ones which are not connected from the back or are connected from the back but to a tile which is not currently visible to the player. This way I can simply have a pool of vehicles and spawn them at inflow nodes whenever there is no possibility of collision with an existing vehicle (this is done on a timer, and with collision checks).

The actual movement of vehicles is just a straight forward interpolation between its current position and its current target (with collision checks). The vehicles are not aware of target nodes but are assigned a target position by the vehicle manager and the nodes themselves have methods which return the next successor node (which might be random).

Performance/Possible Improvements

So this works very well, however the actual car turning is not perfect and looks choppy. One way this could be improved is by interpolating the cars angle as well and not just turning it to face the direction.

Another thing would be to make the nodes convert to live nodes sometime before the game is even launched - Serialising them and just loading at runtime would be much better for load times.

Overall, the system is very close to being finished. With a little polishing it could be integrated into a game pretty quickly.