Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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:
    782
    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:
    642
    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.)
     
    SparrowsNest likes this.
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    782
  4. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    267
    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