Search Unity

How to make AI go into a search pattern?

Discussion in 'Scripting' started by Lethn, Mar 13, 2018.

  1. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    There's plenty of easy ways to have navmesh A.I follow waypoints and randomly wonder, that's actually fairly easy to work out. How does one though set it up so that an agent searches for the player in a certain area? I'm thinking about this and I'm wondering is there perhaps a collider that the player should have that the agent is permitted to wonder about in or is it more complicated than that?

    By a search pattern, I'm thinking of games like Farcry and Deus Ex: Mankind Divided where you have NPCs that don't necessarily go back to patrol but actively start searching for you however they're not just scripted to follow the player they wonder in the general area the player was in.

    A more advanced version of this would be Shadow of Mordor where a silhouette of the player is left behind in the actual game and the orcs attempt to find that as it's the last location they sighted the player.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Here's a relatively simple approach. Let's assume you have different "modes" for the NPC (Patrol, Chase, Search), and the NPC is currently in the Search mode. The NPC doesn't know where the player is, but they know where the player was when they last saw the player. Make that position (lastKnownPlayerPosition) the navMeshAgent destination. When the NPC arrives at that position, enter a loop where the NPC does the following:
    • Choose a new destination, based on picking a random direction and distance, and ensuring that it's a reasonable destination
    • When the NPC reaches that point, repeat.
    • If the agent ever gets line of sight with the player, transition to Chase state.
    After enough times through the loop, revert back to the Patrol state, so the agent goes back to their patrol route.

    From there, you could try to make the AI smarter by having the player's last known velocity vector inform the random destination. (For example, if the player was heading east when the NPC last saw him, the NPC would keep going east first.)
     
    SparrowGS likes this.
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    This exactly what I did in a game, thanks for the velocity tip, I've been trying to to figure out a nice way of doing that