Search Unity

Resolved How to set precise paths for NPCs like in Stardew Valley?

Discussion in 'Navigation' started by anrorolove, Sep 28, 2020.

  1. anrorolove

    anrorolove

    Joined:
    Sep 25, 2020
    Posts:
    18
    What is the most efficient way to create scheduled custom path for different NPCs in a RPG 2D game where they can pause at certain points before resuming to walk. If the player interacts with them, they will stop walking and engage in a dialogue. They aren't enemies nor following the player just characters in a world.

    I have so far only seen mostly waypoint system as the most followed approach for game in topdown 2D game. How did Stardew Valley managed to achieve that? Any suggestions or script/coding to include will be helpful!
     
  2. nikofon007

    nikofon007

    Joined:
    Sep 10, 2020
    Posts:
    38
    There is no way to create a custom NavMeshPath. I assume the best way to solve your problem is just to create an array of vector3s that your NPC will loop through (via SetDestination(ArrayName[i+1]) every time the agent reaches ArrayName) and just creating an event that will call isStopped() and initiate a dialog when player interacts with a NPC.
    Ofcourse you can customise NPC's behaviour by adding some random checks(for example the next destionation will not be i + 1 but a random index or you can add a chance for NPC to stay still for x seconds when they reach their destination)
     
  3. Skynet766

    Skynet766

    Joined:
    Sep 13, 2020
    Posts:
    22
    Having the AI move between a series of waypoints isn't something the NavmeshAgent specifically needs to take care of. The Navmesh agent is for getting your NPC from A to B with the shortest path possible, avoiding obstacles etc.

    Moving between two waypoints is what the NavmeshAgent is for. When it comes to things like "move to various places and stop to sightsee" that means you need to add something like a Coroutine or State Machine to control that. A coroutine may do something like this:
    - Tell the AI to move to the first waypoint
    - Wait until the AI reaches the destination
    - Wait for however long the AI is meant to stand at the waypoint
    - Tell the AI to move to the second waypoint...
     
    anrorolove likes this.