Search Unity

How do I dynamically implement foot traffic around the player?

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Oct 22, 2014.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    My game has tons of NPCs with names, jobs, and other things the player cares about, but I'm finding that my levels are mighty sparse even with all of the NPCs doing their thing. To make things look more alive I built a system that spontaneously generates "junk" NPCs to fill out scenes, and I'm looking for a way to maintain a "bubble" of foot traffic around the PC by constantly spawning junk NPCs ahead of and behind the player, path them past, then despawn them as soon as they're out of sight. Where I'm struggling is with how to programmatically define "ahead of" and "behind": I can't spawn directly along the PC's facing, as that would result in visible pop-in, and if I just iterate through random points in the general direction of travel until I find one without line of sight to the player, I'm more likely than not to spawn NPCs inside rooms or behind walls that don't have access to the PC's route of travel, resulting in them despawning before they're ever seen.

    So is there an obvious way I'm missing to actually determine where NPCs are spawned? The last-ditch effort I thought of was to place invisible nodes along major walkways, spawn NPCs at the nearest node along the PC's direction of travel that he couldn't see, and send them to the nearest node behind him that he couldn't see, at which point they despawn, but that seems very brute-force-ish, and it would require me to create and curate a giant network of spawning nodes.
     
  2. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    have the spawn in occur in places like alley ways, or have doors that only npcs can use etc
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Do you have some sort of grid or navigation mesh used for path-finding? If so, you could iterate over that to find nearby nodes that can't be seen from the player's location. This strikes me as fairly easy work to spread over multiple frames, too (it's not like you need to spawn a new NPC every frame anyway).
     
  4. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Oh that's a great idea JoeStrout, my pathfinding is currently using a recast graph so that should be quite fast... do you think it would be more optimal to maintain a list of starting and ending nodes, or to make each NPC find their own start + end points? The latter has the advantage of making crowd management very simple, I could just spawn n junk NPCs and have them constantly find a start point, spawn, find an end point, navigate to it, if visible find a new end point, else de-spawn, change appearance, and re-spawn ahead of the player. However, I'm always nervous about leaving anything decentralized.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I always suggest starting with whatever is simplest, and then refactoring when you find performance or maintenance bottlenecks. Just wrap things up as neatly as you can, keeping implementation details private, to make refactoring easier later.

    This sounds like a cool project, and I'd love to see it sometime!
     
  6. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I'll be sure to let you know once it's in a demo-able state! It's very funny, but I'm finding that actual gameplay is almost trivial to implement compared to some of the things you have to carefully think through to make a believable, enjoyable sandbox ecology.
     
    JoeStrout likes this.