Skip to content
ReadySpawn

How to make NPC pedestrians and traffic AI for a city game

By Imersa Studios · Updated

Use waypoints: draw a graph of lanes and sidewalks, let cars and pedestrians pick the next waypoint, and add rules for junctions. Add a heat counter for police and spawn chasers when it rises. Spawn only near the player and recycle NPCs to save performance.

1. Waypoint graphs for traffic

Mark lane points along each road and connect them. A car picks a target waypoint, steers toward it and chooses the next one on arrival. At junctions, choose a random connected waypoint. This avoids pathfinding and costs very little.

Traffic System does this for you with lanes, junctions and speed limits, so you spend your time on the game instead of the plumbing.

2. Rules at junctions

  • Slow down when another car is close ahead (a short forward raycast).
  • Give way by priority or by a timed signal.
  • Never spawn a car where the player can see it appear.

3. Pedestrians

Pedestrians can use the same idea on sidewalk waypoints, with random pauses and a few idle animations. For reactions, add a trigger that makes them flee or stare when something loud happens. Animation basics are in how to animate a character in Blender.

4. A simple wanted level

Keep a number called heat. Crimes add to it, time without crimes decays it. At thresholds, spawn police cars that chase the player using the same driving code and drop heat when they lose sight for several seconds.

heat = Mathf.Max(0f, heat - decayPerSecond * Time.deltaTime);
if (heat >= 2f && chasers < maxChasers) SpawnPoliceNearPlayer();

Police weapon art can come from the police weapons pack.

5. Keep performance in budget

  • Only simulate NPCs near the player and recycle those that fall behind.
  • Use simple colliders and low poly meshes.
  • Cap the number of active cars and walkers, and check the budget with the frame time calculator.

Questions

Who wrote this

Imersa Studios builds the Unity systems, low poly packs and field guides sold on itch.io. The code and techniques here come from shipping those products. More about the studio.