Spawn Rates And Clones
Without a limit on when the NPCs spawn, they’ll all spawn at once, and if there’s a limited number of spawning locations, the NPCs will all bunch up. This not only looks stupid and unrealistic, but if there’s a capsule collider on the NPCs (not sure if I’ll bother with worrying about collisions), spawning them in the exact same spot makes the prefabs bounce off one another, sending the placeholder primitives spinning off into space.
What I needed was a way to make it look like NPCs were jumping into the system at random intervals, but not insanely infrequent intervals. Once an NPC enters the system, they’re on the move and visible until they dock, and are on the move and visible again until they reach the exit jumpgate. If the points of interest are far away, a low spawn rate means the player might not actually see any NPCs. That’s not exciting. On the flip-side, I don’t want the space-lanes to be clogged with NPCs because that would be overkill, and I’d need to seed the NPC name database with an astronomical amount of records (or create a random naming dictionary, which I might do in the end).
So in the Update() method, the system checks the current Time.time() (which is the number of seconds since the game has started) against the last spawn check time, plus a variable amount of seconds. If the check passes, there’s a secondary check — a “coin flip” that picks a random number between 0 and 20. If that number is less than 2, an NPC is spawned. The time check ensures that we’re not flipping the coin every frame, because statistically that is the same as pumping out NPCs at an insane rate, and the coin flip ensures that NPCs aren’t spawning in at a predictable rate.