AI cars can be configured to simulate simple traffic behaviors like driving in a designated lane, turning into a new lane at intersections, stopping for red lights, and stopping if another car is in front to avoid collision.

This tutorial demonstrates how to configure AI cars, AI waypoint routes and traffic lights to create traffic simulations for your own projects.

City Traffic Demo Scene Location

Assets\TurnTheGameOn\Arcade Racer\Scenes\Traffic_Demo_RacingCity

This city scene has dozens of winding waypoint routes and intersections, it’s included as a more advanced example of what can be achieved. Be sure to explore this scene before getting started to familiarize yourself with the content.

Getting Started

In this tutorial, we will cover the basic concept and procedure of configuring and linking multiple waypoint routes for traffic vehicles to drive on using the included RacingCity scene. Once we have connected routes, we can add a traffic light to control when a vehicle can exit a route and proceed to the connected route(s).

  1. Open the following scene:
    Assets\TurnTheGameOn\Arcade Racer\Scenes\RacingCity_Empty

Setup AI Waypoint Routes

  1. Spawn an AI Waypoint Route into the scene by pressing Ctrl+Alt+R or selecting Tools/TurnTheGameOn/Arcade Racer/Create/AI_WaypointRoute

  1. Add waypoints to this route by pressing Alt+LeftClick in the Scene View on a Collider.

  1. Repeat steps 2 and 3 to create a second route that the AI car can turn onto.

  1. Repeat steps 2 and 3 to create a route used as a turning path (this route is very short, and only used in the intersection to help guide the car to the actual route/lane we want it on). This type of intersection route will typically require about 4 waypoints, the first is directly in front of the end point from route 1, and the last is before the start point of route 2.

Connect AI Waypoint Routes

  1. Connect the first waypoint route’s last waypoint (IKD_Waypoint 15) to the first waypoint (IKD_Waypoint 1) of the intersection route using the OnReachWaypointSettings NewRoutePoint array. When an AI car reaches the end of a waypoint route, it will use this array and choose a random point from it to use as a new route.

  1. Connect the intersection waypoint route’s last waypoint (IKD_Waypoint 4) to the first waypoint (IKD_Waypoint 1) of the second waypoint route. Note that when connecting a new route point a gizmo line should be drawn between the two points.

Add Traffic

  1. At this point, we’ve connected 3 AI waypoint routes that will allow an AI car to drive in the designated lane on the street and turn onto a new street lane using the intersection. Let’s add some traffic optimized cars to the first route’s SpawnTrafficVehicles array to make sure they will follow the waypoints correctly.

Press play to observe the AI behavior.

Set Waypoint Speed Limits

You may notice the AI cars do not slow down for the turn, we’ll need to set speed limits on the waypoints in order to limit the car AI top speed and have them behave as desired. Every time a car collides with a waypoint on it’s current route it will use that waypoint’s OnReachWaypointSettings.

  1. Select all the individual waypoints, enable UseSpeedLimit, and assign a speed limit. I’ve found that 25 is a good speed to use for cars approaching a sharp turn – I’ll just assign them all a 25 limit for simplicity.

  1. Select the intersection waypoints and assign a speed limit of 15. I’ve found this to be a good speed for intersection turning, you may want to reduce the speed of pre-intersection waypoints as well to slow the car appropriately before it reaches the intersection.

Press play to observe the AI behavior.

The cars should now turn at an acceptable speed and make it to the end of the 3rd route.

Create Another Route For The Intersection

  1. Repeat steps 2 and 3 to create a route for cross traffic at the intersection. I’ll make this route shorter since the tutorial does not require us making it any longer. Select all the individual waypoints on the new route, enable UseSpeedLimit, and assign a speed limit of 25.

  1. Assign the adjacent waypoint route’s first waypoint (IKD_Waypoint 1) to the last waypoint of the intersection route using the OnReachWaypointSettings NewRoutePoint array.

Add More Traffic

  1. Add some traffic optimized cars to the new intersection route’s SpawnTrafficVehicles array.

Setup A Traffic Light Manager

  1. Spawn a TrafficLightManager into the scene by pressing Ctrl+Alt+L or selecting Tools/TurnTheGameOn/Arcade Racer/Create/TrafficLightManager

This will have a generic traffic light object that you can configure for your needs; by default it’s conveniently located near our first road’s traffic light pole.

  1. Assign the to the corresponding Waypoint Route on the TrafficLight script; this script will now control the assigned route, only allowing traffic to proceed past the final waypoint when the light is green.

  1. Duplicate the IKD_TrafficLight object and move it above the other route that crosses through the intersection. Assign that Waypoint Route to the TrafficLight script.

  1. Assign the duplicate IKD_TrafficLight object to the parent TrafficLightManager as a new Traffic Light Cycle index. By default, the On Duration is set to 18 seconds, let’s change this to 10 to make sure we can see the traffic stop and start at the lights.

Press play to observe the AI behavior.

The AI cars should behave as expected, and only proceed through the intersection when the light is green.

You may repeat these steps as many times as necessary to create more complex road networks, with groups of traffic lights controlling waypoint routes, while in sequence to create traffic simulations.

This tutorial is now complete. Thank you.


Was this helpful?

Yes No
You indicated this topic was not helpful to you ...
Could you please leave a comment telling us why? Thank you!
Thanks for your feedback.

Post your comment on this topic.

Please do not use this for support questions.
For customer support, please contact us here.

Post Comment