playMaker

Author Topic: Navmesh Agent Destination, farthest reachable point on path?  (Read 4227 times)

craigz

  • Beta Group
  • Full Member
  • *
  • Posts: 234
    • Haven Made
Navmesh Agent Destination, farthest reachable point on path?
« on: March 03, 2018, 12:06:17 PM »
So I'm not quite sure the term that I'm looking for, but I'm trying to find the farthest point along a Navmesh agents path its able to get to? (especially if the path is incomplete?). And I'd like to set some sort of low cost delay as to how long it waits before rechecking its path (since I can have a fair amount of units).

Two scenarios I'm trying to work through, so maybe there's a better way to do this:



1.
Unit is told to go inside castle, but it can't because there is something in the way. I'd like to get the farthest point along that path up to the gate/point of entry. (usually this is inset from the wall, so the agent finds the gate no problem).


2.
There is a group of units blocking the way to a certain point. Have the game object wait until a path becomes available, then proceed.


best,

craigz
« Last Edit: March 19, 2018, 06:33:30 PM by craigz »

craigz

  • Beta Group
  • Full Member
  • *
  • Posts: 234
    • Haven Made
Re: Navmesh Agent Destination, farthest reachable point on path?
« Reply #1 on: March 19, 2018, 06:32:58 PM »
Bump with the updated subject line!

Deek

  • Full Member
  • ***
  • Posts: 133
Re: Navmesh Agent Destination, farthest reachable point on path?
« Reply #2 on: April 22, 2018, 05:00:54 PM »
The farthest path you describe is the distance from the NavMeshAgent to its current destination, but that isn't needed in your case.

You should grab the 'Path Finding' package from the Ecosystem if you didn't already, since it contains an exhaustive list of actions that covers pretty much all the basic stuff you can do with Unity's built-in pathfinding system. Everything more complicated should be created by combining these to achieve the desired result.
My own custom actions might also come in handy, namely "Find Closest Agent" and "Nav Mesh Agent Move To", which I've added to the attachments or can be found in the Ecosystem.

In your scenarios, everything that shouldn't be passable needs a "Nav Mesh Obstacle" component (use as many as you need), which represent a box or capsule collider (you can also make the capsule collider act as a sphere by reducing the height) that the NavMeshAgent not only doesn't go through, but also takes into account when generating the path, which means that it will automatically go around the obstacle, if you enable carving on it (this carves the collider size into the underlying NavMesh, making that section impassable by any agent | you can also make the collider bigger than the object itself, which increases the offset to that object when going around it and the collider boundaries of obstacles can even overlap or be nested to combine them).

For your first scenario you don't want your player to move to the goal directly, but rather add subsections where the agent moves to first (in this case add an empty GameObject where the gate is and set this as the target the agent should go to; after that you could warp the agent to the goal with the 'Agent Warp' action | I prefer to use empty GameObjects as placeholders when it comes to animations or subtargets, because you can move them more easily around in the editor than specifying Vector3 positions).
Here you can use the "Nav Mesh Agent Move To" action and because you added an obstacle component to the barriers, your agent will automatically search for the shortest way around them to the gate (of course you always need to bake your NavMesh in the 'Navigation' window when changing anything).

The second scenario is a bit more complex and I'm not quite sure how to achieve this, but still got some ideas:
- if those blocking units are static, by giving each of those a NavMesh Obstacle component and checking if the path is invalid/stale (don't know which one :P)
- by shooting a NavMesh Raycast (they are different from normal Raycasts) to the goal with the action "Agent Ray Cast" and checking if it hit any of the units
- by creating a path and continuously sampling its position ('Agent Sample Path Position'), then using that position with my "Find Closest Agent" action (every blocking unit would then need a NavMeshAgent component) and seeing if the distance is in a certain threshold (if that distance is bigger than your players size it should be able to pass through for example)

In the end you should experiment with the given actions from the Path Finding package or try to alter your approach.
« Last Edit: April 22, 2018, 05:05:41 PM by Deek »