Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Simple Traffic System

Discussion in 'Assets and Asset Store' started by Stephen_O, Dec 15, 2019.

  1. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505

    The AITrafficCar script has a function MoveCarToPool()

    You can write a script with the logic check you desire, and if conditions are met call it.

    Code (CSharp):
    1. using TurnTheGameOn.SimpleTrafficSystem;
    2. using UnityEngine;
    3.  
    4. public class MoveCarToPool : MonoBehaviour
    5. {
    6.     // Attach to AITrafficCar
    7.     // Returns car to pool when condition is met
    8.     AITrafficCar _AITrafficCar;
    9.  
    10.     void Start()
    11.     {
    12.         _AITrafficCar = GetComponent<AITrafficCar>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if (true)
    18.         {
    19.             _AITrafficCar.MoveCarToPool();
    20.         }
    21.     }
    22. }
     
  2. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    87
    Ahhh! I guess I did disable the mesh renderers on them! Brilliant! That does it!
     
    Stephen_O likes this.
  3. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.8 will be ready soon. It will have a Router Editor mode added to STS tools, this will make manually selecting and editing waypoints unnecessary.

    I've had a couple reviewers mention that it's not possible to remove route connections or lane change points, this is not true, these settings are stored on the AITrafficWaypoint script and can be removed directly from the waypoint inspector. However, I do think there is something I can do to make the workflow easier, more-clear, and hopefully avoid people reaching this conclusion. This was the primary goal with the Route Editor.

     
    hunter-baun likes this.
  4. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    511
    How would I go about using this with something like world streamer where it doesnt load the whole world?
     
  5. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    I'm not sure, I've never used world streamer so it's hard to say.

    STS cars are basically game objects that move toward waypoints. I think that the developer of World Streamer would have more insight as what needs to be done if his asset requires a unique workflow for content creation.
     
  6. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    511
    So a lot of the system are grouped up. How would I go about not having any of the waypoints and other objects children? I have another route I could go if they have to be patented but if say the center point (0,0,0) and the parent objects are moved to the new center point. Would that cause an issue?
     
  7. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    It's hard to tell without being able to test, that's not something I've planned for when writing the system. I don't have an easy way to produce that scenario. My advice would be to try it and see what happens.
     
  8. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    511
    That's what I was about to do.was just looking for a best approach
     
  9. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.8 has been submitted.

    - Added 'Route Editor' tool to STS Tools Window, this allows you to view/edit all waypoints from the selected route instead of manually selecting each waypoint game object from the hierarchy.
    - Added "Go To Start On Stop" bool to AITraffic Car, this respawns the car to the first route point on it's spawn route when the car comes to a stop.
    - Added EngineSound example script; attach it to a car prefab and add an audio clip for engine sound.
    - Added ReturnToPoolOnTriggerEnter script, attach it to a cube and use as a trigger area to return cars to the pooling system.
    - Added FakeWheel utility script. A simple fake wheel implementation, useful for vehicles that have more than 4 wheels.
    - Moved AITrafficCarRandomMaterial script to Utility folder.
    - Moved AITrafficCarRandomSpeed script to Utility folder.
    - Renamed AITrafficCarRandomMaterial script to RandomMaterial.
    - Renamed AITrafficCarRandomSpeed script to RandomSpeed.
    - Changed AITrafficController.carAIList from private to (public get / private set) to allow custom scripting access.
    - Improved STS Tools help box messaging and info layout.
    - Improved STS Tools window layout, moved Handle Customization options to debug section.
    - Improved STS Tools window layout, moved Load/Reload button, unload button and show debug toggle to the same line.
    - Improved STS Tools Route Connector, scene view F handle is not drawn if a from point is already selected.
    - Improved STS Tools Route Connector, scene view T handle is not drawn if a to point is already selected.
    - Improved STS Tools Route Connector, scene view F handle is not drawn if point is already selected as a to point.
    - Improved STS Tools Route Connector, scene view T handle is not drawn if point is already selected as a from point.
    - Improved STS Tools SignalConnector, scene view L handle is not drawn if light is already selected.
    - Improved STS Tools SignalConnector, scene view R handle is not drawn if route is already selected.

     
    hunter-baun likes this.
  10. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.9 has some new changes ready, this will likely be submitted after 1.0.8 is approved.

    - Removed Cube mesh from waypoints, replaced with gizmo.
    - Added route density limit, used by pooling system. Cars will not spawn on route if density limit has been reached. This prevents the pooling system from over-spawning cars into a concentrated area if the route is not visible.

    Over-spawning on not visible routes was noticeable in the pooling demos, previously when the camera moved to the edge of the map, the not visible street around the corner would have cars spawn bumper to bumper until the route was in camera view.

    The demos have been updated to match this version, pooling now produces a much better result:
    Windows PC Demo
    WebGL Demo
     
    hunter-baun and Freznosis like this.
  11. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    1.0.8 is now available.
     
    hunter-baun likes this.
  12. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.9 submitted.

    - Removed Cube mesh from waypoints, replaced with gizmo.
    - Added method definitions to AITrafficCar script.
    - Added route density limit, used by pooling system. Cars will not spawn on route if density limit has been reached. This prevents the pooling system from over-spawning cars into a concentrated area.
     
    hunter-baun likes this.
  13. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.9 is now available.
     
    Freznosis, Edd2000uk and hunter-baun like this.
  14. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Something new for version 1.0.10

    I've added a delete waypoint function to the Route Editor tool. This will also remove new route point and lane change points from waypoints that were connected to the deleted waypoint. Full support of undo/redo.

    DeleteWaypoints.gif
     
    Freznosis likes this.
  15. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.10 submitted.

    • Added delete waypoint button to Route Editor tool, this will also remove new route point and lane change connections from waypoints that connected to the deleted waypoint.
    • Select button on Route Editor tool no longer performs frame select.
    • Added Frame button to Route Editor tool to frame select the waypoint object.
    • Added RemoveMissingLaneChangePoints function to AITrafficWaypoint.
    • Added RemoveMissingNewRoutePoints function to AITrafficWaypoint.
    • Fix STS Tools window throws errors if not unloaded before closing Unity.
    • Reduced project requirements.
    • Reduced number of demo scenes to 2, default and pooling.
     
    Freznosis likes this.
  16. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.10 is now available.
     
    Freznosis likes this.
  17. marcell123455

    marcell123455

    Joined:
    Jun 18, 2014
    Posts:
    270
    I just rewatched the tutorial videos of the simple traffic system because I plan to use it soon and asked myself if there is no other option to create spawn points for the traffic cars... I mean let's say I have a lot of roads. Do I really need to set every spawn point manually? If yes, do you think you could add a small gui button to "set every x waypoint of selected route to spawnpoint"?
    What do you think?
     
    Stephen_O likes this.
  18. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    That's a good idea. I think it crossed my mind at some point to create a button to randomly setup spawn points on all routes, but no one asked for it and was not a priority then. At this point most of the features are complete and I'm working on polish to improve user experience.

    I'll add that button to version 1.0.11, if you start your work before then email me for an update.
     
    marcell123455 likes this.
  19. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.11 will be submitted soon, here's a couple new tutorials.

    10. Deleting Waypoints


    11. Deleting and Setting Up Random Spawn Points
     
    Bartolomeus755 likes this.
  20. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.11 submitted.

    • Added "Setup Random Spawn Points" button to Spawn Points window.
    • Added "Remove All Spawn Points" button to Spawn Points window.
    • Added "Setup Random Spawn Points" button to AITrafficWaypointRoute inspector.
    • Added "Remove All Spawn Points" button to AITrafficWaypointRoute inspector.
    • Added MoveAllCarsToPool method to AITrafficController.
     
  21. AngeliqueSWOV

    AngeliqueSWOV

    Joined:
    Apr 16, 2020
    Posts:
    1
    Dear Stephen, I just downloaded your asset. I got the demo scene playing in game mode well. Yet, I can't add any waypoints by holding down the shift button while left clicking on a collider in the scene. I have set the API Compatibility Level to .Net 4.x and I've installed the package Burst 1.2.3. I am using Unity version 2019.3.12f1. Do you know what I can do to make sure I can add waypoints?
     
  22. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Hello,

    You need have the AITrafficWaypointRoute selected, and the surface you want to spawn a waypoint on requires a collider (it sounds like it does); that's about all.

    I did a test in 2019.3.13 - In an empty scene I added a plane, then added AITrafficWaypointRoute, selected it, then shift click to add waypoints on plane and it's working.
     
    Last edited: May 23, 2020
  23. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.11 is now available.
     
    Freznosis likes this.
  24. AngeliqueTinga

    AngeliqueTinga

    Joined:
    May 25, 2020
    Posts:
    1
    Dear Stephen,

    Thank you for your reply. Apparently it had something to do with the current project in which I imported your asset. When I import your asset in a new project it works well. I remade the project in which it didn't work and now it works in that project as well :) Thank you also for the clear instruction videos; they are really helpful.

    Best,

    Angelique
     
    Stephen_O likes this.
  25. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Great, I'm happy to hear you're up and running :)
     
  26. combatsheep

    combatsheep

    Joined:
    Jan 30, 2015
    Posts:
    129
    Hi.

    I imported STS to my project and just set up the simple road with Spline Route Creator.
    But 2 errors occurred when I played on the unity editor.
    ---
    NullReferenceException: Object reference not set to an instance of an object
    TurnTheGameOn.SimpleTrafficSystem.AITrafficWaypointRoute.Start () (at Assets/TurnTheGameOn/SimpleTrafficSystem/Scripts/AITrafficWaypointRoute.cs:39)
    ---
    When I double click the error, "AITraficWaypointRoute" object focus up in the hierarchy.

    How can I fix it?
    Thanks. STS_Road.jpg STS_Hierarchy.jpg STS_Inspector.jpg

    Env:
    STS ver 1.0.11
    Unity2019.2.10f1
    macOS 10.14.6
    Burst 1.2.3
     
  27. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Hello, it looks like you're missing the AITrafficController in your scene. Add it by selecting the Spawn AI Traffic Controller button from the STS Tools window. This is responsible for controlling the cars.

    Here's an example:

     
    combatsheep likes this.
  28. combatsheep

    combatsheep

    Joined:
    Jan 30, 2015
    Posts:
    129
    Hi, Stephen_0.

    Thanks for your response quickly!:)
    I made it and cars ran!

    Finally, I purchased STS;)(I have asked some questions by E-mail before purchasing it)

    Regards,
     
    Stephen_O likes this.
  29. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Thanks for purchasing, and I'm glad you got it to work :)
     
  30. amynox

    amynox

    Joined:
    Oct 23, 2016
    Posts:
    166
    Hi,
    Your asset looks great. Before purchasing i want to know:
    1/ Can i use it for exemple to tell a specific NPC (in a car or bike) to go from a specific point A to point B (along the "fake" traffic") or the asset can only handel "dumb" simulation ?
    2/ The NPC Car will use your system to drive arround the traffic, and A* path when its off road (let say point B is in off-road position that are not covered by your waypoint) is this possible ?
    2/ Didn't see any documentation about the API ?

    Thanks
     
  31. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Hello, this asset does not support bikes or path finding to specific points. All cars choose paths randomly when multiple paths are available. I've been working on exposing scripting API for the included systems, but it's not documented yet.
     
  32. combatsheep

    combatsheep

    Joined:
    Jan 30, 2015
    Posts:
    129
    Hi.

    Are there any way to cut the connection of the WaypointRoute?
    I want to delete the broken WaypointRoute.
    But When I delete the broken WaypointRoute, the next normal WaypointRoute is broken.

    Thanks.

    Env:
    STS ver 1.0.11
    Unity2019.2.10f1
    macOS 10.14.6
    Burst 1.2.3

    STS_wannaCutConnection.jpg

    Sorry, this problem is solved.
    When I delete the connected waypoint by RouteEditor, the connection is removed.;)
     
    Last edited: May 30, 2020
    Stephen_O likes this.
  33. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,050
    Very comprehensive looking system indeed, could you show the profiler to give an indication of the CPU time it takes to control say 100 cars moving around a world?
     
  34. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    PC Specs:
    - CPU: i7 6800K 3.4 GHz
    - GPU: GTX 1080

    This is the default demo scene profiled in editor, there are 358 active cars driving around, pooling is disabled.

    Profiler.jpg

    PC Demo is here, built using IL2CPP the stats are from Advanced FPS Counter doesn't drop below 400 FPS and CPU processing is not above 2.4 MS.

    The PC Pooling Demo with a density limit of 200 does not drop below 850 fps and CPU processing is not above 1.3 MS.
     
    combatsheep likes this.
  35. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,050
    Thanks for that, so 5.23ms for 358 cars plus 5.6ms for the physics to move those cars, so all in just under 11ms for 358 cars.
     
  36. amynox

    amynox

    Joined:
    Oct 23, 2016
    Posts:
    166
    Sorry to insiste... I understand for path finding ... i think i can implement it easily ... but how about : Can i use it for exemple to tell a specific NPC (in a car) to go from a specific point A to point B (along the "fake" traffic") ?
     
  37. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    You can create unique routes where a car can travel from point A to B following the waypoints of a route, as shown in tutorial 1.

    However, It's not yet possible to tell a car that's on one route to go to a specific point of another route in a network of interconnected routes. This particular feature has been requested a few times and may be developed for a future release.
     
    combatsheep likes this.
  38. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Personally, while I find the profiler insightful and useful, I don't use it as a benchmark to gauge CPU processing times. Since it's running in the editor, it inherits the editor overhead, plus it's own overhead which causes process time to take a bit longer.

    Since compiled builds run faster than editor with no overhead, I find build tests to be more accurate of the expected result. The 11 MS reported by the profiler is many times higher than the 2.4 MS process time in the standalone build; even the 6.5 MS reported by the stats window is almost 3x higher than the builds 2.4 MS.
     
    combatsheep likes this.
  39. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.12 submitted.

    • Improved Spline Route Creator, start control point is better positioned to improve usability.
    • Improved Spline Route Creator, end control point is better positioned to improve usability.

    Previously these points were always placed with the same offset from the first/last points and often getting in the way of the route path.
     
    combatsheep likes this.
  40. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.12 is now available.
     
  41. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,050
    Another question, in your videos all the junctions are controlled by lights, does your system support junctions and turnings with no light control, and if so how well does the traffic avoid one another in those cases?
     
  42. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    You can create junctions anywhere, though the sensors are not powerful enough to allow the cars to navigate around each other on their own. The purpose of sensors is to allow the cars to stop for obstacles, stop for other cars, and change lanes.

    I'm working on a new feature called 'yield triggers', they are box colliders with is trigger enabled that gets assigned to waypoint routes and placed near intersections. These will provide trigger area is occupied info for cross traffic areas and can be assigned to individual waypoints; so any waypoint will be able to have a way to determine of the intersection is clear, and stop the car to allow yielding for cross traffic if it's not clear.
     
  43. BeardedGiant

    BeardedGiant

    Joined:
    Apr 15, 2019
    Posts:
    1
    Is there a way to DeRegister a car from the AITrafficController? I'm working on a game where the player can get into car's and take control of them (think GTA) and although I call the stop driving method I notice that the car's transform is a bit jaggy when movement as if the caching of the car's transform is trying to catch up with it.

    I tested the same thing with the same car not registered to AITrafficController and that problem doesn't exist. So far, if you want to take control of a car from STS, you have to do the following:
    • call StopDriving on the AITrafficCar component
    • set the car's rigidbody to non kinematic (basically set it to false)
    • duplicate the wheel meshes of the car and hide the meshrenderer for the original wheels as they remained in place, their position updated via the cachedtransform by the AITrafficController
    Edit: By de-registered from the AICarTrafficController I mean the AITrafficCar Script was removed and my custom driving component was added with the prefab having the same structure and childed objects.

    And as a separate thing for people like me:
    In your early tutorial video on how to create a car for the system you've changed the meshFilter's mesh and material to the one you wanted and mentioned you can also put the model as a child game object and keep the parent without a meshfilter/renderer (and mentioned that the more transforms you have, the less optimized it will be). I had to edit the AITrafficCar and Editor_AITrafficCar to add support for an external meshRenderer field. Some of us are using meshes purchased from other artists that, sadly, are rotated so in my case - the car will drive rear first with the driver looking out the back. The fix is easy and simple but just mentioning this situations in case other people bump into this, might be worth looking into it. For anyone looking for a way to do this, here's my changes:

    Editor_AITrafficCar.cs
    Code (CSharp):
    1.             SerializedProperty meshRenderer = serializedObject.FindProperty("meshRenderer");
    2.             EditorGUI.BeginChangeCheck();
    3.             EditorGUILayout.PropertyField(meshRenderer, true);
    4.             if(EditorGUI.EndChangeCheck())
    5.             {
    6.                 serializedObject.ApplyModifiedProperties();
    7.             }
    8.  
    AITrafficCar
    Code (CSharp):
    1.  
    2.     public class AITrafficCar : MonoBehaviour
    3.     {
    4.         public MeshRenderer meshRenderer;
    5. ..................
    6.         public void RegisterCar(AITrafficWaypointRoute route)
    7.         {
    8.  
    9.             //Changed to support a separate mesh renderer not in the parent game object
    10.             if (brakeMaterial == null)
    11.             {
    12.                 if(GetComponent<MeshRenderer>() != null)
    13.                 {
    14.                     MeshRenderer mesh = GetComponent<MeshRenderer>();
    15.                     brakeMaterial = mesh.material;
    16.                 }
    17.                 else
    18.                 {
    19.                     brakeMaterial = meshRenderer.material;
    20.                 }
    21.  
    22.             }
    23.             assignedIndex = AITrafficController.Instance.RegisterCarAI(this, route);
    24.             startRoute = route;
    25.             rb = GetComponent<Rigidbody>();
    26.         }
    27.  
     
    Last edited: Jun 6, 2020
    Stephen_O likes this.
  44. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    There's not currently a way to do this, but thanks for asking as it helps me understand custom use cases, a couple other users have also inquired about having the option to disconnect cars from the controller. At this point I will make it a priority on the requested features list, new options will be added in the future, I don't have an ETA, that will allow you to add/remove cars from the controller after startup so you can do more custom stuff with cars.
     
  45. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505

    Thanks for the feedback on this implementation. Version 1.0.x has primarily been focused on new features to improve workflow without any foundational changes to allow for easy upgrades between version updates. I'll consider changes to the car setup/inspector, and any other requested areas when I eventually move to version 1.1.
     
  46. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.13 is almost ready for submission.

    • Added Yield Triggers, a new experimental feature that allows cars to yield for cross traffic at a traffic light intersection - this is useful for allowing left hand turn over oncoming traffic.

     
  47. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Version 1.0.13 submitted.

    • Added Yield Triggers, a new experimental feature that allows cars to yield for cross traffic at a traffic light intersection - this is useful for allowing left hand turn over oncoming traffic.
    • Improved waypoint gizmo to properly display bounds of transform rotation/scale and box collider center/size.
    • Fixed a bug when the car would exit a route to a new route it would skip the first/assigned point.
     
    combatsheep likes this.
  48. combatsheep

    combatsheep

    Joined:
    Jan 30, 2015
    Posts:
    129
    Hi,
    I want to update to ver 1.0.13 from ver 1.0.12.
    1)Can I update with overriding the asset?
    2)Do I have to add the prefabs?
    Import_Unity_Package.jpg

    Thanks.
     
    Stephen_O likes this.
  49. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,505
    Hi,

    I would recommend to copy the project if you're not using version control, just in case. But it should be ok to import over the current install.

    You don't need to import the prefabs folder, there were no changes to the car prefabs.
     
    combatsheep likes this.
  50. combatsheep

    combatsheep

    Joined:
    Jan 30, 2015
    Posts:
    129
    Hi Stephen_O,

    Thank you!
    I'll make a backup before importing and try it.;)
     
    Stephen_O likes this.