Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need advice on designing a traffic system

Discussion in 'Game Design' started by FredrikSalomon, Nov 17, 2015.

  1. FredrikSalomon

    FredrikSalomon

    Joined:
    Oct 8, 2015
    Posts:
    7
    Hi guys,

    I'm working on simple city-builder/transport type of game and started working on implementing traffic. I have made a build system where the player can drag and lay down road (non-tileable) and the road pieces can change shape to turns, T-intersection etc. to make realistic roads. Now I have googled a lot on traffic simulation and looked at different assets here in the Unity store but haven't really found the right solution for me.

    I have tried a few design solutions by adding fixed points to each road pieace and storing them in an array of vectors to use for traffic movement. However, I always end up with the same problem: How to tell the system what is right and left? I have tried to solve it by implementing a compass based system to keep track of road pieces direction but generally feel a bit lost.

    I would like to have some advice on how I should attack the traffic system. There is something I'm missing. Ideally I want to build it from scratch because the system should be fairly easy and non-physic (at least for now) and also the road must be dynamic and changeable upon user input.

    Any advice would be greatly helpful!

    Regards
    Fredrik
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I would structure this by setting up a system where each piece of road has a reference to each of its neighbours. Then you can just use straight forward graph searches to identify appropriate paths through the net work.
     
  3. FredrikSalomon

    FredrikSalomon

    Joined:
    Oct 8, 2015
    Posts:
    7
    I have done a neighbouring connecting system so I'm somewhat on the right track. Could you please elaborate or give some link on "straight forward graph search"? I could not really find the right thing on google.

    Also how would I go about creating paths for my vehicles? They will have a destination and I should be able to setup an A* solution for the pathfinding but how do I go about getting the vehicles to stay on the right side of the road and at the same time following the path going through turns and intersections?
     
    Last edited: Nov 18, 2015
  4. Gigiwoo

    Gigiwoo

    Joined:
    Mar 16, 2011
    Posts:
    2,981
    Have you investigated A* pathfinding? Apologies that my answer takes us down the implementation path. Perhaps this thread would be best served in another forum area.

    Gigi
     
  5. FredrikSalomon

    FredrikSalomon

    Joined:
    Oct 8, 2015
    Posts:
    7
    Hi,

    Well, I am using A* on another project so I'm familiar with that. The problem is how to build paths from the road nodes. I don't know exactly how to explain but each road piece has 2 nodes for each traffic direction. Connecting the road pieces is easy but to build an actual path with the nodes on correct side of the road seems to be the difficult part. This is where I'm stuck.

    /Fredrik
     
  6. goonter

    goonter

    Joined:
    Aug 31, 2015
    Posts:
    89
  7. FredrikSalomon

    FredrikSalomon

    Joined:
    Oct 8, 2015
    Posts:
    7
    Thank you but the problem is not really pathfinding or A*. It is more how to get right and left lanes. Look at the screen attached, the bus can find it's path, but how to make it understand to drive on the right side of the road?

    How should I think here? Compass heading?

    I know we are moving into more implementation phase now so please move thread if needed.
     

    Attached Files:

  8. goonter

    goonter

    Joined:
    Aug 31, 2015
    Posts:
    89
    There may be other ways to do it, but what I'd do is have each road section include a forward and a backward lane, so 2 paths per road rather than one. And to drive on the correct side, you could ensure that the AI always spawns pointing forward in the forward lane, and can never travel backwards. They'd need to go to an intersection that includes a u-turn path to turn around.
     
  9. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    @BoredMormon is right this is really a graph problem. It might seem intimidating at first but the logic is really fairly simple. Unfortunately there really aren't any good, maintained A*/Dijkstra implementations in C# that are open source that I've been able to find. But there is a ton of snippets and abandoned code, more then enough to piece together an implementation that works.

    This page probably has more information on the subject then any other place I know of, good place to start learning.
     
    FredrikSalomon likes this.
  10. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    I've done a traffic system a few years ago that was physics based, but I connected all the routes manually. I would recommend first doing it by hand, just manually link the path on your roads and get a working pathfinding system going based on each point having a list of possible options at each intersection. When you get that working right, start trying to get it setup to allow you to spawn and dynamically connect all possible directions between intersections as you add them. I hope that makes sense. Good luck!
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Side of the road is easy. Just figure out the travel direction. Then calculate the cross product between the travel direction and up. Then offset the vehicle in the resulting direction.

    Psudeo code:

    Code (CSharp):
    1. Vector3 nextPoint;
    2. Vector3 currentPoint;
    3.  
    4. Vector3 direction = nextPoint - currentPoint;
    5. Vector3 offsetDirection = Vector3.Cross(direction, Vector3.up);
    6.  
    7. transfom.position = currentPoint + offsetDirection * 0.5f;
     
    FredrikSalomon likes this.
  12. FredrikSalomon

    FredrikSalomon

    Joined:
    Oct 8, 2015
    Posts:
    7
    Thanks for your help.

    I have implemented an offset system where the vehicle is offset to the right of the node which is determined by the traveling direction. In the future I would like to implement a lane system with each road piece having a "right" and "left" node, and as you mentioned intersections with different traveling choices. For now this system will work. Will work on it later in the project. Thanks again!
     
    Kiwasi likes this.
  13. ImmortalPancake

    ImmortalPancake

    Joined:
    Dec 5, 2012
    Posts:
    18
    What is the system you are using to make your roads? I am looking for something simple I can use for a prototype for a class project but cant find anything that fits my needs.
     
  14. FredrikSalomon

    FredrikSalomon

    Joined:
    Oct 8, 2015
    Posts:
    7
    What do you mean by system? Build or pathfinding system?
     
  15. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    @ImmortalPancake there are assets out there (easy roads comes to mind) that allow quick prototyping and road design. It might be worth it to purchase a copy to allow you to quickly get a road network into your project. There are others out there but check the free version of easyroads out first:

    https://www.assetstore.unity3d.com/en/#!/content/987