NPC Traffic Patterns

NPC Traffic Patterns

Posted by on Aug 13, 2015 in Game Development

Here’s the semi-final-ish-sorta example of NPC traffic patterns in a sector.

In order for the sector to seem populated and active, NPCs need to go about their business at least to the point where it looks like they’re getting things done. My recent focus has been entirely on getting these little prefabs to move around according to their “jobs”, which has been both easier than I thought, and also a real pain in the ass.

Color Chart

The white elements are “nodes”, populated centers of activity, and are labeled according to what they represent. Their identities are defined by components and tags for ease of use. They also have other components on them (or they will at some point), but for this test they’re just 3D prefabs labeled according to what I need them to be.

Blue prefabs are the cops. Their job is to patrol between nodes and keeping the peace. They’ll intercept pirates and might hassle the player under certain circumstances. Their route is calculated by taking all objects in the sector that are STATION or JUMPGATE, shuffling that list, and then moving between those object positions in a loop.

Red prefabs are merchants. They were supposed to be the most difficult to work with, but that wasn’t actually the case. Merchants jump in at a gate, travel to and dock at a station, and then undock and leave through another gate (could be the ingress gate…it doesn’t matter). Their pathing is simple: pick a random JUMPGATE object, a random STATION object, and another random JUMPGATE object, in that specific order, and move between them. Unlike the cops, though, when a merchant type enters the trigger sphere of a station, it becomes inactive for a semi-random amount of time. When the time is up, they reactivate and move to the next node. When they enter the gate trigger sphere, they become inactive until we need another merchant from the pool.

Green prefabs are miners. Their job is to stay motionless, mostly, at a “home” asteroid (if there are any in the sector). Every now and then they’ll pick a station to fly to, dump their cargo, and return to their home asteroid. Unlike merchants and police, the miners need to stay motionless most of the time (eventually when they have models, they’ll bob around and shoot mining lasers, but otherwise won’t move), and then randomly move to a station where they use the same docking timer system that merchants use. Miners were tricky because I didn’t want them to bunch up on one rock, and when they do (through happenstance) they had to not sit on top of one another. I had code that places objects around a central point, like positions on a clock, so I used that in case multiple miners get assigned to the same rock.

Problems With the Current System

If you watch the video, you may notice a few issues:

  1. The cops follow the same patrol route. This isn’t normal; for some reason, I lucked out during the recording that the cops happened to get the same roll of the dice when it came to choosing their patrol nodes.
  2. The cops “bounce” off the hull of their nodes. This is because unlike merchants and miners, the police use a straight proximity check that’s a bit too restrictive. I need them to turn when they’re further out, but I need to ensure that other NPCs don’t trigger it before they trigger the trigger — meaning the invisible trigger sphere that handles docking, jumping, and mining for NPCs who are into those things.
  3. The crappy “animations”. The cops turn like they’re driving light-cycles from Tron. I may have painted myself into an amateur’s corner with the way I’m doing movement which will cause issues for me when I try to get them to turn sooner and smoother, so you might hear a lot of swearing in the future.
  4. One of the miners doesn’t want to go back to work. I suspect it’s because this guy didn’t quite make it into the trigger sphere that would have set his work schedule, so I have to look at that. It’s probably related to problem #2.